持续の动力
The Growth Of Trees

Unity3D中, NGUI 给button按钮添加单间事件的方法很多,在这里只给推荐一种比较常用的方法。

推荐方法:使用UIListener。

1、给button组价添加上UIListener。选择Button-Component-NGUI-Internal-Event Listener,点击“Event Listener”即可添加到button上。注意,挂在按钮上就可以,它没有任何参数。

如图:

2、调用方法:在任何一个脚本或者类中即可得到按钮的点击事件、把如下代码放在任意类中或者脚本中。

 

using UnityEngine;
using System.Collections;

public class BtnNUGUI : MonoBehaviour {


  void Awake()
  {
    GameObject button = GameObject.Find("UI Root (2D)/Camera/Anchor/Panel/Button");
    UIEventListener.Get(button).onClick = ButtonClick;
  }

  void ButtonClick(GameObject button)
  {
    Debug.Log("NGUI button name :"+button.name);
  }

// Use this for initialization void Start () { } // Update is called once per frame void Update () { } }

 

  

 

posted on 2015-08-01 09:52  持续の动力  阅读(962)  评论(0编辑  收藏  举报