SanAndreasUnity/Assets/Scripts/Settings/SoundSettings.cs

46 lines
1.5 KiB
C#
Raw Normal View History

2021-01-02 22:22:52 +00:00
using SanAndreasUnity.Behaviours.Vehicles;
using SanAndreasUnity.UI;
2021-01-02 18:37:11 +00:00
using UnityEngine;
namespace SanAndreasUnity.Settings
{
public class SoundSettings : MonoBehaviour
{
[Range(0f, 1f)] public float defaultSoundVolume = 0.5f;
2021-01-02 22:22:52 +00:00
OptionsWindow.FloatInput m_soundVolume = new OptionsWindow.FloatInput ("Global sound volume", 0, 1) {
2021-01-02 18:37:11 +00:00
getValue = () => AudioListener.volume,
setValue = (value) => { AudioListener.volume = value; },
persistType = OptionsWindow.InputPersistType.OnStart
};
2021-01-02 22:22:52 +00:00
OptionsWindow.FloatInput m_radioVolume = new OptionsWindow.FloatInput ("Radio volume", 0, 1) {
isAvailable = () => VehicleManager.Instance != null,
getValue = () => VehicleManager.Instance.radioVolume,
setValue = ApplyRadioVolume,
persistType = OptionsWindow.InputPersistType.OnStart,
};
2021-01-02 18:37:11 +00:00
private void Awake()
{
// apply default value
AudioListener.volume = this.defaultSoundVolume;
2021-01-02 22:22:52 +00:00
OptionsWindow.RegisterInputs(
"SOUND",
m_soundVolume,
m_radioVolume);
}
private static void ApplyRadioVolume(float newValue)
{
VehicleManager.Instance.radioVolume = newValue;
foreach (var vehicle in Vehicle.AllVehicles)
{
if (vehicle.RadioAudioSource != null)
vehicle.RadioAudioSource.volume = newValue;
}
2021-01-02 18:37:11 +00:00
}
}
}