VoiceManager.cs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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 AssetHandle 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. //|| !YooAssets.CheckResExist(path
  49. if (player.volume == 0 || !isOn)
  50. return;
  51. var operation = YooAssets.LoadAssetAsync<AudioClip>(path);
  52. operation.Completed += (op) =>
  53. {
  54. // 直接操作Unity对象(假设回调已在主线程)
  55. if (op.Status == EOperationStatus.Succeed)
  56. player.clip = op.AssetObject as AudioClip;
  57. else
  58. Debug.LogError($"加载失败: {path}");
  59. };
  60. }
  61. public void PlayVoice()
  62. {
  63. if (player.clip != null)
  64. {
  65. player.Play();
  66. }
  67. }
  68. public void StopVoice()
  69. {
  70. if (player == null)
  71. {
  72. return;
  73. }
  74. player.Stop();
  75. handle = null;
  76. player.clip = null;
  77. }
  78. public float GetClipLength()
  79. {
  80. if (player.clip != null)
  81. {
  82. return player.clip.length;
  83. }
  84. return 0;
  85. }
  86. public float GetClipRemainingLength()
  87. {
  88. if (player.clip != null)
  89. {
  90. return Mathf.Ceil(player.clip.length - player.time);
  91. }
  92. return 0;
  93. }
  94. public void PlayOneShotCroutine(string path, Action action = null)
  95. {
  96. if (coroutine != null)
  97. {
  98. StopCoroutine(coroutine);
  99. }
  100. handle?.Release();
  101. if (_isOn)
  102. {
  103. if (YooAssetsEx.CheckResExist(path))
  104. {
  105. coroutine = StartCoroutine(PlayOneShot(path, action));
  106. }
  107. else
  108. {
  109. action?.Invoke();
  110. }
  111. }
  112. }
  113. private IEnumerator PlayOneShot(string path, Action action = null)
  114. {
  115. //AudioClip clip = GFGAsset.Load<AudioClip>(path);
  116. handle = YooAssets.LoadAssetAsync<AudioClip>(path);
  117. yield return handle;
  118. player.clip = handle.AssetObject as AudioClip;
  119. player.Play();
  120. WaitForSound(action);
  121. }
  122. public void Stop()
  123. {
  124. player.Stop();
  125. }
  126. // 音频播放完成的回调函数
  127. private async Task WaitForSound(Action action = null)
  128. {
  129. int milliseconds = (int)(player.clip.length * 1000);
  130. await Task.Delay(milliseconds);
  131. action?.Invoke();
  132. }
  133. public bool IsPlaying()
  134. {
  135. return player.isPlaying;
  136. }
  137. public void SetVolumn(float volumn)
  138. {
  139. player.volume = volumn;
  140. }
  141. public float GetVolumn()
  142. {
  143. return player.volume;
  144. }
  145. }
  146. }