VoiceManager.cs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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. GRoot.inst.soundVolume = 1;
  30. }
  31. else
  32. {
  33. GRoot.inst.soundVolume = 0;
  34. Stop();
  35. }
  36. LocalCache.SetBool(LauncherConfig.SOUND_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.SOUND_KEY, true);
  47. }
  48. public void LoadRes(string path)
  49. {
  50. if (!YooAssets.CheckResExist(path))
  51. {
  52. return;
  53. }
  54. handle = YooAssets.LoadAssetSync<AudioClip>(path);
  55. player.clip = handle.AssetObject as AudioClip;
  56. }
  57. public void PlayVoice()
  58. {
  59. if(player.clip != null)
  60. {
  61. player.Play();
  62. }
  63. }
  64. public void StopVoice()
  65. {
  66. player.Stop();
  67. handle = null;
  68. player.clip = null;
  69. }
  70. public float GetClipLength()
  71. {
  72. if (player.clip != null)
  73. {
  74. return player.clip.length;
  75. }
  76. return 0;
  77. }
  78. public float GetClipRemainingLength()
  79. {
  80. if (player.clip != null)
  81. {
  82. return Mathf.Ceil(player.clip.length - player.time);
  83. }
  84. return 0;
  85. }
  86. public void PlayOneShotCroutine(string path, Action action = null)
  87. {
  88. if (coroutine != null)
  89. {
  90. StopCoroutine(coroutine);
  91. }
  92. handle?.Release();
  93. if (_isOn)
  94. {
  95. if (YooAssets.CheckResExist(path))
  96. {
  97. coroutine = StartCoroutine(PlayOneShot(path, action));
  98. }
  99. else
  100. {
  101. action?.Invoke();
  102. }
  103. }
  104. }
  105. private IEnumerator PlayOneShot(string path, Action action = null)
  106. {
  107. //AudioClip clip = GFGAsset.Load<AudioClip>(path);
  108. handle = YooAssets.LoadAssetAsync<AudioClip>(path);
  109. yield return handle;
  110. player.clip = handle.AssetObject as AudioClip;
  111. player.Play();
  112. WaitForSound(action);
  113. }
  114. public void Stop()
  115. {
  116. player.Stop();
  117. }
  118. // 音频播放完成的回调函数
  119. private async Task WaitForSound(Action action = null)
  120. {
  121. int milliseconds = (int)(player.clip.length * 1000);
  122. await Task.Delay(milliseconds);
  123. action?.Invoke();
  124. }
  125. public bool IsPlaying()
  126. {
  127. return player.isPlaying;
  128. }
  129. }
  130. }