标签:style blog http color 使用 os io strong
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;     }         //计算按钮的点击事件     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和委托的方法来实现消息的传递:
如图:

(转)NGUI研究院之三种方式监听NGUI的事件方法,布布扣,bubuko.com
标签:style blog http color 使用 os io strong
原文地址:http://www.cnblogs.com/wonderKK/p/3901781.html