Event system of UGUI source code analysis in Unity (3)-EventData

Event system of UGUI source code analysis in Unity (3)-EventData

In order to pass data in the event system, Unity provides EventData related classes to encapsulate this type of data. Understanding these structures will help us learn the following modules.

There are several classes related to EventData , which will be introduced one by one below.

  • AbstractEventData: abstract base class for event data
  • BaseEventData : AbstractEventData: basic event data class
  • PointerEventData: BaseEventData: pointer event data class
  • AxisEventData: BaseEventData: Axis event data class

AbstractEventData

AbstractEventData is the abstract base class of event data, with a small content, and the interfaces and properties provided mainly focus on whether the event data has been used.

In some logic, if event data is marked as used in some event processing, it will not be processed in other events.

public abstract class AbstractEventData
{
    protected bool m_Used;

    public virtual void Reset()
    {
        m_Used = false;
    }

    public virtual void Use()
    {
        m_Used = true;
    }

    public virtual bool used
    {
        get { return m_Used; }
    }
}

BaseEventData

BaseEventData is the basic event data class, and the content is not much. It mainly provides references to several event system roles:

  • EventSystem
  • The currently active input module
  • currently selected object
public class BaseEventData : AbstractEventData
{
    private readonly EventSystem m_EventSystem;
    public BaseEventData(EventSystem eventSystem)
    {
        m_EventSystem = eventSystem;
    }

    public BaseInputModule currentInputModule
    {
        get { return m_EventSystem.currentInputModule; }
    }

    public GameObject selectedObject
    {
        get { return m_EventSystem.currentSelectedGameObject; }
        set { m_EventSystem.SetSelectedGameObject(value, this); }
    }
}

AxisEventData

AxisEventData is an axis event data class, which encapsulates the data related to axis events. This axis is the Edit->Project Settings->InputAxes in it, which is mainly used in the processing of navigation events and displacement events (OnMove), providing displacement vectors and directions.

This type is mainly used to encapsulate mobile event data when using devices such as handles.

public enum MoveDirection
{
    Left,
    Up,
    Right,
    Down,
    None
}

public class AxisEventData : BaseEventData
{
    public Vector2 moveVector { get; set; }
    public MoveDirection moveDir { get; set; }

    public AxisEventData(EventSystem eventSystem)
        : base(eventSystem)
        {
            moveVector = Vector2.zero;
            moveDir = MoveDirection.None;
        }
}

PointerEventData

Next is our highlight, PointerEventData is a pointer event data class, which is also the main part of the event data used by the entire event system.

The so-called pointer event is simply touch and click. For details, please refer to the first overview.

Unity encapsulates touch and click-related information into PointerEventData , and simulates touch events as mouse left button clicks.

InputButton和FramePressState

Unity encapsulates InputButton , provides the abstraction of several mouse buttons, and expresses the status of several buttons through FramePressState .

public class PointerEventData : BaseEventData
{
    // 分别代表左右中键
    public enum InputButton
    {
        Left = 0,
        Right = 1,
        Middle = 2
    }

    // 每个键在**当前帧**的状态
    public enum FramePressState
    {
        Pressed, // 按下
        Released, // 抬起
        PressedAndReleased, // 按下又抬起
        NotChanged // 与上一帧相同
    }

    // ...
}

Basic data and status information

PointerEventData provides information such as the basis and status of some properties that encapsulate the event.

The main contents are as follows:

  • public int pointerId: The id of the key. When using the mouse, -1 represents the left button, -2 represents the right button, and -3 represents the middle button. It has not been tested when using touch, and it will be added later when there is a chance to test.
  • public bool eligibleForClick: Whether the event is a qualified click at this time, as you can see later, if the movement event is processed before the click event is processed, this state will be canceled and the click event cannot be processed. This state is the key for ScrollRect to be able to slide and click at the same time .
  • public Vector2 position: The current touch or click position, in screen coordinates
  • public Vector2 delta: The amount of position change between the last update and this update
  • public Vector2 pressPosition: current (or last) click or touch position
  • public float clickTime: time of last click or touch
  • public int clickCount: Number of clicks, used for rapid multiple clicks in a short period of time, such as double-clicking, etc.
  • public Vector2 scrollDelta: The amount of rolling change between the last update and this update
  • public bool useDragThreshold: Whether to use the drag threshold, which is the value set on the EventSystem component introduced in the previous article
  • public bool dragging: Whether it is currently dragged or not
  • public InputButton button: the key of the current event
  • public bool IsPointerMoving: Whether there has been movement between the last update and this update
  • public bool IsScrolling: Whether a rollover occurred between the last update and this update

game object held by

Every time the pointer data involves some game objects, PointerEventData records some objects for use.

There are mainly the following properties to save the game object:

  • public GameObject pointerEnter: The object that the mouse enters, that is to say, the position of the mouse pointer is inside a certain object area
  • public GameObject lastPress: The last object that received OnPointerDownthe event
  • public GameObject rawPointerPress: The original pressed object, no matter whether it handles OnPointerDownthe event or not, there may be some doubts here, and we will understand it later when we introduce event handling
  • public GameObject pointerPress: The object that has currently received OnPointerDownthe event
  • public GameObject pointerDrag: The object that has currently received OnDragthe event
  • public RaycastResult pointerCurrentRaycast: RaycastResult associated with the current event
  • public RaycastResult pointerPressRaycast: RaycastResult associated with the last press event
  • public List<GameObject> hovered: When the mouse hovers, the object list formed by pointerEnter and all its parent nodes and ancestor nodes
  • public Camera enterEventCamera: the camera associated with the current event
  • public Camera pressEventCamera: RaycastResult associated with the last press event

ToString

Work with the method of the EventSystem component introduced in the previous article ToStringto display the current event system status.

public override string ToString()
{
    var sb = new StringBuilder();
    sb.AppendLine("<b>Position</b>: " + position);
    sb.AppendLine("<b>delta</b>: " + delta);
    sb.AppendLine("<b>eligibleForClick</b>: " + eligibleForClick);
    sb.AppendLine("<b>pointerEnter</b>: " + pointerEnter);
    sb.AppendLine("<b>pointerPress</b>: " + pointerPress);
    sb.AppendLine("<b>lastPointerPress</b>: " + lastPress);
    sb.AppendLine("<b>pointerDrag</b>: " + pointerDrag);
    sb.AppendLine("<b>Use Drag Threshold</b>: " + useDragThreshold);
    sb.AppendLine("<b>Current Rayast:</b>");
    sb.AppendLine(pointerCurrentRaycast.ToString());
    sb.AppendLine("<b>Press Rayast:</b>");
    sb.AppendLine(pointerPressRaycast.ToString());
    return sb.ToString();
}

insert image description here

As shown in the figure, they are the state information of a certain frame of the left button, right button and middle button, which can help us understand well when studying the event system.

Summarize

Today we introduced one of the foundations of the event system: event data.

We know how Unity defines events and encapsulates event data, which lays the foundation for us to study the next modules.

In the next article, we will introduce the message system ExecuteEvents , and see how Unity cleverly implements the conventional heavy message system.

Well, that's all for today's content, I hope it will be helpful to everyone.

Guess you like

Origin blog.csdn.net/woodengm/article/details/123430626