SoundManager.cs 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. using UnityEngine;
  2. using FairyGUI;
  3. using GFGGame.Launcher;
  4. using YooAsset;
  5. using System.Collections;
  6. namespace GFGGame
  7. {
  8. public class SoundManager : SingletonMonoBase<SoundManager>
  9. {
  10. private AudioSource player;
  11. private AssetOperationHandle handle;
  12. private Coroutine coroutine;
  13. private bool _isOn = true;
  14. public bool isOn
  15. {
  16. get
  17. {
  18. return _isOn;
  19. }
  20. set
  21. {
  22. if(_isOn != value)
  23. {
  24. _isOn = value;
  25. if(_isOn)
  26. {
  27. GRoot.inst.soundVolume = 1;
  28. }
  29. else
  30. {
  31. GRoot.inst.soundVolume = 0;
  32. Stop();
  33. }
  34. LocalCache.SetBool(LauncherConfig.SOUND_KEY, _isOn);
  35. }
  36. }
  37. }
  38. private void Awake()
  39. {
  40. player = this.gameObject.AddComponent<AudioSource>();
  41. isOn = LocalCache.GetBool(LauncherConfig.SOUND_KEY, true);
  42. }
  43. private void Start()
  44. {
  45. }
  46. private void Update()
  47. {
  48. }
  49. public void PlayOneShotCroutine(string path)
  50. {
  51. if (coroutine != null)
  52. {
  53. StopCoroutine(coroutine);
  54. }
  55. handle?.Release();
  56. if (_isOn)
  57. {
  58. coroutine = StartCoroutine(PlayOneShot(path));
  59. }
  60. }
  61. private IEnumerator PlayOneShot(string path)
  62. {
  63. //AudioClip clip = GFGAsset.Load<AudioClip>(path);
  64. handle = YooAssets.LoadAssetAsync<AudioClip>(path);
  65. yield return handle;
  66. player.clip = handle.AssetObject as AudioClip;
  67. player.Play();
  68. }
  69. public void Stop()
  70. {
  71. player.Stop();
  72. }
  73. }
  74. }