Files
AIGame/AIClient/Assets/HotUpdate/Scripts/Gameplay/MainMenuMusic.cs
T

70 lines
1.8 KiB
C#

using UnityEngine;
namespace HotUpdate.Gameplay
{
/// <summary>Playback for the authored main-menu music prefab, independent of SFX.</summary>
public sealed class MainMenuMusic : MonoBehaviour
{
public const string PrefabName = "MainMenuMusic";
public const string PrefabFolder = "Assets/Bundles/Sound/Prefab";
[SerializeField] AudioSource _source;
bool _inMenu = true, _appPaused, _started, _began, _paused;
public AudioSource Source => _source;
void OnEnable()
{
PrototypeClientPrefs.AudioPreferencesChanged += ApplyPlayback;
ApplyPlayback();
}
void Start()
{
_started = true;
ApplyPlayback();
}
public void SetInMenu(bool inMenu)
{
_inMenu = inMenu;
ApplyPlayback();
}
void ApplyPlayback()
{
if (!_started || _source == null) return;
if (isActiveAndEnabled && _inMenu && !_appPaused && PrototypeClientPrefs.Music)
{
if (!_began)
{
_source.Play();
_began = true;
}
else if (_paused) _source.UnPause();
_paused = false;
}
else if (_began && !_paused)
{
_source.Pause();
_paused = true;
}
}
void OnApplicationPause(bool paused)
{
_appPaused = paused;
ApplyPlayback();
}
void OnDisable()
{
PrototypeClientPrefs.AudioPreferencesChanged -= ApplyPlayback;
if (_source != null && _began && !_paused)
{
_source.Pause();
_paused = true;
}
}
}
}