You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
CrowdControl/Assets/Source/Scripts/SoundSwitcher.cs

41 lines
1.1 KiB
C#

3 months ago
using UnityEngine;
using UnityEngine.Audio;
using UnityEngine.UI;
using static D2D.Utilities.CommonGameplayFacade;
public class SoundSwitcher : MonoBehaviour
{
[SerializeField] private AudioMixer audioMixer;
[SerializeField] private Button audioButton;
[SerializeField] private Image cross;
private const string MasterVolume = "MasterVolume";
private const float Mute = -80f;
private void Awake()
{
audioButton.onClick.AddListener(SwitchSound);
UpdateCross();
}
private void Start()
{
float masterValue = PlayerPrefs.GetFloat(MasterVolume, 0);
audioMixer.SetFloat(MasterVolume, masterValue);
}
private void SwitchSound()
{
float value = PlayerPrefs.GetFloat(MasterVolume, 0);
PlayerPrefs.SetFloat(MasterVolume, value == 0 ? Mute : 0);
audioMixer.SetFloat(MasterVolume, PlayerPrefs.GetFloat(MasterVolume, 0));
UpdateCross();
}
private void UpdateCross()
{
cross.gameObject.SetActive(PlayerPrefs.GetFloat(MasterVolume, 0) == Mute);
}
}