Unity 新UI事件系統(EventSystem) Demo

Unity 新UI事件系統(EventSystem) Demo

時間:2015-03-06 16:59:57      閱讀:4986      評論:0      收藏:0      [點我收藏+]

標籤:des   class   style   代碼   com   log   si   it   方法   

新的UI系統消息傳遞被全新設計,該系統用MonoBehaviour 來實現自定義的接口,以便可以接受來自消息系統的回調。當一個對象被消息的執行指定,那麼這個對象的所有實現了自定義接口的腳本將被通知,指定的方法將被執行。

 

1.定義一個接口ICustomMessageTarget繼承IEventSystemHandler, 這樣當發送此類型消息時,實現此接口的腳本所在的對象將會被通知。

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

public interface ICustomMessageTarget : IEventSystemHandler
{
    void Message1();
    void Message2();
}

2.測試腳本CustomMessageTarget實現ICustomMessageTarget,加到一個對象上。

using UnityEngine;
using System.Collections;

public class CustomMessageTarget : MonoBehaviour , ICustomMessageTarget
{

    public void Message1()
    {
        Debug.Log("Message1 received.");
    }
    public void Message2()
    {
        Debug.Log("Message2 received.");
    }
}

這樣當執行下面代碼時事件將被觸發

 ExecuteEvents.Execute<ICustomMessageTarget>(go, null, (x, y) => x.Message1());

 

同時EventSystem還提供了大量的事件, 只需要在腳本中實現這些接口就可以觸發相應的方法

  • IPointerEnterHandler - OnPointerEnter - Called when a pointer enters the object
  • IPointerExitHandler - OnPointerExit - Called when a pointer exits the object
  • IPointerDownHandler - OnPointerDown - Called when a pointer is pressed on the object
  • IPointerUpHandler - OnPointerUp - Called when a pointer is released (called on the original the pressed object)
  • IPointerClickHandler - OnPointerClick - Called when a pointer is pressed and released on the same object
  • IInitializePotentialDragHandler - OnInitializePotentialDrag - Called when a drag target is found, can be used to initialise values
  • IBeginDragHandler - OnBeginDrag - Called on the drag object when dragging is about to begin
  • IDragHandler - OnDrag - Called on the drag object when a drag is happening
  • IEndDragHandler - OnEndDrag - Called on the drag object when a drag finishes
  • IDropHandler - OnDrop - Called on the object where a drag finishes
  • IScrollHandler - OnScroll - Called when a mouse wheel scrolls
  • IUpdateSelectedHandler - OnUpdateSelected - Called on the selected object each tick
  • ISelectHandler - OnSelect - Called when the object becomes the selected object
  • IDeselectHandler - OnDeselect - Called on the selected object becomes deselected
  • IMoveHandler - OnMove - Called when a move event occurs (left, right, up, down, ect)
  • ISubmitHandler - OnSubmit - Called when the submit button is pressed
  • ICancelHandler - OnCancel - Called when the cancel button is pressed

例子:

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

public class TestPointerEnterEvent : MonoBehaviour , IPointerEnterHandler , IPointerClickHandler
{

    public void OnPointerEnter(PointerEventData data)
    {
        Debug.Log("Pointer enter.");
    }

    public void OnPointerClick(PointerEventData data)
    {
        Debug.Log("Pointer click.");
    }
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章