Add settings menu to control audio and return to main menu

This commit is contained in:
Ben Calegari 2026-07-28 22:01:28 -07:00
parent c88d3fc625
commit 0b7bc936ef
13 changed files with 697 additions and 22 deletions

View file

@ -7,6 +7,12 @@ 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.
/// </summary>
/// <remarks>
/// Volume is the authored <see cref="volume"/> scaled by the player's music slider
/// (<see cref="AudioVolumeSettings.MusicScalar"/>). Unlike one-shot SFX, the track is
/// continuous, so this subscribes to <see cref="AudioVolumeSettings.OnChanged"/> and
/// re-applies live while the slider is dragged.
/// </remarks>
[RequireComponent(typeof(AudioSource))]
public class MusicPlayer : MonoBehaviour
{
@ -14,14 +20,36 @@ namespace TD.Audio
[Range(0f, 1f)]
[SerializeField] private float volume = 1f;
private AudioSource source;
private void Awake()
{
source = GetComponent<AudioSource>();
}
private void OnEnable()
{
AudioVolumeSettings.OnChanged += ApplyVolume;
}
private void OnDisable()
{
AudioVolumeSettings.OnChanged -= ApplyVolume;
}
private void Start()
{
var src = GetComponent<AudioSource>();
src.clip = clip;
src.volume = volume;
src.loop = true;
src.playOnAwake = false;
src.Play();
source.clip = clip;
source.loop = true;
source.playOnAwake = false;
ApplyVolume();
source.Play();
}
private void ApplyVolume()
{
if (source == null) return;
source.volume = volume * AudioVolumeSettings.MusicScalar;
}
}
}