12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364 |
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using Sirenix.OdinInspector;
- using UnityEngine.Audio;
- namespace KairoEngine.Core.ConfigOptions
- {
- [CreateAssetMenu(fileName = "VolumeConfigOption", menuName = "KairoEngine/ConfigOptions/Volume", order = 1), HideMonoScript]
- public class VolumeConfigOption : ConfigOptionBase
- {
- [PropertyOrder(4)] public AudioMixerGroup audioMixerChannel;
- [PropertyOrder(4)] public AudioMixer masterMixer;
- [PropertyOrder(5), Range(0.0001f, 1f)] public float defaultVolume;
- [PropertyOrder(6), TextArea(), HideLabel, PropertySpace(4,0)] public string description;
- private float currentVolume = 0;
-
- public override ConfigOptionUiData GetUiData()
- {
- Debug.Log($"Current {title} is {currentVolume}");
- var sliderData = new ConfigOptionUiData.SliderData(0.0001f, 1f, currentVolume);
- return new ConfigOptionUiData(title, description, sliderData);
- }
- public override void LoadValue(bool debug)
- {
- currentVolume = PlayerPrefs.GetFloat(id, defaultVolume);
- masterMixer.GetFloat(id, out float oldVolume);
- ApplyConfig();
- masterMixer.GetFloat(id, out float newVolume);
- if(debug) Debug.Log($"{title} changed from {Mathf.Pow(10, oldVolume/20).ToString("F2")} to {Mathf.Pow(10, newVolume/20).ToString("F2")}");
- }
- public override void SetDefaultValue()
- {
- currentVolume = defaultVolume;
- PlayerPrefs.SetFloat(id, defaultVolume);
- ApplyConfig();
- }
- public override void SetValue(ConfigOptionData data, bool save = false)
- {
- PlayerPrefs.SetFloat(id, data.floatValue);
- currentVolume = data.floatValue;
- ApplyConfig();
- if(save) PlayerPrefs.SetFloat(id, currentVolume);
- }
- private void ApplyConfig()
- {
- var value = Mathf.Log10(currentVolume) * 20f;
- bool worked = masterMixer.SetFloat(id, value);
- if(worked == false) Debug.LogError($"Error setting volume for {title}");
- }
- [Button("Apply Default"), PropertyOrder(100), PropertySpace(4,4)]
- private void ApplyDefaultConfig()
- {
- currentVolume = defaultVolume;
- ApplyConfig();
- }
- }
- }
|