using UnityEngine; using FairyGUI; using GFGGame.Launcher; using YooAsset; using System.Collections; namespace GFGGame { public class SoundManager : SingletonMonoBase { private AudioSource player; private AssetOperationHandle handle; private Coroutine coroutine; private float _lastVolumn = 1; private bool _isOn = true; public bool isOn { get { return _isOn; } set { if(_isOn != value) { _isOn = value; if(_isOn) { GRoot.inst.soundVolume = _lastVolumn; } else { _lastVolumn = GRoot.inst.soundVolume; GRoot.inst.soundVolume = 0; Stop(); } LocalCache.SetBool(LauncherConfig.SOUND_KEY, _isOn); } } } private void Awake() { player = this.gameObject.AddComponent(); isOn = LocalCache.GetBool(LauncherConfig.SOUND_KEY, true); player.volume = LocalCache.GetFloat(LauncherConfig.SOUND_VOLUMN_KEY, 1); } private void Start() { } private void Update() { } public void PlayOneShotCroutine(string path) { if (coroutine != null) { StopCoroutine(coroutine); } handle?.Release(); if (_isOn) { coroutine = StartCoroutine(PlayOneShot(path)); } } private IEnumerator PlayOneShot(string path) { //AudioClip clip = GFGAsset.Load(path); handle = YooAssets.LoadAssetAsync(path); yield return handle; player.clip = handle.AssetObject as AudioClip; player.Play(); } public void Stop() { player.Stop(); } public void SetVolumn(float volumn) { GRoot.inst.soundVolume = volumn; } } }