68 lines
2.2 KiB
C#
68 lines
2.2 KiB
C#
using System;
|
|
using TemplateUnity.MainMenu;
|
|
using UnityEngine;
|
|
|
|
namespace TemplateUnity.Core
|
|
{
|
|
public class OptionHandler: MonoBehaviour
|
|
{
|
|
public static OptionHandler Instance { get; private set; }
|
|
|
|
public event EventHandler OnAudiosettingChange;
|
|
|
|
[SerializeField] private float maxVolume = 10;
|
|
|
|
private const string SaveKeyWords = "TMP";
|
|
|
|
private float _masterVolume;
|
|
private float _musicVolume;
|
|
private float _sfxVolume;
|
|
|
|
private void Awake()
|
|
{
|
|
if (Instance)
|
|
{
|
|
Debug.LogWarning($"{typeof(OptionHandler)} already instantiated! {transform}");
|
|
Destroy(gameObject);
|
|
return;
|
|
}
|
|
|
|
Instance = this;
|
|
|
|
_masterVolume = PlayerPrefs.GetFloat($"{SaveKeyWords}_MasterVolume", maxVolume);
|
|
_musicVolume = PlayerPrefs.GetFloat($"{SaveKeyWords}_MusicVolume", maxVolume);
|
|
_sfxVolume = PlayerPrefs.GetFloat($"{SaveKeyWords}_SfxVolume", maxVolume);
|
|
}
|
|
|
|
private void Start()
|
|
{
|
|
DontDestroyOnLoad(gameObject.transform);
|
|
|
|
UIOptionSection.onAnyMasterVolumeValueChanged += (_, amount) =>
|
|
{
|
|
_masterVolume = amount;
|
|
PlayerPrefs.SetFloat($"{SaveKeyWords}_MasterVolume", amount);
|
|
OnAudiosettingChange?.Invoke(this, EventArgs.Empty);
|
|
};
|
|
|
|
UIOptionSection.onAnyMusicVolumeValueChanged += (_, amount) =>
|
|
{
|
|
_musicVolume = amount;
|
|
PlayerPrefs.SetFloat($"{SaveKeyWords}_MusicVolume", amount);
|
|
OnAudiosettingChange?.Invoke(this, EventArgs.Empty);
|
|
};
|
|
|
|
UIOptionSection.onAnySfxVolumeValueChanged += (_, amount) =>
|
|
{
|
|
_sfxVolume = amount;
|
|
PlayerPrefs.SetFloat($"{SaveKeyWords}_SfxVolume", amount);
|
|
OnAudiosettingChange?.Invoke(this, EventArgs.Empty);
|
|
};
|
|
}
|
|
|
|
public float MaxVolume => maxVolume;
|
|
public float MasterVolume => _masterVolume;
|
|
public float MusicVolume => _musicVolume;
|
|
public float SfxVolume => _sfxVolume;
|
|
}
|
|
} |