using UnityEngine; using UnityEngine.UI; using UnityEngine.SceneManagement; public class UIBtnMusic : MonoBehaviour { public static UIBtnMusic instance; public AudioSource audioSource; // 音效播放器 public AudioSource BGMaudioSource;//背景音效 public AudioClip buttonClickClip; // 按钮点击音效 public AudioClip BGMClickClip_main;//主场景 public AudioClip BGMClickClip_Time;//大逃杀倒计时 public AudioClip ClickClip_fight;//大逃杀击杀倒计时 public AudioClip ClickClip_doorOpen;//开门 public AudioClip ClickClip_attack;//攻击 public AudioClip ClickClip_Paoxiao;//攻击 private void Start() { instance = this; // 保持音效管理器不被销毁 DontDestroyOnLoad(gameObject); // 给场景中所有按钮添加音效 AddButtonSoundEffects(); // 监听场景加载事件 SceneManager.sceneLoaded += OnSceneLoaded; PlayBGMClickSound(BGMClickClip_main);//背景播放 } private void OnSceneLoaded(Scene scene, LoadSceneMode mode) { // 每次切换到新场景时,都为所有按钮添加音效 AddButtonSoundEffects(); } private void AddButtonSoundEffects() { Button[] buttons = FindObjectsOfType