SoundManager.cs 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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. }
  42. private void Start()
  43. {
  44. }
  45. private void Update()
  46. {
  47. }
  48. public void PlayOneShotCroutine(string path)
  49. {
  50. if (coroutine != null)
  51. {
  52. StopCoroutine(coroutine);
  53. }
  54. handle?.Release();
  55. if (_isOn)
  56. {
  57. coroutine = StartCoroutine(PlayOneShot(path));
  58. }
  59. }
  60. private IEnumerator PlayOneShot(string path)
  61. {
  62. //AudioClip clip = GFGAsset.Load<AudioClip>(path);
  63. handle = YooAssets.LoadAssetAsync<AudioClip>(path);
  64. yield return handle;
  65. player.clip = handle.AssetObject as AudioClip;
  66. player.Play();
  67. }
  68. public void Stop()
  69. {
  70. player.Stop();
  71. }
  72. }
  73. }