40 lines
961 B
C#
40 lines
961 B
C#
![]() |
using System.Collections;
|
||
|
using System.Collections.Generic;
|
||
|
using UnityEngine;
|
||
|
using DG.Tweening;
|
||
|
|
||
|
/// <summary>
|
||
|
/// 控制背景音乐的播放和切换
|
||
|
/// </summary>
|
||
|
public class MusicController : UnitySingleton<MusicController>
|
||
|
{
|
||
|
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<AudioSource>();
|
||
|
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);
|
||
|
});
|
||
|
|
||
|
}
|
||
|
|
||
|
}
|