目录
方式一:组件拖拽
方式二:实现事件接口
方式三:代码添加事件
方式四:继承EventTrigger,重写需要的逻辑
看到群里有小伙伴再问UI的事件监听,分享一下。
方式一:组件拖拽添加EventTrigger组件,拖拽实例,添加事件的这种方式就省略不讲了,在实际运用中虽然方便简单但是不推荐使用。
方式二:实现事件接口using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
public class InfoPanel: MonoBehaviour,IPointerEnterHandler,IPointerExitHandler,IPointerDownHandler,IPointerClickHandler{
public void OnPointerEnter (PointerEventData eventData)
{
Debug.Log ("enter");
//鼠标进入当前实例范围,就触发
Debug.Log (eventData.pointerEnter.name);
}
public void OnPointerExit(PointerEventData eventData){
Debug.Log ("exit");
}
//按下就触发
public void OnPointerDown(PointerEventData eventData){
Debug.Log ("down");
}
//鼠标点击完成(按下,抬起)触发 ,如果按下的时候在别的地方抬起,则不会触发
public void OnPointerClick(PointerEventData eventData){
Debug.Log ("click");
}
}
方式三:代码添加事件
public Image image;
public Image image1;
// Use this for initialization
void Start()
{
AddEventTriggerEvent(image, EventTriggerType.PointerDown, (o) => { Debug.Log("PointerDown"); });
AddEventTriggerEvent(image1, EventTriggerType.PointerClick, (o) => { Debug.Log("PointerClick"); });
}
///
/// 为EventTrigger添加事件及事件监听。
///
/// 带有或需要添加EventTrigger的对象
/// 要添加的事件的类型
/// 事件的触发回调
private void AddEventTriggerEvent(Component obj, EventTriggerType eventType, UnityAction callback)
{
EventTrigger.Entry entry = null;
EventTrigger trigger = obj.GetComponent();
if (trigger != null) // 已有EventTrigger
{
// 查找是否已经存在要注册的事件
foreach (EventTrigger.Entry existingEntry in trigger.triggers)
{
if (existingEntry.eventID == eventType)
{
entry = existingEntry;
break;
}
}
}
else
{
trigger = obj.gameObject.AddComponent();
}
// 如果这个事件不存在,就创建新的实例
if (entry == null)
{
entry = new EventTrigger.Entry();
entry.eventID = eventType;
}
// 添加触发回调并注册事件
entry.callback.AddListener(callback);
trigger.triggers.Add(entry);
}
方式四:继承EventTrigger,重写需要的逻辑
public class UGUIEventListener : EventTrigger
{
public delegate void VoidDelegate(GameObject go);
public VoidDelegate onClick;
public VoidDelegate onDown;
public VoidDelegate onEnter;
public VoidDelegate onExit;
public VoidDelegate onUp;
public VoidDelegate onSelect;
public VoidDelegate onUpdateSelect;
public VoidDelegate onLongPress; //长按按键
public float interval = 1f; //长按按键判断间隔
public float invokeInterval = 0.2f; //长按状态方法调用间隔
private bool isPointDown = false;
private float lastInvokeTime; //鼠标点击下的时间
private float timer;
static public UGUIEventListener Get(GameObject go)
{
UGUIEventListener listener = go.GetComponent();
if (listener == null) listener = go.AddComponent();
return listener;
}
public override void OnPointerClick(PointerEventData eventData)
{
if (onClick != null) onClick(gameObject);
}
public override void OnPointerDown(PointerEventData eventData)
{
isPointDown = true;
lastInvokeTime = Time.time;
if (onDown != null) onDown(gameObject);
}
public override void OnPointerEnter(PointerEventData eventData)
{
if (onEnter != null) onEnter(gameObject);
}
public override void OnPointerExit(PointerEventData eventData)
{
isPointDown = false; //鼠标移出按钮时推出长按状态
if (onExit != null) onExit(gameObject);
}
public override void OnPointerUp(PointerEventData eventData)
{
isPointDown = false;
if (onUp != null) onUp(gameObject);
}
public override void OnSelect(BaseEventData eventData)
{
if (onSelect != null) onSelect(gameObject);
}
public override void OnUpdateSelected(BaseEventData eventData)
{
if (onUpdateSelect != null) onUpdateSelect(gameObject);
}
private void Update()
{
if (isPointDown)
{
if (Time.time - lastInvokeTime > interval)
{
timer += Time.deltaTime;
if (timer > invokeInterval)
{
onLongPress.Invoke(gameObject);
timer = 0;
}
}
}
}
public GameObject image;
// Use this for initialization
private void Awake()
{
UGUIEventListener.Get(image).onLongPress = OnLongPress;
UGUIEventListener.Get(image).onClick = OnClick;
UGUIEventListener.Get(image).onDown = OnDown;
UGUIEventListener.Get(image).onUp = OnUp;
UGUIEventListener.Get(image).onEnter = OnEnter;
UGUIEventListener.Get(image).onExit = OnExit;
}
private void OnExit(GameObject go)
{
Debug.Log("OnExit");
}
private void OnEnter(GameObject go)
{
Debug.Log("OnEnter");
}
private void OnUp(GameObject go)
{
Debug.Log("OnUp");
}
private void OnDown(GameObject go)
{
Debug.Log("OnDown");
}
private void OnClick(GameObject go)
{
Debug.Log("OnClick");
}
public void OnLongPress(GameObject go)
{
Debug.Log("OnLongPress");
}