NGUI事件的種類很多,比如點擊、雙擊、拖動、滑動等等,他們處理事件的原理幾乎萬全一樣,本文只用按鈕來舉例。
1.直接監聽事件
把下面腳本直接綁定在按鈕上,點擊按鈕觸發的方法名必須為OnClick,當按鈕點擊時就可以監聽到,這種方法不太好很不靈活。
private void OnClick() { Debug.Log("我是按鈕1" + gameObject.name); }
2.使用SendMessage
選擇按鈕后,在Unity導航菜單欄中選擇Component->Interaction->Button Message 組件。
Target:接收按鈕消息的游戲對象。
Function Name:接收按鈕消息的方法,擁有這個方法的腳本必須綁定在上面Target對象身上。
Trigger:觸發的事件,OnClick顯然是一次點擊。
Include Children :是否讓該對象的所有子對象也發送這個點擊事件。
private void OnButton2Click() { Debug.Log("我是按鈕2被點擊了"); }
這里我將腳本掛在了主攝像機上,然后給按鈕2添加UIButton Message(Script)組件,然后Target:Main Camera,Function Name:OnButton2Click。
3.使用UIListener
這個也是推薦大家使用的一種方法,選擇按鈕后在Unity導航菜單欄中選擇Component->NGUI->Internal ->Event Listener 。 掛在按鈕上就可以,它沒有任何參數。
void Awake () { //獲取需要監聽的按鈕對象 GameObject button = GameObject.Find("UI Root/Button3"); //設置這個按鈕的監聽,指向本類的ButtonClick方法中。 UIEventListener.Get(button).onClick = OnButton3Click; } private void OnButton3Click(GameObject button) { Debug.Log("我是按鈕3被點擊了"); }
該方式采用了委托來調用觸發事件,直接將腳本掛在按鈕3上。
怎么樣是不是很靈活?再看看它的源碼,使用的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, Vector2 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 (Vector2 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; } }
整個場景圖如下