NGUI的事件綁定可以使用 UIButtonMessage
在一個游戲對象上添加Button Message組件:
在Button Message組件上添加要通知的游戲對象上所掛載的腳本的方法
Target:要通知的掛載腳本的游戲對象
Function Name:調用的方法
使用Button Message不是很靈活,還有一種是使用UIEventListener
在unity菜單上選擇:Component->NGUI->Internal ->Event Listener
然后把你要綁定的方法的腳本拖到Script中
然后在腳本中進行事件綁定
代碼:

void Awake () { //獲取需要監聽的對象 GameObject startGameButton = GameObject.Find("UI Root/startGameButton"); //設置這個對象的監聽事件 UIEventListener.Get(startGameButton).onHover += ButtonHover; UIEventListener.Get(startGameButton).onClick +=PlayTapMusic ; } //計算按鈕的點擊事件 void ButtonHover(GameObject button,bool state) { GameObject swipeSound = GameObject.Find("swipeSound"); swipeSound.audio.Play (); } //播放點擊按鈕的聲音 public void PlayTapMusic(GameObject button){ GameObject tapSound = GameObject.Find("tapSound"); tapSound.audio.Play (); }
其實只是要在要綁定的對象上掛上一個腳本,然后使用UIEventListener的Get方法進行事件綁定
UIEventListener.Get(要監聽的游戲對象).綁定的事件+=方法名;