美文网首页
UGUI源代码分析(EventData)

UGUI源代码分析(EventData)

作者: UnityChan | 来源:发表于2019-07-12 15:19 被阅读0次

    EventData,事件数据,记录当前事件相关的信息。

    有抽象基类AbstractEventData,衍生出的有BaseEventData,后者又有衍生类PointerEventDataAxisEventDataEventData是UI系统总事件的承载者,其中包含有与该事件相关的各种信息。
    AbstractEventData只负责一件事,用于处理和获取该事件是否被使用的状态。
    BaseEventData比上面多了两个属性,即当前的输入模块currentInputModule和当前选中的对象selectedObject
    AxisEventData也就只包含一个字段,一个属性,分别是移动的方向和移动矢量
    PointerEventData是最复杂的,它里面多了很多和点击,触发相关的信息,该类代码如下

    using System;
    using System.Text;
    using System.Collections.Generic;
    
    namespace UnityEngine.EventSystems
    {
        /// <summary>
        /// Each touch event creates one of these containing all the relevant information.
        /// </summary>
        public class PointerEventData : BaseEventData
        {
            // 鼠标按钮的左中右键
            public enum InputButton
            {
                Left = 0,
                Right = 1,
                Middle = 2
            }
            // 某一帧内按键的状态
            public enum FramePressState
            {
                Pressed,
                Released,
                PressedAndReleased,
                NotChanged
            }
    
            public GameObject pointerEnter { get; set; }            // 接收OnPointerEnter事件的对象
    
            private GameObject m_PointerPress;
            public GameObject lastPress { get; private set; }       //上一次响应按下事件的对象
            public GameObject rawPointerPress { get; set; }         // 发生按下事件的对象,即使它不能响应该事件
            public GameObject pointerDrag { get; set; }             // 接收OnDrag事件的对象
    
    
            public RaycastResult pointerCurrentRaycast { get; set; }  // 当前事件关联的射线投射结果
            public RaycastResult pointerPressRaycast { get; set; }      // 按下(点击)事件关联的射线投射结果
    
            public List<GameObject> hovered = new List<GameObject>();  // 存储的是一组对象,这些对象都接收过OnPointerEnter事件
    
            public bool eligibleForClick { get; set; }                  // 当前事件是否用于点击
    
            public int pointerId { get; set; }
    
            public Vector2 position { get; set; }           // 当前点击(鼠标或触摸)位置
            public Vector2 delta { get; set; }              // 与上一帧点击的变化量
            public Vector2 pressPosition { get; set; }      // 按下的位置
            // World-space position where a ray cast into the screen hits something
            [Obsolete("Use either pointerCurrentRaycast.worldPosition or pointerPressRaycast.worldPosition")]
            public Vector3 worldPosition { get; set; }
            // World-space normal where a ray cast into the screen hits something
            [Obsolete("Use either pointerCurrentRaycast.worldNormal or pointerPressRaycast.worldNormal")]
            public Vector3 worldNormal { get; set; }
            public float clickTime { get; set; }                            // 上一次点击的时间
            public int clickCount { get; set; }                             // 点击的数量
    
            /* 和拖拽相关的信息 */
            public Vector2 scrollDelta { get; set; }                        // 与上一帧相比的滑动量
            public bool useDragThreshold { get; set; }                      // 是否使用拖拽阈值
            public bool dragging { get; set; }                              // 是否正在拖动
    
            public InputButton button { get; set; }
    
            public PointerEventData(EventSystem eventSystem) : base(eventSystem)
            {
                eligibleForClick = false;
    
                pointerId = -1;
                position = Vector2.zero; // Current position of the mouse or touch event
                delta = Vector2.zero; // Delta since last update
                pressPosition = Vector2.zero; // Delta since the event started being tracked
                clickTime = 0.0f; // The last time a click event was sent out (used for double-clicks)
                clickCount = 0; // Number of clicks in a row. 2 for a double-click for example.
    
                scrollDelta = Vector2.zero;
                useDragThreshold = true;
                dragging = false;
                button = InputButton.Left;
            }
    
            public bool IsPointerMoving()
            {
                return delta.sqrMagnitude > 0.0f;
            }
    
            public bool IsScrolling()
            {
                return scrollDelta.sqrMagnitude > 0.0f;
            }
    
            public Camera enterEventCamera
            {
                get { return pointerCurrentRaycast.module == null ? null : pointerCurrentRaycast.module.eventCamera; }
            }
    
            public Camera pressEventCamera
            {
                get { return pointerPressRaycast.module == null ? null : pointerPressRaycast.module.eventCamera; }
            }
    
            public GameObject pointerPress
            {
                get { return m_PointerPress; }
                set
                {
                    if (m_PointerPress == value)
                        return;
    
                    lastPress = m_PointerPress;
                    m_PointerPress = value;
                }
            }
    
            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();
            }
        }
    }
    

    以上便是EventData 的大概内容吧,理解都在注释里了,本次水文结束

    相关文章

      网友评论

          本文标题:UGUI源代码分析(EventData)

          本文链接:https://www.haomeiwen.com/subject/tnnekctx.html