全部對於Unity3D中 NGUI 觸發事件的監聽方法


NGUI事件的種類非常多。比方點擊、雙擊、拖動、滑動等等,他們處理事件的原理差點兒萬全一樣,本文僅僅用button來舉例。

方法一.直接監聽事件

把以下腳本直接綁定在button上。當button點擊時就能夠監聽到。這樣的方法不太好非常不靈活。

void OnClick()

{

Debug.Log(“Button is Click!!!”);

}

方法二.使用SendMessage

選擇button后。在Unity導航菜單條中選擇Component->Interaction->Button Message 組件。

Target:接收button消息的游戲對象。

Function Name:接收button消息的方法,擁有這種方法的腳本必須綁定在上面Target對象身上。

Trigger:觸發的事件,OnClick顯然是一次點擊。

Include Children :是否讓該對象的全部子對象也發送這個點擊事件。

到UIButtonMessage.cs這個腳本中看看,事實上非常easy就是調用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

這個也是推薦大家使用的一種方法,選擇button后在Unity導航菜單條中選擇Component->NGUI->Internal ->Event Listener。掛在button上就能夠,它沒有不論什么參數。

在不論什么一個腳本或者類中就可以得到button的點擊事件、把例如以下代碼放在隨意類中或者腳本中。

void Awake ()
    {  
                 //獲取須要監聽的button對象
         GameObject button = GameObject.Find("UI Root (2D)/Camera/Anchor/Panel/LoadUI/MainCommon/Button");
                 //設置這個button的監聽,指向本類的ButtonClick方法中。
        UIEventListener.Get(button).onClick = ButtonClick;
    }

         //計算button的點擊事件
     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;
         }
     }



免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM