NGUI事件的種類很多,比如點擊、雙擊、拖動、滑動等等,他們處理事件的原理幾乎萬全一樣,本文只用按鈕來舉例。
1.直接監聽事件
把下面腳本直接綁定在按鈕上,當按鈕點擊時就可以監聽到,這種方法不太好很不靈活。
1
2
3
4
|
void OnClick()
{
Debug.Log("Button is Click!!!");
}
|
2.使用SendMessage
選擇按鈕后,在Unity導航菜單欄中選擇Component->Interaction->Button Message 組件。
Target:接收按鈕消息的游戲對象。
Function Name:接收按鈕消息的方法,擁有這個方法的腳本必須綁定在上面Target對象身上。
Trigger:觸發的事件,OnClick顯然是一次點擊。
Include Children :是否讓該對象的所有子對象也發送這個點擊事件。
到UIButtonMessage.cs這個腳本中看看,其實很簡單就是調用Unity自身的SendMessage而已。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
void Send ()
{
if (string.IsNullOrEmpty(functionName)) return;
if (target == null) target = gameObject;
if (includeChildren)
{
Transform[] transforms = target.GetComponentsInChildren<Transform>();
for (int i = 0, imax = transforms.Length; i < imax; ++i)
{
Transform t = transforms[i];
t.gameObject.SendMessage(functionName, gameObject, SendMessageOptions.DontRequireReceiver);
}
}
else
{
target.SendMessage(functionName, gameObject, SendMessageOptions.DontRequireReceiver);
}
}
|
3.使用UIListener
這個也是推薦大家使用的一種方法,選擇按鈕后在Unity導航菜單欄中選擇Component->NGUI->Internal ->Event Listener 。 掛在按鈕上就可以,它沒有任何參數。。
在任何一個腳本或者類中即可得到按鈕的點擊事件、把如下代碼放在任意類中或者腳本中。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
void Awake ()
{
//獲取需要監聽的按鈕對象
GameObject button = GameObject.Find("UI Root (2D)/Camera/Anchor/Panel/LoadUI/MainCommon/Button");
//設置這個按鈕的監聽,指向本類的ButtonClick方法中。
UIEventListener.Get(button).onClick = ButtonClick;//同時也支持UIEventListener.Get(button).onClick += ButtonClick
}
//計算按鈕的點擊事件
void ButtonClick(GameObject button)
{
Debug.Log("GameObject " + button.name);
}
|
怎么樣是不是很靈活?再看看它的源碼,使用的C#的代理,將UI的場景事件通過代理傳遞出去了。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
|
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;
}
}
|
但是有時候我們項目中需要監聽UI的東西可能不止這些,我們也可以拓展一下C#的事件方法。或者也可以使用 Unity3D研究院之通過C#使用Advanced CSharp Messenger(五十)
四,這里補充一點,還可以通過在Unity編輯器屬性視窗中制定事件的target和委托的方法來實現消息的傳遞:
如圖:
- 本文固定鏈接: http://www.xuanyusong.com/archives/2390
- 轉載請注明: 雨松MOMO 2013年06月24日 於 雨松MOMO程序研究院 發表