70 lines
1.9 KiB
C#
70 lines
1.9 KiB
C#
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<Button>();
|
|
foreach (Button button in buttons)
|
|
{
|
|
button.onClick.RemoveListener(OnButtonClick); // 确保没有重复添加监听器
|
|
button.onClick.AddListener(OnButtonClick);
|
|
}
|
|
}
|
|
|
|
private void OnButtonClick()
|
|
{
|
|
PlayButtonClickSound();
|
|
}
|
|
|
|
private void PlayButtonClickSound()
|
|
{
|
|
if (audioSource && buttonClickClip)
|
|
{
|
|
audioSource.PlayOneShot(buttonClickClip);
|
|
}
|
|
}
|
|
|
|
public void PlayBGMClickSound(AudioClip audioClip)//背景播放
|
|
{
|
|
audioSource.clip = audioClip;
|
|
audioSource.Play();
|
|
}
|
|
|
|
public void PlayClickSound(AudioClip audioClip)//音效播放
|
|
{
|
|
audioSource.PlayOneShot(audioClip);
|
|
}
|
|
} |