VoiceManager.cs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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 { return _isOn; }
  19. set
  20. {
  21. if (_isOn != value)
  22. {
  23. _isOn = value;
  24. if (_isOn)
  25. {
  26. //player.volume = 1;
  27. }
  28. else
  29. {
  30. //player.volume = 0;
  31. Stop();
  32. }
  33. LocalCache.SetBool(GameConfig.VOICE_KEY, _isOn);
  34. }
  35. }
  36. }
  37. private void Awake()
  38. {
  39. player = this.gameObject.AddComponent<AudioSource>();
  40. player.priority = 256;
  41. //设置这个声音会大一些
  42. player.spatialBlend = 1f;
  43. isOn = LocalCache.GetBool(GameConfig.VOICE_KEY, true);
  44. player.volume = LocalCache.GetFloat(GameConfig.VOICE_VOLUMN_KEY, 1.0f);
  45. }
  46. public void LoadResAsync(string path)
  47. {
  48. if (player.volume == 0 || !isOn || !YooAssets.CheckResExist(path))
  49. return;
  50. var operation = YooAssets.LoadAssetAsync<AudioClip>(path);
  51. operation.Completed += (op) =>
  52. {
  53. // 直接操作Unity对象(假设回调已在主线程)
  54. if (op.Status == EOperationStatus.Succeed)
  55. player.clip = op.AssetObject as AudioClip;
  56. else
  57. Debug.LogError($"加载失败: {path}");
  58. };
  59. }
  60. public void PlayVoice()
  61. {
  62. if (player.clip != null)
  63. {
  64. player.Play();
  65. }
  66. }
  67. public void StopVoice()
  68. {
  69. if (player == null)
  70. {
  71. return;
  72. }
  73. player.Stop();
  74. handle = null;
  75. player.clip = null;
  76. }
  77. public float GetClipLength()
  78. {
  79. if (player.clip != null)
  80. {
  81. return player.clip.length;
  82. }
  83. return 0;
  84. }
  85. public float GetClipRemainingLength()
  86. {
  87. if (player.clip != null)
  88. {
  89. return Mathf.Ceil(player.clip.length - player.time);
  90. }
  91. return 0;
  92. }
  93. public void PlayOneShotCroutine(string path, Action action = null)
  94. {
  95. if (coroutine != null)
  96. {
  97. StopCoroutine(coroutine);
  98. }
  99. handle?.Release();
  100. if (_isOn)
  101. {
  102. if (YooAssets.CheckResExist(path))
  103. {
  104. coroutine = StartCoroutine(PlayOneShot(path, action));
  105. }
  106. else
  107. {
  108. action?.Invoke();
  109. }
  110. }
  111. }
  112. private IEnumerator PlayOneShot(string path, Action action = null)
  113. {
  114. //AudioClip clip = GFGAsset.Load<AudioClip>(path);
  115. handle = YooAssets.LoadAssetAsync<AudioClip>(path);
  116. yield return handle;
  117. player.clip = handle.AssetObject as AudioClip;
  118. player.Play();
  119. WaitForSound(action);
  120. }
  121. public void Stop()
  122. {
  123. player.Stop();
  124. }
  125. // 音频播放完成的回调函数
  126. private async Task WaitForSound(Action action = null)
  127. {
  128. int milliseconds = (int)(player.clip.length * 1000);
  129. await Task.Delay(milliseconds);
  130. action?.Invoke();
  131. }
  132. public bool IsPlaying()
  133. {
  134. return player.isPlaying;
  135. }
  136. public void SetVolumn(float volumn)
  137. {
  138. player.volume = volumn;
  139. }
  140. public float GetVolumn()
  141. {
  142. return player.volume;
  143. }
  144. }
  145. }