Unity3D NGUI 给button按钮添加单间事件

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 () { } }

  

Ethical behaviour is ... 合乎道德的行为是...... Doing the right thing,even when no one is watching. 在任何时候,即使没有人看见也要做正确的事情。
原文地址:https://www.cnblogs.com/growthTree/p/4693661.html