VoiceManager.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. using UnityEngine;
  2. using FairyGUI;
  3. using GFGGame.Launcher;
  4. using YooAsset;
  5. using System.Collections;
  6. using System.Threading.Tasks;
  7. using System;
  8. namespace GFGGame
  9. {
  10. public class VoiceManager : SingletonMonoBase<VoiceManager>
  11. {
  12. private AudioSource player;
  13. private AssetOperationHandle handle;
  14. private Coroutine coroutine;
  15. private bool _isOn = true;
  16. public bool isOn
  17. {
  18. get
  19. {
  20. return _isOn;
  21. }
  22. set
  23. {
  24. if (_isOn != value)
  25. {
  26. _isOn = value;
  27. if (_isOn)
  28. {
  29. //player.volume = 1;
  30. }
  31. else
  32. {
  33. //player.volume = 0;
  34. Stop();
  35. }
  36. LocalCache.SetBool(GameConfig.VOICE_KEY, _isOn);
  37. }
  38. }
  39. }
  40. private void Awake()
  41. {
  42. player = this.gameObject.AddComponent<AudioSource>();
  43. player.priority = 256;
  44. //设置这个声音会大一些
  45. player.spatialBlend = 1f;
  46. isOn = LocalCache.GetBool(GameConfig.VOICE_KEY, true);
  47. player.volume = LocalCache.GetFloat(GameConfig.VOICE_VOLUMN_KEY, 1.0f);
  48. }
  49. public void LoadRes(string path)
  50. {
  51. if (player.volume == 0 || !isOn || !YooAssets.CheckResExist(path))
  52. {
  53. return;
  54. }
  55. handle = YooAssets.LoadAssetSync<AudioClip>(path);
  56. player.clip = handle.AssetObject as AudioClip;
  57. }
  58. public void PlayVoice()
  59. {
  60. if (player.clip != null)
  61. {
  62. player.Play();
  63. }
  64. }
  65. public void StopVoice()
  66. {
  67. if(player == null)
  68. {
  69. return;
  70. }
  71. player.Stop();
  72. handle = null;
  73. player.clip = null;
  74. }
  75. public float GetClipLength()
  76. {
  77. if (player.clip != null)
  78. {
  79. return player.clip.length;
  80. }
  81. return 0;
  82. }
  83. public float GetClipRemainingLength()
  84. {
  85. if (player.clip != null)
  86. {
  87. return Mathf.Ceil(player.clip.length - player.time);
  88. }
  89. return 0;
  90. }
  91. public void PlayOneShotCroutine(string path, Action action = null)
  92. {
  93. if (coroutine != null)
  94. {
  95. StopCoroutine(coroutine);
  96. }
  97. handle?.Release();
  98. if (_isOn)
  99. {
  100. if (YooAssets.CheckResExist(path))
  101. {
  102. coroutine = StartCoroutine(PlayOneShot(path, action));
  103. }
  104. else
  105. {
  106. action?.Invoke();
  107. }
  108. }
  109. }
  110. private IEnumerator PlayOneShot(string path, Action action = null)
  111. {
  112. //AudioClip clip = GFGAsset.Load<AudioClip>(path);
  113. handle = YooAssets.LoadAssetAsync<AudioClip>(path);
  114. yield return handle;
  115. player.clip = handle.AssetObject as AudioClip;
  116. player.Play();
  117. WaitForSound(action);
  118. }
  119. public void Stop()
  120. {
  121. player.Stop();
  122. }
  123. // 音频播放完成的回调函数
  124. private async Task WaitForSound(Action action = null)
  125. {
  126. int milliseconds = (int)(player.clip.length * 1000);
  127. await Task.Delay(milliseconds);
  128. action?.Invoke();
  129. }
  130. public bool IsPlaying()
  131. {
  132. return player.isPlaying;
  133. }
  134. public void SetVolumn(float volumn)
  135. {
  136. player.volume = volumn;
  137. }
  138. public float GetVolumn()
  139. {
  140. return player.volume;
  141. }
  142. }
  143. }