_TheStrongestSnail/TheStrongestSnail/Assets/Scripts/UIBtnMusic.cs

70 lines
1.9 KiB
C#
Raw Normal View History

2024-12-09 16:54:32 +08:00
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;
public class UIBtnMusic : MonoBehaviour
{
public static UIBtnMusic instance;
public AudioSource audioSource; // <20><>Ч<EFBFBD><D0A7><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
public AudioSource BGMaudioSource;//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ч
public AudioClip buttonClickClip; // <20><>ť<EFBFBD><C5A5><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ч
public AudioClip BGMClickClip_main;//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
public AudioClip BGMClickClip_Time;//<2F><><EFBFBD><EFBFBD>ɱ<EFBFBD><C9B1><EFBFBD><EFBFBD>ʱ
public AudioClip ClickClip_fight;//<2F><><EFBFBD><EFBFBD>ɱ<EFBFBD><C9B1>ɱ<EFBFBD><C9B1><EFBFBD><EFBFBD>ʱ
public AudioClip ClickClip_doorOpen;//<2F><><EFBFBD><EFBFBD>
public AudioClip ClickClip_attack;//<2F><><EFBFBD><EFBFBD>
public AudioClip ClickClip_Paoxiao;//<2F><><EFBFBD><EFBFBD>
private void Start()
{
instance = this;
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ч<EFBFBD><D0A7><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
DontDestroyOnLoad(gameObject);
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>а<EFBFBD>ť<EFBFBD><C5A5><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ч
AddButtonSoundEffects();
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>¼<EFBFBD>
SceneManager.sceneLoaded += OnSceneLoaded;
PlayBGMClickSound(BGMClickClip_main);//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
}
private void OnSceneLoaded(Scene scene, LoadSceneMode mode)
{
// ÿ<><C3BF><EFBFBD>л<EFBFBD><D0BB><EFBFBD><EFBFBD>³<EFBFBD><C2B3><EFBFBD>ʱ<EFBFBD><CAB1><EFBFBD><EFBFBD>Ϊ<EFBFBD><CEAA><EFBFBD>а<EFBFBD>ť<EFBFBD><C5A5><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ч
AddButtonSoundEffects();
}
private void AddButtonSoundEffects()
{
Button[] buttons = FindObjectsOfType<Button>();
foreach (Button button in buttons)
{
button.onClick.RemoveListener(OnButtonClick); // ȷ<><C8B7>û<EFBFBD><C3BB><EFBFBD>ظ<EFBFBD><D8B8><EFBFBD><EFBFBD>Ӽ<EFBFBD><D3BC><EFBFBD><EFBFBD><EFBFBD>
button.onClick.AddListener(OnButtonClick);
}
}
private void OnButtonClick()
{
PlayButtonClickSound();
}
private void PlayButtonClickSound()
{
if (audioSource && buttonClickClip)
{
audioSource.PlayOneShot(buttonClickClip);
}
}
public void PlayBGMClickSound(AudioClip audioClip)//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
{
audioSource.clip = audioClip;
audioSource.Play();
}
public void PlayClickSound(AudioClip audioClip)//<2F><>Ч<EFBFBD><D0A7><EFBFBD><EFBFBD>
{
audioSource.PlayOneShot(audioClip);
}
}