Megacity demo game for UOS
您最多选择25个主题 主题必须以中文或者字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

71 行
2.4 KiB

using Unity.MegaCity.Audio;
using UnityEngine;
using UnityEngine.UIElements;
namespace Unity.MegaCity.UI
{
/// <summary>
/// Modifies the AudioMixer Groups via AudioMaster
/// Reads the data to set the Sliders in the AudioView
/// </summary>
public class UISoundSettings : UISettingsTab
{
private Slider m_VolumeSlider;
private Slider m_SoundFXSlider;
private Slider m_MusicSlider;
public override string TabName => "audio";
protected override void Initialization()
{
var root = GameSettingsView.Q<GroupBox>().Q<VisualElement>("sliders");
m_VolumeSlider = root.Q<Slider>("volume");
m_SoundFXSlider = root.Q<Slider>("sound-fx");
m_MusicSlider = root.Q<Slider>("music");
m_SoundFXSlider.RegisterValueChangedCallback(OnSoundFXUpdated);
m_VolumeSlider.RegisterValueChangedCallback(OnVolumeUpdated);
m_MusicSlider.RegisterValueChangedCallback(OnMusicUpdated);
base.Initialization();
}
protected override void SaveCurrentState()
{
base.SaveCurrentState();
UpdateSliderCurrentState(m_VolumeSlider);
UpdateSliderCurrentState(m_SoundFXSlider);
}
private void OnDestroy()
{
if (IsSet)
{
m_SoundFXSlider.UnregisterValueChangedCallback(OnSoundFXUpdated);
m_VolumeSlider.UnregisterValueChangedCallback(OnVolumeUpdated);
m_MusicSlider.UnregisterValueChangedCallback(OnMusicUpdated);
}
}
private void OnVolumeUpdated(ChangeEvent<float> value)
{
AudioMaster.Instance.volume.audioMixer.SetFloat("volume", Mathf.Log(value.newValue) * 20);
}
private void OnSoundFXUpdated(ChangeEvent<float> value)
{
AudioMaster.Instance.soundFX.audioMixer.SetFloat("sound-fx", Mathf.Log(value.newValue) * 20);
}
private void OnMusicUpdated(ChangeEvent<float> value)
{
AudioMaster.Instance.music.audioMixer.SetFloat("music", Mathf.Log(value.newValue) * 20);
}
public override void Reset()
{
base.Reset();
ResetSliderCurrentState(m_VolumeSlider);
ResetSliderCurrentState(m_SoundFXSlider);
}
}
}