NGUI的事件绑定可以使用 UIButtonMessage

在一个游戏对象上添加Button Message组件:

NGUI事件监听之UIEventListener的使用

在Button Message组件上添加要通知的游戏对象上所挂载的脚本的方法

Target:要通知的挂载脚本的游戏对象

Function Name:调用的方法

 

NGUI事件监听之UIEventListener的使用

 

使用Button Message不是很灵活,还有一种是使用UIEventListener

在unity菜单上选择:Component->NGUI->Internal ->Event Listener

NGUI事件监听之UIEventListener的使用

 然后把你要绑定的方法的脚本拖到Script中

NGUI事件监听之UIEventListener的使用

NGUI事件监听之UIEventListener的使用

然后在脚本中进行事件绑定

NGUI事件监听之UIEventListener的使用

 代码:

    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

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2021-09-13
  • 2022-12-23
  • 2021-06-19
  • 2021-10-25
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2022-01-01
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案