1. 程式人生 > >[UnityUI]UGUI的事件系統

[UnityUI]UGUI的事件系統

官方文件連結:http://docs.unity3d.com/460/Documentation/Manual/SupportedEvents.html

Supported Events
The Eventsystem supports a number of events, and they can be customised further in user custom user written InputModules.
The events that are supported by the StandaloneInputModule and TouchInputModule are provided by interface and can be implemented on a MonoBehaviour by implementing the interface. If you have a valid EventSystem configured the events will be called at the correct time.


IPointerEnterHandler - OnPointerEnter - Called when a pointer enters the object 
指標進入
public void OnPointerEnter(PointerEventData eventData);


IPointerExitHandler - OnPointerExit - Called when a pointer exits the object
指標退出
public void OnPointerExit(PointerEventData eventData);


IPointerDownHandler - OnPointerDown - Called when a pointer is pressed on the object 
指標按下
public void OnPointerDown(PointerEventData eventData);


IPointerUpHandler - OnPointerUp - Called when a pointer is released (called on the original the pressed object)
指標釋放(可能按下時的指標位置跟釋放時的指標位置不同,這裡指的是按下時指標指著的物體)
public void OnPointerUp(PointerEventData eventData);


IPointerClickHandler - OnPointerClick - Called when a pointer is pressed and released on the same object
在同一物體上按下並釋放
public void OnPointerClick(PointerEventData eventData);


IInitializePotentialDragHandler - OnInitializePotentialDrag - Called when a drag target is found, can be used to initialise values
拖拽時的初始化,跟IPointerDownHandler差不多,在按下時呼叫 
public void OnInitializePotentialDrag(PointerEventData eventData);


IBeginDragHandler - OnBeginDrag - Called on the drag object when dragging is about to begin
開始拖拽
public void OnBeginDrag(PointerEventData eventData);


IDragHandler - OnDrag - Called on the drag object when a drag is happening
拖拽中
public void OnDrag(PointerEventData eventData);


IEndDragHandler - OnEndDrag - Called on the drag object when a drag finishes
拖拽結束(被拖拽的物體呼叫)
public void OnEndDrag(PointerEventData eventData);


IDropHandler - OnDrop - Called on the object where a drag finishes
拖拽結束(拖拽結束後的位置(即滑鼠位置)如果有物體,則那個物體呼叫)
public void OnDrop(PointerEventData eventData);


IScrollHandler - OnScroll - Called when a mouse wheel scrolls
滾輪滾動
public void OnScroll(PointerEventData eventData);


IUpdateSelectedHandler - OnUpdateSelected - Called on the selected object each tick
被選中的物體每幀呼叫
public void OnUpdateSelected(BaseEventData eventData);


ISelectHandler - OnSelect - Called when the object becomes the selected object
物體被選中時(EventSystem.current.SetSelectedGameObject(gameObject))
public void OnSelect(BaseEventData eventData);


IDeselectHandler - OnDeselect - Called on the selected object becomes deselected
物體從選中到取消選中時
public void OnDeselect(BaseEventData eventData);


IMoveHandler - OnMove - Called when a move event occurs (left, right, up, down, ect)
物體移動時(與InputManager裡的Horizontal和Vertica按鍵相對應),前提條件是物體被選中
public void OnMove(AxisEventData eventData);


ISubmitHandler - OnSubmit - Called when the submit button is pressed
提交按鈕被按下時(與InputManager裡的Submit按鍵相對應,PC上預設的是Enter鍵),前提條件是物體被選中
public void OnSubmit(BaseEventData eventData);


ICancelHandler - OnCancel - Called when the cancel button is pressed
取消按鈕被按下時(與InputManager裡的Cancel按鍵相對應,PC上預設的是Esc鍵),前提條件是物體被選中
public void OnCancel(BaseEventData eventData);

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

總結:

1.共有17個介面方法

2.其中OnMove使用的引數是AxisEventData eventData

3.其中OnUpdateSelected、OnSelect 、OnDeselect 、OnSubmit 、OnCancel 使用的引數是BaseEventData eventData

4.其餘使用的引數是PointerEventData eventData

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

實現事件的兩種方式:

1.編輯器:新增Event Trigger元件

2.程式碼:繼承EventTrigger,實現方法(EventTrigger繼承了上面所說的介面)

using UnityEngine;
using System.Collections;
using UnityEngine.EventSystems;

public class TestUI : EventTrigger {

    public override void OnPointerEnter(PointerEventData eventData)
    {
        print("OnPointerEnter");
    }

    public override void OnDrag(PointerEventData eventData)
    {
        print("OnDrag");
    }
}

