監聽NGUI事件

轉載地址:http://unity3d.9ria.com/?p=3719&replytocom=411368

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;
         }
     }

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章