1. 程式人生 > >UGUI的事件監聽機制

UGUI的事件監聽機制

UGUI控制元件的事件響應有很多種方式,比如使用元件EventTrigger來新增事件監聽,或者實現IDragHandler等介面,或者更直接地繼承EventTrigger來進行更靈話的呼叫。

下面分別就上面3種監聽方式進行講解,這些例子都是實現拖動Image的功能。

一、使用元件EventTrigger來新增事件監聽

新增一個指令碼EventTriggerTest,掛到一個Image上。那麼OK,現在可以拖動圖片了。

 

[csharp] view plain copy

在CODE上檢視程式碼片派生到我的程式碼片

  1. using UnityEngine;  
  2. using UnityEngine.EventSystems;  
  3. using Framework;  
  4.   
  5. public class EventTriggerTest : MonoBehaviour  
  6. {  
  7.     public void OnDrag(BaseEventData baseEventData)  
  8.     {  
  9.         PointerEventData data = baseEventData as PointerEventData;  
  10.         var rt = data.pointerPress.GetComponent<RectTransform>();  
  11.         Vector3 globalMousePos;  
  12.         if (RectTransformUtility.ScreenPointToWorldPointInRectangle(rt, data.position, data.pressEventCamera, out globalMousePos))  
  13.         {  
  14.             rt.position = globalMousePos;  
  15.         }  
  16.     }  
  17. }  
using UnityEngine;
using UnityEngine.EventSystems;
using Framework;

public class EventTriggerTest : MonoBehaviour
{
    public void OnDrag(BaseEventData baseEventData)
    {
        PointerEventData data = baseEventData as PointerEventData;
        var rt = data.pointerPress.GetComponent<RectTransform>();
        Vector3 globalMousePos;
        if (RectTransformUtility.ScreenPointToWorldPointInRectangle(rt, data.position, data.pressEventCamera, out globalMousePos))
        {
            rt.position = globalMousePos;
        }
    }
}

 

 

二、實現IDragHandler等介面

UGUI的所有介面如下

 

[csharp] view plain copy

在CODE上檢視程式碼片派生到我的程式碼片

  1. IPointerEnterHandler,  
  2. IPointerExitHandler,  
  3. IPointerDownHandler,  
  4. IPointerUpHandler,  
  5. IPointerClickHandler,  
  6. IInitializePotentialDragHandler,  
  7. IBeginDragHandler,  
  8. IDragHandler,  
  9. IEndDragHandler,  
  10. IDropHandler,  
  11. IScrollHandler,  
  12. IUpdateSelectedHandler,  
  13. ISelectHandler,  
  14. IDeselectHandler,  
  15. IMoveHandler,  
  16. ISubmitHandler,  
  17. ICancelHandler  
        IPointerEnterHandler,
        IPointerExitHandler,
        IPointerDownHandler,
        IPointerUpHandler,
        IPointerClickHandler,
        IInitializePotentialDragHandler,
        IBeginDragHandler,
        IDragHandler,
        IEndDragHandler,
        IDropHandler,
        IScrollHandler,
        IUpdateSelectedHandler,
        ISelectHandler,
        IDeselectHandler,
        IMoveHandler,
        ISubmitHandler,
        ICancelHandler

這裡就實現IDragHandler介面,來實現Image的拖動。新增指令碼EventInterfaceTest,掛到Image上。

 

 

[csharp] view plain copy

在CODE上檢視程式碼片派生到我的程式碼片

  1. using UnityEngine;  
  2. using UnityEngine.EventSystems;  
  3.   
  4. class EventInterfaceTest : MonoBehaviour, IDragHandler  
  5. {  
  6.     public void OnDrag(PointerEventData data)  
  7.     {  
  8.         var rt = gameObject.GetComponent<RectTransform>();  
  9.         Vector3 globalMousePos;  
  10.         if (RectTransformUtility.ScreenPointToWorldPointInRectangle(rt, data.position, data.pressEventCamera, out globalMousePos))  
  11.         {  
  12.             rt.position = globalMousePos;  
  13.         }  
  14.     }  
  15. }  
using UnityEngine;
using UnityEngine.EventSystems;

class EventInterfaceTest : MonoBehaviour, IDragHandler
{
    public void OnDrag(PointerEventData data)
    {
        var rt = gameObject.GetComponent<RectTransform>();
        Vector3 globalMousePos;
        if (RectTransformUtility.ScreenPointToWorldPointInRectangle(rt, data.position, data.pressEventCamera, out globalMousePos))
        {
            rt.position = globalMousePos;
        }
    }
}


