91 lines
3.1 KiB
C#
91 lines
3.1 KiB
C#
///
|
||
//规则是这样的 可以在每个窗口UI下添加该脚本 音效ID在 Sound.txt表格中配置
|
||
// UI生效时会 遍历窗口所有的子控件,寻找Button等脚本 并添加Button等点击事件 播放音效
|
||
// 窗口所有的Button点击播放的音效一样,如果有些button小播放其它的音效,就需要在该button下添加该脚本。
|
||
// UGUI中有些是继承的 IPointerClickHandler事件,它的点击也需要添加音效的话,就需要在该脚本的问题上也添加一个
|
||
// ButtonPlaySound脚本 。在IPointerClickHandler事件的触发函数中寻找 ButtonPlaySound脚本并调用 PlaySound函数
|
||
//
|
||
///
|
||
using UnityEngine;
|
||
using UnityEngine.UI;
|
||
using System.Collections;
|
||
using Games.GlobeDefine;
|
||
|
||
public class ButtonPlaySound : MonoBehaviour {
|
||
|
||
|
||
public int SoundSourceId = GlobeVar.defaultButtonSound; //Button的音效也配置在表格中吧 表格中默认的是音效 id=7
|
||
public bool IsForBidSound = false; //是否禁止音效 true禁止 false不禁止
|
||
|
||
|
||
void Start () {
|
||
|
||
//先添加这两种吧,看后面有没有需要增加的
|
||
SetButtonSound();
|
||
SetToggleSound();
|
||
}
|
||
|
||
private void SetToggleSound()
|
||
{
|
||
Toggle[] toggles = GetComponentsInChildren<Toggle>();
|
||
for (int i = 0; i < toggles.Length; i++)
|
||
{
|
||
Toggle toggle = toggles[i];
|
||
ButtonPlaySound ChildSound = toggle.gameObject.GetComponent<ButtonPlaySound>();
|
||
if (ChildSound != null)
|
||
{
|
||
if (ChildSound.SoundSourceId > 0)
|
||
continue;
|
||
if (IsForBidSound)
|
||
continue;
|
||
}
|
||
|
||
for (int j = 0; j < toggle.onValueChanged.GetPersistentEventCount(); j++)
|
||
{
|
||
toggle.onValueChanged.SetPersistentListenerState(j, UnityEngine.Events.UnityEventCallState.RuntimeOnly);
|
||
}
|
||
|
||
toggle.onValueChanged.AddListener(delegate (bool isOn)
|
||
{
|
||
if (isOn)
|
||
PlaySound();
|
||
});
|
||
}
|
||
}
|
||
|
||
private void SetButtonSound()
|
||
{
|
||
Button[] btns = GetComponentsInChildren<Button>();
|
||
for (int i = 0; i < btns.Length; i++)
|
||
{
|
||
Button btnChild = btns[i];
|
||
ButtonPlaySound ChildSound = btnChild.gameObject.GetComponent<ButtonPlaySound>();
|
||
if (ChildSound != null)
|
||
{
|
||
if (ChildSound.SoundSourceId > 0)
|
||
continue;
|
||
if (IsForBidSound)
|
||
continue;
|
||
}
|
||
|
||
for (int j = 0; j < btnChild.onClick.GetPersistentEventCount(); j++)
|
||
{
|
||
btnChild.onClick.SetPersistentListenerState(j, UnityEngine.Events.UnityEventCallState.RuntimeOnly);
|
||
}
|
||
|
||
btnChild.onClick.AddListener(PlaySound);
|
||
}
|
||
}
|
||
|
||
public void PlaySound()
|
||
{
|
||
if (IsForBidSound)
|
||
return;
|
||
|
||
if(SoundSourceId>=0 && GameManager.gameManager!=null && GameManager.gameManager.SoundManager!=null)
|
||
{
|
||
GameManager.gameManager.SoundManager.PlaySoundEffect(SoundSourceId);
|
||
}
|
||
}
|
||
}
|