using System.Collections; using UnityEngine; /* Home场景全局控制音效播放 * 本代码挂在在Home场景的Player上 */ public class HomeSoundEffectController: MonoBehaviour { public static HomeSoundEffectController Instance; //音效相关 public AudioSource audioSource; public AudioClip[] soundEffect; void Awake() { // 单例模式,确保只有一个GlobalSoundManager if (Instance == null) { Instance = this; //DontDestroyOnLoad(gameObject); // 必须关掉否则会导致原场景destroy不能执行 } else { Destroy(gameObject); } } public void PlaySoundEffect(int id) { audioSource.PlayOneShot(soundEffect[id]); } }