// Assets/_Project/Scripts/Audio/MusicPlayer.cs using UnityEngine; namespace TD.Audio { /// /// Plays a single looping music track for the scene it lives in. /// Add to a GameObject in the scene, assign a clip, and it starts on load. /// /// /// Volume is the authored scaled by the player's music slider /// (). Unlike one-shot SFX, the track is /// continuous, so this subscribes to and /// re-applies live while the slider is dragged. /// [RequireComponent(typeof(AudioSource))] public class MusicPlayer : MonoBehaviour { [SerializeField] private AudioClip clip; [Range(0f, 1f)] [SerializeField] private float volume = 1f; private AudioSource source; private void Awake() { source = GetComponent(); } private void OnEnable() { AudioVolumeSettings.OnChanged += ApplyVolume; } private void OnDisable() { AudioVolumeSettings.OnChanged -= ApplyVolume; } private void Start() { source.clip = clip; source.loop = true; source.playOnAwake = false; ApplyVolume(); source.Play(); } private void ApplyVolume() { if (source == null) return; source.volume = volume * AudioVolumeSettings.MusicScalar; } } }