MusicManager.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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 settingVolumn = 1f; // 设置音量
  14. private float tempVolume = 1f; // 临时音量
  15. private float changeBaseValue = 0.01f; // 缓动单位改变基数
  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(GameConfig.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(GameConfig.MUSIC_KEY, true);
  48. settingVolumn = LocalCache.GetFloat(GameConfig.MUSIC_VOLUMN_KEY, 1.0f);
  49. player.volume = settingVolumn;
  50. }
  51. private void Update()
  52. {
  53. if (player.clip == null || !isOn)
  54. {
  55. return;
  56. }
  57. // 播放语音时背景音乐变小
  58. if (VoiceManager.Instance.IsPlaying())
  59. {
  60. tempVolume = 0.17f;
  61. }
  62. else
  63. {
  64. tempVolume = settingVolumn;
  65. }
  66. // 音量缓动变化
  67. if (player.volume != tempVolume)
  68. {
  69. player.volume = Mathf.Lerp(player.volume, tempVolume, changeBaseValue);
  70. if (Mathf.Abs(player.volume - tempVolume) <= 0.05)
  71. {
  72. player.volume = tempVolume;
  73. }
  74. }
  75. }
  76. public void PlayCroutine(string path, bool must = false, float volume = 1.0f)
  77. {
  78. if (currentName != path || must)
  79. {
  80. currentName = path;
  81. if (coroutine != null)
  82. {
  83. StopCoroutine(coroutine);
  84. }
  85. handle?.Release();
  86. if (_isOn)
  87. {
  88. coroutine = StartCoroutine(Play(path, volume));
  89. }
  90. }
  91. }
  92. private IEnumerator Play(string path, float volume = 1.0f)
  93. {
  94. //AudioClip clip = GFGAsset.Load<AudioClip>(path);
  95. handle = YooAssets.LoadAssetAsync<AudioClip>(path);
  96. yield return handle;
  97. player.clip = handle.AssetObject as AudioClip;
  98. player.volume = volume;
  99. player.Play();
  100. }
  101. public float GetSoundTime()
  102. {
  103. if (player.clip != null)
  104. {
  105. return player.clip.length;
  106. }
  107. return 0;
  108. }
  109. public void Pause()
  110. {
  111. player.Pause();
  112. }
  113. public void UnPause()
  114. {
  115. player.UnPause();
  116. }
  117. public void Stop()
  118. {
  119. handle?.Release();
  120. player?.Stop();
  121. }
  122. public void SetVolume(float volume)
  123. {
  124. player.volume = volume;
  125. }
  126. public void SetSettingVolumn(float volume)
  127. {
  128. settingVolumn = volume;
  129. }
  130. public float GetSettingVolumn()
  131. {
  132. return settingVolumn;
  133. }
  134. }
  135. }