UGUI的事件監聽機制
阿新 • • 發佈:2018-11-03
UGUI控制元件的事件響應有很多種方式,比如使用元件EventTrigger來新增事件監聽,或者實現IDragHandler等介面,或者更直接地繼承EventTrigger來進行更靈話的呼叫。
下面分別就上面3種監聽方式進行講解,這些例子都是實現拖動Image的功能。
一、使用元件EventTrigger來新增事件監聽
新增一個指令碼EventTriggerTest,掛到一個Image上。那麼OK,現在可以拖動圖片了。
[csharp] view plain copy
- 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;
- }
- }
- }
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
- IPointerEnterHandler,
- IPointerExitHandler,
- IPointerDownHandler,
- IPointerUpHandler,
- IPointerClickHandler,
- IInitializePotentialDragHandler,
- IBeginDragHandler,
- IDragHandler,
- IEndDragHandler,
- IDropHandler,
- IScrollHandler,
- IUpdateSelectedHandler,
- ISelectHandler,
- IDeselectHandler,
- IMoveHandler,
- ISubmitHandler,
- 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
- 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;
- }
- }
- }
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
- 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);
- }
- }
- }
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
- 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;
- }
- }
- }
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