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

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

你可能感兴趣的:(UGUI)