PointerEventData介绍
PointerEventData eventData在Unity中是用于传递指针事件数据的类,包含了有关指针事件的信息,如位置、按下状态、各种事件类型对应的物体等。在拖拽事件和鼠标检测事件中都会使用到这个类来获取事件数据。
1.PointerEventData eventData 它继承于BaseEventData类,含有多个字段,如下是源码:
/// <summary>
/// A class that can be used for sending simple events via the event system.
/// </summary>
public abstract class AbstractEventData
{
protected bool m_Used;
/// <summary>
/// Reset the event.
/// </summary>
public virtual void Reset()
{
m_Used = false;
}
/// <summary>
/// Use the event.
/// </summary>
/// <remarks>
/// Internally sets a flag that can be checked via used to see if further processing should happen.
/// </remarks>
public virtual void Use()
{
m_Used = true;
}
/// <summary>
/// Is the event used?
/// </summary>
public virtual bool used
{
get { return m_Used; }
}
}
/// <summary>
/// A class that contains the base event data that is common to all event types in the new EventSystem.
/// </summary>
public class BaseEventData : AbstractEventData
{
private readonly EventSystem m_EventSystem;
public BaseEventData(EventSystem eventSystem)
{
m_EventSystem = eventSystem;
}
/// <summary>
/// >A reference to the BaseInputModule that sent this event.
/// </summary>
public BaseInputModule currentInputModule
{
get { return m_EventSystem.currentInputModule; }
}
/// <summary>
/// The object currently considered selected by the EventSystem.
/// </summary>
public GameObject selectedObject
{
get { return m_EventSystem.currentSelectedGameObject; }
set { m_EventSystem.SetSelectedGameObject(value, this); }
}
}
BaseEventData类是基础的事件数据类,而AbstractEventData类是事件数据的抽象基类。BaseEventData类可以看到包含EventSystem m_EventSystem当前的事件管理器,BaseInputModule currentInputModule 当前的输入模块,GameObject selectedObject 当前事件管理器认为的选中物体 这三个字段。而AbstractEventData类 包含bool m_Used 是否已经被使用过 这个一个字段。
2.PointerEventData eventData 本身包含字段也很多:
- 枚举InputButton,包含鼠标的左右中键;
- 枚举FramePressState,包含表示鼠标的左右中键的状态:按下、释放、按下和释放、没有改变;
- GameObject pointerEnter 用于OnPointerEnter和OnPointerExit,前者设置,后者取消;
- GameObject m_PointerPress和pointerPress 用于OnPointerDown和OnPointerUp,前者设置,后者取消;
- GameObject lastPress 原始按下的对象GameObject。这意味着即使它本身无法接收OnPointerDown按下事件,它仍然是“被按下”的GameObject;
- GameObject rawPointerPress 即使无法处理按下事件,按下操作所作用的对象;
- GameObject pointerDrag 用于OnDrag
- GameObject pointerClick 用于OnPointerClick
- RaycastResult pointerCurrentRaycast 用于当前事件关联的 RaycastResult
- RaycastResult pointerPressRaycast 用于最后一按下事件关联的RaycastResult
- List<GameObject> hovered 用于存储指针(如鼠标或触摸光标)当前悬停在其上的所有GameObject对象的列表。
- bool eligibleForClick 用于是否可以点击
- int displayIndex 用于当前事件的索引
- int pointerId 用于键的id, 使用鼠标的情况下, -1代表左键, -2代表右键, -3代表中键
- Vector2 position 用于当前触摸或者点击的位置
- Vector2 delta 用于上次更新到此时更新间的位置变化量
- Vector2 pressPosition 用于当前(或者最后一次)点击或者触摸的位置
- Vector3 worldPosition 用于世界坐标下的坐标
- Vector3 worldNormal 用于世界坐标下的法线
- float clickTime 用于上次点击或者触摸的时间
- int clickCount 用于点击次数,双击等
- Vector2 scrollDelta 用于上次更新以来的滚动量
- bool useDragThreshold 用于是否使用拖拽阈值
- bool dragging 用于是否是拖拽状态
- InputButton button 用于当前事件的按键
- 以下是UIElements的事件字段:float pressure、float tangentialPressure、float altitudeAngle、float azimuthAngle、float twist、Vector2 tilt、PenStatus penStatus、Vector2 radius、bool reentered用到的较少。
- Vector2 radiusVariance 触摸半径的准确性,将此值与半径相加以获得最大触摸半径,减去此值以获得最小触摸半径。
- bool fullyExited 在指针退出的情况下,指定指针是完全退出区域还是刚刚进入子区域
- IsPointerMoving() 用于是否指针处于移动状态
- IsScrolling() 用于输入设备上是否正在使用滚动
- enterEventCamera 用于当前事件相关联的摄像机
- pressEventCamera 用于上次事件相关联的摄像机
总结:
简单介绍了关于指针事件类PointerEventData的关键字段和具体的作用, 希望对大家有所帮助。