a、直接监听事件
把下面脚本直接绑定在按钮上,当按钮点击时就可以监听到,这种方法不太好很不灵活。
void OnClick()
{
Debug.Log("Button is Click!!!");
}
b、SendMessage
选择Button-Component-NGUI-Interaction-Button Message//[ˌɪntər'ækʃn]n.
c、UIListener(推荐)
选择Button-Component-NGUI-Internal-Event Listener
调用方法:
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 () { } }