MusicManager.cs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. using System.Collections;
  2. using UnityEngine;
  3. using GFGGame.Launcher;
  4. using YooAsset;
  5. namespace GFGGame
  6. {
  7. public class MusicManager : SingletonMonoBase<MusicManager>
  8. {
  9. private AudioSource player;
  10. private string currentName;
  11. private AssetOperationHandle handle;
  12. private Coroutine coroutine;
  13. private float storyDialogVolumn = 0.6f;
  14. private float storyDialogVolumnWhenVoice = 0.17f;
  15. private float storyDialogChangeValue = 0.3f;
  16. private bool _isOn = true;
  17. public bool isOn
  18. {
  19. get
  20. {
  21. return _isOn;
  22. }
  23. set
  24. {
  25. if(_isOn != value)
  26. {
  27. _isOn = value;
  28. if(_isOn)
  29. {
  30. if(currentName != null)
  31. {
  32. PlayCroutine(currentName, true);
  33. }
  34. }
  35. else
  36. {
  37. Stop();
  38. }
  39. LocalCache.SetBool(LauncherConfig.MUSIC_KEY, _isOn);
  40. }
  41. }
  42. }
  43. private void Awake()
  44. {
  45. player = this.gameObject.AddComponent<AudioSource>();
  46. player.loop = true;
  47. isOn = LocalCache.GetBool(LauncherConfig.MUSIC_KEY, true);
  48. }
  49. private void Start()
  50. {
  51. }
  52. private void Update()
  53. {
  54. if (player.clip == null)
  55. {
  56. return;
  57. }
  58. if (VoiceManager.Instance.IsPlaying())
  59. {
  60. if (player.volume > storyDialogVolumnWhenVoice)
  61. {
  62. player.volume -= storyDialogChangeValue * Time.deltaTime;
  63. player.volume = Mathf.Max(player.volume, storyDialogVolumnWhenVoice);
  64. }
  65. }
  66. else
  67. {
  68. if (player.volume < storyDialogVolumn)
  69. {
  70. player.volume += storyDialogChangeValue * Time.deltaTime;
  71. player.volume = Mathf.Min(player.volume, storyDialogVolumn);
  72. }
  73. }
  74. }
  75. public void PlayCroutine(string path, bool must = false, float volume = 1.0f)
  76. {
  77. if(currentName != path || must)
  78. {
  79. currentName = path;
  80. if(coroutine != null)
  81. {
  82. StopCoroutine(coroutine);
  83. }
  84. handle?.Release();
  85. if (_isOn)
  86. {
  87. coroutine = StartCoroutine(Play(path, volume));
  88. }
  89. }
  90. }
  91. private IEnumerator Play(string path, float volume = 1.0f)
  92. {
  93. //AudioClip clip = GFGAsset.Load<AudioClip>(path);
  94. handle = YooAssets.LoadAssetAsync<AudioClip>(path);
  95. yield return handle;
  96. player.clip = handle.AssetObject as AudioClip;
  97. player.volume = volume;
  98. player.Play();
  99. }
  100. public float GetSoundTime()
  101. {
  102. if (player.clip != null)
  103. {
  104. return player.clip.length;
  105. }
  106. return 0;
  107. }
  108. public void Pause()
  109. {
  110. player.Pause();
  111. }
  112. public void UnPause()
  113. {
  114. player.UnPause();
  115. }
  116. public void Stop()
  117. {
  118. handle?.Release();
  119. player?.Stop();
  120. }
  121. public void SetDefaultStoryDialogVolume()
  122. {
  123. player.volume = storyDialogVolumn;
  124. }
  125. }
  126. }