介绍
这是我项目中因为需求,而根据Unity自带的接口(interface
)创建的一个UI。
我有个文章专门记录了一些UGUI的接口及其用法和触发条件。该文算是那篇文章的一个实例。附上链接:https://blog.csdn.net/f_957995490/article/details/103928846
功能
该UI同时具有点击功能和拖拽功能。项目要求点击在UI的范围内,在该范围投影内的3D物体便会触发响应事件;或者在UI的范围内进行拖拽,可以旋转相应的3D物体。关于旋转的逻辑并贴出来,很多方法很简单,对本文也并不重要。
代码
- 使用委托事件
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;
public class UITouch : Selectable, IPointerClickHandler, IDragHandler, IBeginDragHandler, IEndDragHandler
{
private bool isDrag = false;
public ParameterVecter2 onClick;
public virtual void OnPointerClick(PointerEventData eventData)
{
//Debug.Log(eventData.position);
if (!isDrag)
{//如果经过拖拽,则不出发事件
if (null != onClick) onClick.Invoke(eventData.position);
//Debug.Log("PointerClick");
}
}
public ParameterVecter2 onBeginDrag;
public virtual void OnBeginDrag(PointerEventData eventData)
{
isDrag = true;
if (null != onBeginDrag) onBeginDrag.Invoke(eventData.position);
//Debug.Log("BeginDrag");
}
public ParameterVecter2 onDrag;
public virtual void OnDrag(PointerEventData eventData)
{
if (null != onDrag) onDrag.Invoke(eventData.position);
//Debug.Log("Drag");
}
public ParameterVecter2 onEndDrag;
public virtual void OnEndDrag(PointerEventData eventData)
{
if (null != onEndDrag) onEndDrag.Invoke(eventData.position);
//Debug.Log("EndDrag");
isDrag = false;
}
}
public delegate void ParameterVecter2(Vector2 vec);
public delegate void ParameterVecter3(Vector3 vec);
- 使用
UnityEvent
using System;
using UnityEngine;
using UnityEngine.Events;
using UnityEngine.EventSystems;
using UnityEngine.UI;
namespace ZM_Code
{
/// <summary>
/// UI触碰
/// </summary>
public class UITouch : Selectable, IPointerClickHandler, IDragHandler, IBeginDragHandler, IEndDragHandler
{
private bool isDrag = false;
public ParameterVecter2Event onClick;
public virtual void OnPointerClick(PointerEventData eventData)
{
//Debug.Log(eventData.position);
if (!isDrag)
{//如果经过拖拽,则不出发事件
if (null != onClick) onClick.Invoke(eventData.position);
//Debug.Log("PointerClick");
}
}
public ParameterVecter2Event onBeginDrag;
public virtual void OnBeginDrag(PointerEventData eventData)
{
isDrag = true;
if (null != onBeginDrag) onBeginDrag.Invoke(eventData.position);
//Debug.Log("BeginDrag");
}
public ParameterVecter2Event onDrag;
public virtual void OnDrag(PointerEventData eventData)
{
if (null != onDrag) onDrag.Invoke(eventData.position);
//Debug.Log("Drag");
}
public ParameterVecter2Event onEndDrag;
public virtual void OnEndDrag(PointerEventData eventData)
{
if (null != onEndDrag) onEndDrag.Invoke(eventData.position);
//Debug.Log("EndDrag");
isDrag = false;
}
public ParameterVecter2Event onScroll;
/// <summary>
/// 鼠标滚落事件
/// </summary>
public virtual void OnScroll(PointerEventData eventData)
{
if (null != onScroll) onScroll.Invoke(eventData);
//Debug.Log(eventData.scrollDelta);
//Debug.Log("Scroll");
}
[Serializable]
public class ParameterVecter2Event : UnityEvent<Vector2>
{
public ParameterVecter2Event() { }
}
[Serializable]
public class ParameterVecter3Event : UnityEvent<Vector3>
{
public ParameterVecter3Event() { }
}
}
}