SoundManager.cs 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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 float _lastVolumn = 1;
  14. private bool _isOn = true;
  15. public bool isOn
  16. {
  17. get
  18. {
  19. return _isOn;
  20. }
  21. set
  22. {
  23. if (_isOn != value)
  24. {
  25. _isOn = value;
  26. if (_isOn)
  27. {
  28. GRoot.inst.soundVolume = _lastVolumn;
  29. }
  30. else
  31. {
  32. _lastVolumn = GRoot.inst.soundVolume;
  33. GRoot.inst.soundVolume = 0;
  34. Stop();
  35. }
  36. LocalCache.SetBool(GameConfig.SOUND_KEY, _isOn);
  37. }
  38. }
  39. }
  40. private void Awake()
  41. {
  42. player = this.gameObject.AddComponent<AudioSource>();
  43. isOn = LocalCache.GetBool(GameConfig.SOUND_KEY, true);
  44. player.volume = LocalCache.GetFloat(GameConfig.SOUND_VOLUMN_KEY, 1.0f);
  45. }
  46. private void Start()
  47. {
  48. }
  49. private void Update()
  50. {
  51. }
  52. public void PlayOneShotCroutine(string path)
  53. {
  54. if (coroutine != null)
  55. {
  56. StopCoroutine(coroutine);
  57. }
  58. handle?.Release();
  59. if (_isOn)
  60. {
  61. coroutine = StartCoroutine(PlayOneShot(path));
  62. }
  63. }
  64. private IEnumerator PlayOneShot(string path)
  65. {
  66. //AudioClip clip = GFGAsset.Load<AudioClip>(path);
  67. handle = YooAssets.LoadAssetAsync<AudioClip>(path);
  68. yield return handle;
  69. player.clip = handle.AssetObject as AudioClip;
  70. player.Play();
  71. }
  72. public void Stop()
  73. {
  74. player.Stop();
  75. }
  76. public void SetVolumn(float volumn)
  77. {
  78. GRoot.inst.soundVolume = volumn;
  79. }
  80. }
  81. }