using System.Collections; using System.Collections.Generic; using UnityEngine; using DG.Tweening; /// /// 控制背景音乐的播放和切换 /// public class MusicController : UnitySingleton { public AudioClip start; public AudioClip game; private AudioClip current; private AudioSource audioSource; private float volume; // Start is called before the first frame update void Start(){ audioSource = GetComponent(); DontDestroyOnLoad(gameObject); volume = audioSource.volume; To(start); } public void To(AudioClip target){ if(current == target){ return; } current = target; //先将音量降至0 audioSource.DOFade(0,0.5f).OnComplete(()=>{ audioSource.clip = target; audioSource.Play(); audioSource.DOFade(volume,0.5f); }); } }