| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148 | 
							- using System.Collections;
 
- using UnityEngine;
 
- using GFGGame.Launcher;
 
- using YooAsset;
 
- namespace GFGGame
 
- {
 
-     public class MusicManager : SingletonMonoBase<MusicManager>
 
-     {
 
-         private AudioSource player;
 
-         private string currentName;
 
-         private AssetOperationHandle handle;
 
-         private Coroutine coroutine;
 
-         private float settingVolumn = 1f;       // 设置音量
 
-         private float tempVolume = 1f;          // 临时音量
 
-         private float changeBaseValue = 0.01f;  // 缓动单位改变基数
 
-         private bool _isOn = true;
 
-         public bool isOn
 
-         {
 
-             get
 
-             {
 
-                 return _isOn;
 
-             }
 
-             set
 
-             {
 
-                 if (_isOn != value)
 
-                 {
 
-                     _isOn = value;
 
-                     if (_isOn)
 
-                     {
 
-                         if (currentName != null)
 
-                         {
 
-                             PlayCroutine(currentName, true);
 
-                         }
 
-                     }
 
-                     else
 
-                     {
 
-                         Stop();
 
-                     }
 
-                     LocalCache.SetBool(GameConfig.MUSIC_KEY, _isOn);
 
-                 }
 
-             }
 
-         }
 
-         private void Awake()
 
-         {
 
-             player = this.gameObject.AddComponent<AudioSource>();
 
-             player.loop = true;
 
-             isOn = LocalCache.GetBool(GameConfig.MUSIC_KEY, true);
 
-             settingVolumn = LocalCache.GetFloat(GameConfig.MUSIC_VOLUMN_KEY, 1.0f);
 
-             player.volume = settingVolumn;
 
-         }
 
-         private void Update()
 
-         {
 
-             if (player.clip == null || !isOn)
 
-             {
 
-                 return;
 
-             }
 
-             // 播放语音时背景音乐变小
 
-             if (VoiceManager.Instance.IsPlaying())
 
-             {
 
-                 tempVolume = 0.17f;
 
-             }
 
-             else
 
-             {
 
-                 tempVolume = settingVolumn;
 
-             }
 
-             // 音量缓动变化
 
-             if (player.volume != tempVolume)
 
-             {
 
-                 player.volume = Mathf.Lerp(player.volume, tempVolume, changeBaseValue);
 
-                 if (Mathf.Abs(player.volume - tempVolume) <= 0.05)
 
-                 {
 
-                     player.volume = tempVolume;
 
-                 }
 
-             }
 
-         }
 
-         public void PlayCroutine(string path, bool must = false, float volume = 1.0f)
 
-         {
 
-             if (currentName != path || must)
 
-             {
 
-                 currentName = path;
 
-                 if (coroutine != null)
 
-                 {
 
-                     StopCoroutine(coroutine);
 
-                 }
 
-                 handle?.Release();
 
-                 if (_isOn)
 
-                 {
 
-                     coroutine = StartCoroutine(Play(path, volume));
 
-                 }
 
-             }
 
-         }
 
-         private IEnumerator Play(string path, float volume = 1.0f)
 
-         {
 
-             //AudioClip clip = GFGAsset.Load<AudioClip>(path);
 
-             handle = YooAssets.LoadAssetAsync<AudioClip>(path);
 
-             yield return handle;
 
-             player.clip = handle.AssetObject as AudioClip;
 
-             player.volume = volume;
 
-             player.Play();
 
-         }
 
-         public float GetSoundTime()
 
-         {
 
-             if (player.clip != null)
 
-             {
 
-                 return player.clip.length;
 
-             }
 
-             return 0;
 
-         }
 
-         public void Pause()
 
-         {
 
-             player.Pause();
 
-         }
 
-         public void UnPause()
 
-         {
 
-             player.UnPause();
 
-         }
 
-         public void Stop()
 
-         {
 
-             handle?.Release();
 
-             player?.Stop();
 
-         }
 
-         public void SetVolume(float volume)
 
-         {
 
-             player.volume = volume;
 
-         }
 
-         public void SetSettingVolumn(float volume)
 
-         {
 
-             settingVolumn = volume;
 
-         }
 
-         public float GetSettingVolumn()
 
-         {
 
-             return settingVolumn;
 
-         }
 
-     }
 
- }
 
 
  |