三、繼承EventTrigger來進行更靈話的呼叫

 

如果GameObject是動態建立的,那麼就不方便用Unity編輯器加EventTrigger監聽,所以要是能用程式碼方便地進行監聽,又不需要重寫那麼多介面,那是最好不過了。

首先新增指令碼UGUIEventTrigger

 

[csharp] view plain copy

在CODE上檢視程式碼片派生到我的程式碼片

  1. using System.Collections.Generic;  
  2. using UnityEngine;  
  3. using UnityEngine.EventSystems;  
  4. using UnityEngine.Events;  
  5.   
  6. namespace Framework  
  7. {  
  8.     public class UGUIEventTrigger : EventTrigger  
  9.     {  
  10.         public static UGUIEventTrigger Get(GameObject go)  
  11.         {  
  12.             UGUIEventTrigger trigger = go.GetComponent<UGUIEventTrigger>();  
  13.             if (null == trigger)  
  14.             {  
  15.                 trigger = go.AddComponent<UGUIEventTrigger>();  
  16.             }  
  17.             return trigger;  
  18.         }  
  19.   
  20.         public void AddEventListener(EventTriggerType eventTriggerType, UnityAction<BaseEventData> action)  
  21.         {  
  22.             EventTrigger.Entry entry = new EventTrigger.Entry();  
  23.             entry.eventID = eventTriggerType;  
  24.             entry.callback.AddListener(action);  
  25.             if (null == delegates)  
  26.             {  
  27.                 delegates = new List<Entry>();  
  28.             }  
  29.             delegates.Add(entry);  
  30.         }  
  31.     }  
  32. }  
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.Events;

namespace Framework
{
    public class UGUIEventTrigger : EventTrigger
    {
        public static UGUIEventTrigger Get(GameObject go)
        {
            UGUIEventTrigger trigger = go.GetComponent<UGUIEventTrigger>();
            if (null == trigger)
            {
                trigger = go.AddComponent<UGUIEventTrigger>();
            }
            return trigger;
        }

        public void AddEventListener(EventTriggerType eventTriggerType, UnityAction<BaseEventData> action)
        {
            EventTrigger.Entry entry = new EventTrigger.Entry();
            entry.eventID = eventTriggerType;
            entry.callback.AddListener(action);
            if (null == delegates)
            {
                delegates = new List<Entry>();
            }
            delegates.Add(entry);
        }
    }
}

然後編寫測試指令碼,掛到Image上。

[csharp] view plain copy

在CODE上檢視程式碼片派生到我的程式碼片

  1. using UnityEngine;  
  2. using UnityEngine.EventSystems;  
  3.   
  4. public class UGUIEventTriggerTest : MonoBehaviour  
  5. {  
  6.     void Start()  
  7.     {  
  8.         UGUIEventTrigger.Get(gameObject).AddEventListener(EventTriggerType.Drag, OnDrag);  
  9.     }  
  10.   
  11.     private void OnDrag(BaseEventData baseData)  
  12.     {  
  13.         PointerEventData data = baseData as PointerEventData;  
  14.         var rt = data.pointerPress.GetComponent<RectTransform>();  
  15.         Vector3 globalMousePos;  
  16.         if (RectTransformUtility.ScreenPointToWorldPointInRectangle(rt, data.position, data.pressEventCamera, out globalMousePos))  
  17.         {  
  18.             rt.position = globalMousePos;  
  19.         }  
  20.     }  
  21. }  
using UnityEngine;
using UnityEngine.EventSystems;

public class UGUIEventTriggerTest : MonoBehaviour
{
    void Start()
    {
        UGUIEventTrigger.Get(gameObject).AddEventListener(EventTriggerType.Drag, OnDrag);
    }

    private void OnDrag(BaseEventData baseData)
    {
        PointerEventData data = baseData as PointerEventData;
        var rt = data.pointerPress.GetComponent<RectTransform>();
        Vector3 globalMousePos;
        if (RectTransformUtility.ScreenPointToWorldPointInRectangle(rt, data.position, data.pressEventCamera, out globalMousePos))
        {
            rt.position = globalMousePos;
        }
    }
}

原地址:https://blog.csdn.net/gcs6564157/article/details/54767452