using UnityEngine;
using System.Collections;
using UnityEngine.UI;

/// <summary>
/// Button的監聽
/// </summary>
public class TestUI2 : MonoBehaviour {

	void Start () 
	{
        Button button = GetComponent<Button>();
        button.onClick.AddListener(TestClick);
        button.onClick.AddListener
        (
            delegate()
            {
                TestClick2(gameObject);
            }
        );
	}
	
    //呼叫不帶引數的方法
	public void TestClick()
    {
        Debug.Log("Click");
    }

    //呼叫帶引數的方法
    public void TestClick2(GameObject go)
    {
        Debug.Log(gameObject.name);
        Debug.Log("Click2");
    }
}

顯然如果每次都繼承EventTrigger,實現方法是很不科學的,因為這樣會降低編寫效率,因此要把它封裝起來。
using UnityEngine;
using System.Collections;
using UnityEngine.EventSystems;

public class EventTriggerListener : EventTrigger
{
    #region 變數
    //帶引數是為了方便取得綁定了UI事件的物件
    public delegate void UIDelegate(GameObject go);
    public event UIDelegate onPointerEnter;
    public event UIDelegate onPointerExit;
    public event UIDelegate onPointerDown;
    public event UIDelegate onPointerUp;
    public event UIDelegate onPointerClick;
    public event UIDelegate onInitializePotentialDrag;
    public event UIDelegate onBeginDrag;
    public event UIDelegate onDrag;
    public event UIDelegate onEndDrag;
    public event UIDelegate onDrop;
    public event UIDelegate onScroll;
    public event UIDelegate onUpdateSelected;
    public event UIDelegate onSelect;
    public event UIDelegate onDeselect;
    public event UIDelegate onMove;
    public event UIDelegate onSubmit;
    public event UIDelegate onCancel;
    #endregion

    public static EventTriggerListener GetListener(GameObject go)
    {
        EventTriggerListener listener = go.GetComponent<EventTriggerListener>();
        if(listener == null) listener = go.AddComponent<EventTriggerListener>();
        return listener;
    }

    #region 方法
    public override void OnPointerEnter(PointerEventData eventData)
    {
        if (onPointerEnter != null) onPointerEnter(gameObject);
    }
    public override void OnPointerExit(PointerEventData eventData)
    {
        if (onPointerExit != null) onPointerExit(gameObject);
    }
    public override void OnPointerDown(PointerEventData eventData)
    {
        if (onPointerDown != null) onPointerDown(gameObject);
    }
    public override void OnPointerUp(PointerEventData eventData)
    {
        if (onPointerUp != null) onPointerUp(gameObject);
    }
    public override void OnPointerClick(PointerEventData eventData)
    {
        if (onPointerClick != null) onPointerClick(gameObject);
    }
    public override void OnInitializePotentialDrag(PointerEventData eventData)
    {
        if (onInitializePotentialDrag != null) onInitializePotentialDrag(gameObject);
    }
    public override void OnBeginDrag(PointerEventData eventData)
    {
        if (onBeginDrag != null) onBeginDrag(gameObject);
    }
    public override void OnDrag(PointerEventData eventData)
    {
        if (onDrag != null) onDrag(gameObject);
    }
    public override void OnEndDrag(PointerEventData eventData)
    {
        if (onEndDrag != null) onEndDrag(gameObject);
    }
    public override void OnDrop(PointerEventData eventData)
    {
        if (onDrop != null) onDrop(gameObject);
    }
    public override void OnScroll(PointerEventData eventData)
    {
        if (onScroll != null) onScroll(gameObject);
    }
    public override void OnUpdateSelected(BaseEventData eventData)
    {
        if (onUpdateSelected != null) onUpdateSelected(gameObject);
    }
    public override void OnSelect(BaseEventData eventData)
    {
        if (onSelect != null) onSelect(gameObject);
    }
    public override void OnDeselect(BaseEventData eventData)
    {
        if (onDeselect != null) onDeselect(gameObject);
    }
    public override void OnMove(AxisEventData eventData)
    {
        if (onMove != null) onMove(gameObject);
    }
    public override void OnSubmit(BaseEventData eventData)
    {
        if (onSubmit != null) onSubmit(gameObject);
    }
    public override void OnCancel(BaseEventData eventData)
    {
        if (onCancel != null) onCancel(gameObject);
    }
    #endregion
}

using UnityEngine;
using System.Collections;
using UnityEngine.EventSystems;

public class TestUI3 : MonoBehaviour {

    private GameObject button;
    private GameObject image;

