using TMPro; using UnityEngine; using UnityEngine.UI; /// Binds a UI Slider (and optional value label) to the global MasterVolume level. /// The slider's range is authored in the inspector (0 - 1); this only reads/writes /// the value so scene-side range edits are respected. public class MasterVolumeSlider : MonoBehaviour { [SerializeField] private Slider slider; [Tooltip("Optional TMP label showing the current volume as a percentage (e.g. \"80%\").")] [SerializeField] private TMP_Text valueLabel; void OnEnable() { if (!slider) return; slider.SetValueWithoutNotify(MasterVolume.Volume); UpdateLabel(MasterVolume.Volume); slider.onValueChanged.AddListener(OnSliderChanged); } void OnDisable() { if (slider) slider.onValueChanged.RemoveListener(OnSliderChanged); } void OnSliderChanged(float value) { MasterVolume.Volume = value; UpdateLabel(value); } void UpdateLabel(float value) { if (valueLabel) valueLabel.text = Mathf.RoundToInt(value * 100f) + "%"; } }