VoiceManager.cs 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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 int GetClipLength()
  71. {
  72. if (player.clip != null)
  73. {
  74. return (int)Mathf.Ceil(player.clip.length);
  75. }
  76. return 0;
  77. }
  78. public void PlayOneShotCroutine(string path, Action action = null)
  79. {
  80. if (coroutine != null)
  81. {
  82. StopCoroutine(coroutine);
  83. }
  84. handle?.Release();
  85. if (_isOn)
  86. {
  87. if (YooAssets.CheckResExist(path))
  88. {
  89. coroutine = StartCoroutine(PlayOneShot(path, action));
  90. }
  91. else
  92. {
  93. action?.Invoke();
  94. }
  95. }
  96. }
  97. private IEnumerator PlayOneShot(string path, Action action = null)
  98. {
  99. //AudioClip clip = GFGAsset.Load<AudioClip>(path);
  100. handle = YooAssets.LoadAssetAsync<AudioClip>(path);
  101. yield return handle;
  102. player.clip = handle.AssetObject as AudioClip;
  103. player.Play();
  104. WaitForSound(action);
  105. }
  106. public void Stop()
  107. {
  108. player.Stop();
  109. }
  110. // 音频播放完成的回调函数
  111. private async Task WaitForSound(Action action = null)
  112. {
  113. int milliseconds = (int)(player.clip.length * 1000);
  114. await Task.Delay(milliseconds);
  115. action?.Invoke();
  116. }
  117. public bool IsPlaying()
  118. {
  119. return player.isPlaying;
  120. }
  121. }
  122. }