NGUI事件監聽之UIEventListener的使用


  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 ();
    }
View Code

 其實只是要在要綁定的對象上掛上一個腳本,然后使用UIEventListener的Get方法進行事件綁定

UIEventListener.Get(要監聽的游戲對象).綁定的事件+=方法名;

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM