VoiceManager.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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(LauncherConfig.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(LauncherConfig.VOICE_KEY, true);
  47. player.volume = LocalCache.GetFloat(LauncherConfig.VOICE_VOLUMN_KEY, 1);
  48. }
  49. public void LoadRes(string path)
  50. {
  51. if (!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. player.Stop();
  68. handle = null;
  69. player.clip = null;
  70. }
  71. public float GetClipLength()
  72. {
  73. if (player.clip != null)
  74. {
  75. return player.clip.length;
  76. }
  77. return 0;
  78. }
  79. public float GetClipRemainingLength()
  80. {
  81. if (player.clip != null)
  82. {
  83. return Mathf.Ceil(player.clip.length - player.time);
  84. }
  85. return 0;
  86. }
  87. public void PlayOneShotCroutine(string path, Action action = null)
  88. {
  89. if (coroutine != null)
  90. {
  91. StopCoroutine(coroutine);
  92. }
  93. handle?.Release();
  94. if (_isOn)
  95. {
  96. if (YooAssets.CheckResExist(path))
  97. {
  98. coroutine = StartCoroutine(PlayOneShot(path, action));
  99. }
  100. else
  101. {
  102. action?.Invoke();
  103. }
  104. }
  105. }
  106. private IEnumerator PlayOneShot(string path, Action action = null)
  107. {
  108. //AudioClip clip = GFGAsset.Load<AudioClip>(path);
  109. handle = YooAssets.LoadAssetAsync<AudioClip>(path);
  110. yield return handle;
  111. player.clip = handle.AssetObject as AudioClip;
  112. player.Play();
  113. WaitForSound(action);
  114. }
  115. public void Stop()
  116. {
  117. player.Stop();
  118. }
  119. // 音频播放完成的回调函数
  120. private async Task WaitForSound(Action action = null)
  121. {
  122. int milliseconds = (int)(player.clip.length * 1000);
  123. await Task.Delay(milliseconds);
  124. action?.Invoke();
  125. }
  126. public bool IsPlaying()
  127. {
  128. return player.isPlaying;
  129. }
  130. public void SetVolumn(float volumn)
  131. {
  132. player.volume = volumn;
  133. }
  134. public float GetVolumn()
  135. {
  136. return player.volume;
  137. }
  138. }
  139. }