| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159 | using UnityEngine;using FairyGUI;using GFGGame.Launcher;using YooAsset;using System.Collections;using System.Threading.Tasks;using System;namespace GFGGame{    public class VoiceManager : SingletonMonoBase<VoiceManager>    {        private AudioSource player;        private AssetOperationHandle handle;        private Coroutine coroutine;        private bool _isOn = true;        public bool isOn        {            get            {                return _isOn;            }            set            {                if (_isOn != value)                {                    _isOn = value;                    if (_isOn)                    {                        //player.volume = 1;                    }                    else                    {                        //player.volume = 0;                        Stop();                    }                    LocalCache.SetBool(GameConfig.VOICE_KEY, _isOn);                }            }        }        private void Awake()        {            player = this.gameObject.AddComponent<AudioSource>();            player.priority = 256;            //设置这个声音会大一些            player.spatialBlend = 1f;            isOn = LocalCache.GetBool(GameConfig.VOICE_KEY, true);            player.volume = LocalCache.GetFloat(GameConfig.VOICE_VOLUMN_KEY, 1.0f);        }        public void LoadRes(string path)        {            if (player.volume == 0 || !isOn || !YooAssets.CheckResExist(path))            {                return;            }            handle = YooAssets.LoadAssetSync<AudioClip>(path);            player.clip = handle.AssetObject as AudioClip;        }        public void PlayVoice()        {            if (player.clip != null)            {                player.Play();            }        }        public void StopVoice()        {            if(player == null)            {                return;            }            player.Stop();            handle = null;            player.clip = null;        }        public float GetClipLength()        {            if (player.clip != null)            {                return player.clip.length;            }            return 0;        }        public float GetClipRemainingLength()        {            if (player.clip != null)            {                return Mathf.Ceil(player.clip.length - player.time);            }            return 0;        }        public void PlayOneShotCroutine(string path, Action action = null)        {            if (coroutine != null)            {                StopCoroutine(coroutine);            }            handle?.Release();            if (_isOn)            {                if (YooAssets.CheckResExist(path))                {                    coroutine = StartCoroutine(PlayOneShot(path, action));                }                else                {                    action?.Invoke();                }            }        }        private IEnumerator PlayOneShot(string path, Action action = null)        {            //AudioClip clip = GFGAsset.Load<AudioClip>(path);            handle = YooAssets.LoadAssetAsync<AudioClip>(path);            yield return handle;            player.clip = handle.AssetObject as AudioClip;            player.Play();            WaitForSound(action);        }        public void Stop()        {            player.Stop();        }        // 音频播放完成的回调函数         private async Task WaitForSound(Action action = null)        {            int milliseconds = (int)(player.clip.length * 1000);            await Task.Delay(milliseconds);            action?.Invoke();        }        public bool IsPlaying()        {            return player.isPlaying;        }        public void SetVolumn(float volumn)        {            player.volume = volumn;        }        public float GetVolumn()        {            return player.volume;        }    }}
 |