	void Start () 
	{
        button = GameObject.Find("Button");
        image = GameObject.Find("Image");
        EventTriggerListener.GetListener(button).onPointerClick += Click;
        EventTriggerListener.GetListener(image).onDrag += Drag;
	}

    public void Click(GameObject go)
    {
        Debug.Log(go.name);
    }

    public void Drag(GameObject go)
    {
        Debug.Log(go.name);
    }
}

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

上面的EventTriggerListener.cs有些不足的地方,就是PointerEventData等資料沒有利用到,因此這裡修改一下:

using UnityEngine;
using System.Collections;
using UnityEngine.EventSystems;

public class EventTriggerListener : EventTrigger {

    #region 變數
    //帶引數是為了方便取得綁定了UI事件的物件  
    public delegate void PointerEventDelegate(GameObject go, PointerEventData eventData);
    public delegate void BaseEventDelegate(GameObject go, BaseEventData eventData);
    public delegate void AxisEventDelegate(GameObject go, AxisEventData eventData);

    public event PointerEventDelegate onPointerEnter;
    public event PointerEventDelegate onPointerExit;
    public event PointerEventDelegate onPointerDown;
    public event PointerEventDelegate onPointerUp;
    public event PointerEventDelegate onPointerClick;
    public event PointerEventDelegate onInitializePotentialDrag;
    public event PointerEventDelegate onBeginDrag;
    public event PointerEventDelegate onDrag;
    public event PointerEventDelegate onEndDrag;
    public event PointerEventDelegate onDrop;
    public event PointerEventDelegate onScroll;
    public event BaseEventDelegate onUpdateSelected;
    public event BaseEventDelegate onSelect;
    public event BaseEventDelegate onDeselect;
    public event AxisEventDelegate onMove;
    public event BaseEventDelegate onSubmit;
    public event BaseEventDelegate onCancel;
    #endregion

    public static EventTriggerListener GetListener(GameObject go)
    {
        EventTriggerListener listener = go.GetComponent<EventTriggerListener>();
        if (listener == null) listener = go.AddComponent<EventTriggerListener>();
        return listener;
    }

    #region 方法
    public override void OnPointerEnter(PointerEventData eventData)
    {
        if (onPointerEnter != null) onPointerEnter(gameObject, eventData);
    }
    public override void OnPointerExit(PointerEventData eventData)
    {
        if (onPointerExit != null) onPointerExit(gameObject, eventData);
    }
    public override void OnPointerDown(PointerEventData eventData)
    {
        if (onPointerDown != null) onPointerDown(gameObject, eventData);
    }
    public override void OnPointerUp(PointerEventData eventData)
    {
        if (onPointerUp != null) onPointerUp(gameObject, eventData);
    }
    public override void OnPointerClick(PointerEventData eventData)
    {
        if (onPointerClick != null) onPointerClick(gameObject, eventData);
    }
    public override void OnInitializePotentialDrag(PointerEventData eventData)
    {
        if (onInitializePotentialDrag != null) onInitializePotentialDrag(gameObject, eventData);
    }
    public override void OnBeginDrag(PointerEventData eventData)
    {
        if (onBeginDrag != null) onBeginDrag(gameObject, eventData);
    }
    public override void OnDrag(PointerEventData eventData)
    {
        if (onDrag != null) onDrag(gameObject, eventData);
    }
    public override void OnEndDrag(PointerEventData eventData)
    {
        if (onEndDrag != null) onEndDrag(gameObject, eventData);
    }
    public override void OnDrop(PointerEventData eventData)
    {
        if (onDrop != null) onDrop(gameObject, eventData);
    }
    public override void OnScroll(PointerEventData eventData)
    {
        if (onScroll != null) onScroll(gameObject, eventData);
    }
    public override void OnUpdateSelected(BaseEventData eventData)
    {
        if (onUpdateSelected != null) onUpdateSelected(gameObject, eventData);
    }
    public override void OnSelect(BaseEventData eventData)
    {
        if (onSelect != null) onSelect(gameObject, eventData);
    }
    public override void OnDeselect(BaseEventData eventData)
    {
        if (onDeselect != null) onDeselect(gameObject, eventData);
    }
    public override void OnMove(AxisEventData eventData)
    {
        if (onMove != null) onMove(gameObject, eventData);
    }
    public override void OnSubmit(BaseEventData eventData)
    {
        if (onSubmit != null) onSubmit(gameObject, eventData);
    }
    public override void OnCancel(BaseEventData eventData)
    {
        if (onCancel != null) onCancel(gameObject, eventData);
    }
    #endregion

}