NGUI事件的种类很多,比如点击、双击、拖动、滑动等等,他们处理事件的原理几乎万全一样,本文只用按钮来举例。
方法一.直接监听事件
把下面脚本直接绑定在按钮上,当按钮点击时就可以监听到,这种方法不太好很不灵活。
void OnClick()
{
Debug.Log(“Button is Click!!!”);
}
方法二.使用SendMessage
选择按钮后,在Unity导航菜单栏中选择Component->Interaction->Button Message 组件。
Target:接收按钮消息的游戏对象。
Function Name:接收按钮消息的方法,拥有这个方法的脚本必须绑定在上面Target对象身上。
Trigger:触发的事件,OnClick显然是一次点击。
Include Children :是否让该对象的所有子对象也发送这个点击事件。
到UIButtonMessage.cs这个脚本中看看,其实很简单就是调用Unity自身的SendMessage而已。
void Send ()
{
if (string.IsNullOrEmpty(functionName)) return;
if (target == null) target = gameObject;
if (includeChildren)
{
Transform[] transforms = target.GetComponentsInChildren<Transform>();
for (int i = , imax = transforms.Length; i < imax; ++i)
{
Transform t = transforms[i];
t.gameObject.SendMessage(functionName, gameObject, SendMessageOptions.DontRequireReceiver);
}
}
else
{
target.SendMessage(functionName, gameObject, SendMessageOptions.DontRequireReceiver);
}
}
方法三.使用UIListener
这个也是推荐大家使用的一种方法,选择按钮后在Unity导航菜单栏中选择Component->NGUI->Internal ->Event Listener。挂在按钮上就可以,它没有任何参数。
在任何一个脚本或者类中即可得到按钮的点击事件、把如下代码放在任意类中或者脚本中。
void Awake ()
{
//获取需要监听的按钮对象
GameObject button = GameObject.Find("UI Root (2D)/Camera/Anchor/Panel/LoadUI/MainCommon/Button");
//设置这个按钮的监听,指向本类的ButtonClick方法中。
UIEventListener.Get(button).onClick = ButtonClick;
}
//计算按钮的点击事件
void ButtonClick(GameObject button)
{
Debug.Log("GameObject " + button.name);
}
怎么样是不是很灵活?再看看它的源码,使用的C#的代理,将UI的场景事件通过代理传递出去了。
public class UIEventListener : MonoBehaviour
{
public delegate void VoidDelegate (GameObject go);
public delegate void BoolDelegate (GameObject go, bool state);
public delegate void FloatDelegate (GameObject go, float delta);
public delegate void VectorDelegate (GameObject go, Vector delta);
public delegate void StringDelegate (GameObject go, string text);
public delegate void ObjectDelegate (GameObject go, GameObject draggedObject);
public delegate void KeyCodeDelegate (GameObject go, KeyCode key);
public object parameter;
public VoidDelegate onSubmit;
public VoidDelegate onClick;
public VoidDelegate onDoubleClick;
public BoolDelegate onHover;
public BoolDelegate onPress;
public BoolDelegate onSelect;
public FloatDelegate onScroll;
public VectorDelegate onDrag;
public ObjectDelegate onDrop;
public StringDelegate onInput;
public KeyCodeDelegate onKey;
void OnSubmit () { if (onSubmit != null) onSubmit(gameObject); }
void OnClick () { if (onClick != null) onClick(gameObject); }
void OnDoubleClick () { if (onDoubleClick != null) onDoubleClick(gameObject); }
void OnHover (bool isOver) { if (onHover != null) onHover(gameObject, isOver); }
void OnPress (bool isPressed) { if (onPress != null) onPress(gameObject, isPressed); }
void OnSelect (bool selected) { if (onSelect != null) onSelect(gameObject, selected); }
void OnScroll (float delta) { if (onScroll != null) onScroll(gameObject, delta); }
void OnDrag (Vector delta) { if (onDrag != null) onDrag(gameObject, delta); }
void OnDrop (GameObject go) { if (onDrop != null) onDrop(gameObject, go); }
void OnInput (string text) { if (onInput != null) onInput(gameObject, text); }
void OnKey (KeyCode key) { if (onKey != null) onKey(gameObject, key); }
/// <summary>
/// Get or add an event listener to the specified game object.
/// </summary>
static public UIEventListener Get (GameObject go)
{
UIEventListener listener = go.GetComponent<UIEventListener>();
if (listener == null) listener = go.AddComponent<UIEventListener>();
return listener;
}
}