【Unity3D ugui】事件监听机制

UGUI控件的事件响应有很多种方式,比如使用组件EventTrigger来添加事件监听,或者实现IDragHandler等接口,或者更直接地继承EventTrigger来进行更灵话的调用。

下面分别就上面3种监听方式进行讲解,这些例子都是实现拖动Image的功能。

一、使用组件EventTrigger来添加事件监听

添加一个脚本EventTriggerTest,挂到一个Image上。那么OK,现在可以拖动图片了。

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();
		Vector3 globalMousePos;
		if (RectTransformUtility.ScreenPointToWorldPointInRectangle(rt, data.position, data.pressEventCamera, out globalMousePos))
		{
			rt.position = globalMousePos;
		}
	}
}
【Unity3D ugui】事件监听机制_第1张图片


二、实现IDragHandler等接口

UGUI的所有接口如下

        IPointerEnterHandler,
        IPointerExitHandler,
        IPointerDownHandler,
        IPointerUpHandler,
        IPointerClickHandler,
        IInitializePotentialDragHandler,
        IBeginDragHandler,
        IDragHandler,
        IEndDragHandler,
        IDropHandler,
        IScrollHandler,
        IUpdateSelectedHandler,
        ISelectHandler,
        IDeselectHandler,
        IMoveHandler,
        ISubmitHandler,
        ICancelHandler
这里就实现IDragHandler接口,来实现Image的拖动。添加脚本EventInterfaceTest,挂到Image上。

using UnityEngine;
using UnityEngine.EventSystems;

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

三、继承EventTrigger来进行更灵话的调用

如果GameObject是动态创建的,那么就不方便用Unity编辑器加EventTrigger监听,所以要是能用代码方便地进行监听,又不需要重写那么多接口,那是最好不过了。

首先添加脚本UGUIEventTrigger

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();
			if (null == trigger)
			{
				trigger = go.AddComponent();
			}
			return trigger;
		}

		public void AddEventListener(EventTriggerType eventTriggerType, UnityAction action)
		{
			EventTrigger.Entry entry = new EventTrigger.Entry();
			entry.eventID = eventTriggerType;
			entry.callback.AddListener(action);
			if (null == delegates)
			{
				delegates = new List();
			}
			delegates.Add(entry);
		}
	}
}
然后编写测试脚本,挂到Image上。
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();
		Vector3 globalMousePos;
		if (RectTransformUtility.ScreenPointToWorldPointInRectangle(rt, data.position, data.pressEventCamera, out globalMousePos))
		{
			rt.position = globalMousePos;
		}
	}
}




你可能感兴趣的:(C#,Unity3D,移动开发,项目开发)