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(要监听的游戏对象).绑定的事件+=方法名;

 

posted @ 2014-11-14 21:58  唐 森  阅读(8647)  评论(0编辑  收藏  举报