diff options
author | chai <chaifix@163.com> | 2020-10-11 09:12:08 +0800 |
---|---|---|
committer | chai <chaifix@163.com> | 2020-10-11 09:12:08 +0800 |
commit | b1276a1b76ac3b87add90e0c6b887d5afea1cfea (patch) | |
tree | ae12e1bc24f6907c9870bf062d020d6ce1db6fa1 /Assets/uGUI-2017.1/UnityEngine.UI/EventSystem/InputModules/StandaloneInputModule.cs | |
parent | 8d89ca7b0662cff2a93b33ed92205ff3f6170436 (diff) |
*event system鍒濇帰
Diffstat (limited to 'Assets/uGUI-2017.1/UnityEngine.UI/EventSystem/InputModules/StandaloneInputModule.cs')
-rw-r--r-- | Assets/uGUI-2017.1/UnityEngine.UI/EventSystem/InputModules/StandaloneInputModule.cs | 59 |
1 files changed, 44 insertions, 15 deletions
diff --git a/Assets/uGUI-2017.1/UnityEngine.UI/EventSystem/InputModules/StandaloneInputModule.cs b/Assets/uGUI-2017.1/UnityEngine.UI/EventSystem/InputModules/StandaloneInputModule.cs index ee6f216..f9a2f77 100644 --- a/Assets/uGUI-2017.1/UnityEngine.UI/EventSystem/InputModules/StandaloneInputModule.cs +++ b/Assets/uGUI-2017.1/UnityEngine.UI/EventSystem/InputModules/StandaloneInputModule.cs @@ -1,3 +1,9 @@ +/* + * StandaloneInputModule : + * PointerInputModule : + * BaseInputModule + */ + using System; using UnityEngine; using UnityEngine.Serialization; @@ -11,6 +17,7 @@ namespace UnityEngine.EventSystems private Vector2 m_LastMoveVector; private int m_ConsecutiveMoveCount = 0; + // 上一个鼠标位置和当前鼠标位置,每帧更新 private Vector2 m_LastMousePosition; private Vector2 m_MousePosition; @@ -136,11 +143,14 @@ namespace UnityEngine.EventSystems } } + //c 更新鼠标坐标 public override void UpdateModule() { + // 如果没有获取焦点,且忽略没有焦点时的事件,返回(除非开启远程连接,否则会这样) if (!eventSystem.isFocused && ShouldIgnoreEventsOnNoFocus()) return; + // 更新鼠标位置 m_LastMousePosition = m_MousePosition; m_MousePosition = input.mousePosition; } @@ -190,41 +200,49 @@ namespace UnityEngine.EventSystems base.DeactivateModule(); ClearSelection(); } - + + //c input module事件处理主入口 public override void Process() { - if (!eventSystem.isFocused && ShouldIgnoreEventsOnNoFocus()) + if (!eventSystem.isFocused && ShouldIgnoreEventsOnNoFocus()) // 如果没有获取焦点,不需要处理 return; + //event 给选中的对象发布更新事件 bool usedEvent = SendUpdateEventToSelectedObject(); + // 如果勾上了,发送move , submit, cancel事件 if (eventSystem.sendNavigationEvents) { if (!usedEvent) - usedEvent |= SendMoveEventToSelectedObject(); + usedEvent |= SendMoveEventToSelectedObject(); //event 键盘方向键移动时给eventSystem选中的对象发布IMoveHandler事件 if (!usedEvent) - SendSubmitEventToSelectedObject(); + SendSubmitEventToSelectedObject(); //event 如果按了相应按键,发布submit或者cancel事件 } + // 模拟触摸或鼠标输入 // touch needs to take precedence because of the mouse emulation layer - if (!ProcessTouchEvents() && input.mousePresent) + usedEvent = ProcessTouchEvents(); //event 发布触摸事件 + + if(!usedEvent && input.mousePresent) ProcessMouseEvent(); } + //c 触摸事件 private bool ProcessTouchEvents() { - for (int i = 0; i < input.touchCount; ++i) + for (int i = 0; i < input.touchCount; ++i) //多点触控 { Touch touch = input.GetTouch(i); if (touch.type == TouchType.Indirect) continue; - bool released; - bool pressed; - var pointer = GetTouchPointerEventData(touch, out pressed, out released); + bool released; // 这是一个手指抬起操作 + bool pressed; // 这是一个手指放下操作 + var pointer = GetTouchPointerEventData(touch, out pressed, out released); // 射线检测并保存检测结果 + // 处理触摸或抬起反馈,已经准备好了被触摸的物体 ProcessTouchPress(pointer, pressed, released); if (!released) @@ -238,10 +256,12 @@ namespace UnityEngine.EventSystems return input.touchCount > 0; } + //c 处理触摸\抬起 protected void ProcessTouchPress(PointerEventData pointerEvent, bool pressed, bool released) { var currentOverGo = pointerEvent.pointerCurrentRaycast.gameObject; + // 触摸反馈 // PointerDown notification if (pressed) { @@ -261,6 +281,7 @@ namespace UnityEngine.EventSystems pointerEvent.pointerEnter = currentOverGo; } + //event IPointerDownHandler // search for the control that will receive the press // if we can't find a press handler set the press // handler to be what would receive a click. @@ -301,6 +322,7 @@ namespace UnityEngine.EventSystems ExecuteEvents.Execute(pointerEvent.pointerDrag, pointerEvent, ExecuteEvents.initializePotentialDrag); } + // 抬起反馈 // PointerUp notification if (released) { @@ -310,14 +332,14 @@ namespace UnityEngine.EventSystems // Debug.Log("KeyCode: " + pointer.eventData.keyCode); // see if we mouse up on the same element that we clicked on... - var pointerUpHandler = ExecuteEvents.GetEventHandler<IPointerClickHandler>(currentOverGo); + var pointerUpHandler = ExecuteEvents.GetEventHandler<IPointerClickHandler>(currentOverGo); // 从这个对象开始往上找,直到一个挂了继承了IPointerClickHandler的组件 // PointerClick and Drop events - if (pointerEvent.pointerPress == pointerUpHandler && pointerEvent.eligibleForClick) + if (pointerEvent.pointerPress == pointerUpHandler && pointerEvent.eligibleForClick) //只有在pointerPress == pointerUpHandler时才会触发click事件 { ExecuteEvents.Execute(pointerEvent.pointerPress, pointerEvent, ExecuteEvents.pointerClickHandler); } - else if (pointerEvent.pointerDrag != null && pointerEvent.dragging) + else if (pointerEvent.pointerDrag != null && pointerEvent.dragging) // 如果两者不相等,触发drop事件 { ExecuteEvents.ExecuteHierarchy(currentOverGo, pointerEvent, ExecuteEvents.dropHandler); } @@ -346,6 +368,8 @@ namespace UnityEngine.EventSystems if (eventSystem.currentSelectedGameObject == null) return false; + // 发布submit事件或者cancel事件 + var data = GetBaseEventData(); if (input.GetButtonDown(m_SubmitButton)) ExecuteEvents.Execute(eventSystem.currentSelectedGameObject, data, ExecuteEvents.submitHandler); @@ -386,15 +410,16 @@ namespace UnityEngine.EventSystems float time = Time.unscaledTime; Vector2 movement = GetRawMoveVector(); - if (Mathf.Approximately(movement.x, 0f) && Mathf.Approximately(movement.y, 0f)) + if (Mathf.Approximately(movement.x, 0f) && Mathf.Approximately(movement.y, 0f)) // 如果键盘方向键没动,返回 { m_ConsecutiveMoveCount = 0; return false; } + //allow即是否允许发布事件 // If user pressed key again, always allow event bool allow = input.GetButtonDown(m_HorizontalAxis) || input.GetButtonDown(m_VerticalAxis); - bool similarDir = (Vector2.Dot(movement, m_LastMoveVector) > 0); + bool similarDir = (Vector2.Dot(movement, m_LastMoveVector) > 0); // 和之前的方向是否夹角在90°以内,如果是的话说明很接近 if (!allow) { // Otherwise, user held down key or axis. @@ -408,15 +433,18 @@ namespace UnityEngine.EventSystems if (!allow) return false; + // 发布IMoveHandler事件 + // Debug.Log(m_ProcessingEvent.rawType + " axis:" + m_AllowAxisEvents + " value:" + "(" + x + "," + y + ")"); var axisEventData = GetAxisEventData(movement.x, movement.y, 0.6f); if (axisEventData.moveDir != MoveDirection.None) { - ExecuteEvents.Execute(eventSystem.currentSelectedGameObject, axisEventData, ExecuteEvents.moveHandler); + ExecuteEvents.Execute(eventSystem.currentSelectedGameObject, axisEventData, ExecuteEvents.moveHandler); // IMoveHandler.OnMove if (!similarDir) m_ConsecutiveMoveCount = 0; m_ConsecutiveMoveCount++; + m_PrevActionTime = time; m_LastMoveVector = movement; } @@ -473,6 +501,7 @@ namespace UnityEngine.EventSystems return false; var data = GetBaseEventData(); + //event 向eventSystem当前选中的gameobject发布一个更新事件IUpdateSelectedHandler,比如inputfield ExecuteEvents.Execute(eventSystem.currentSelectedGameObject, data, ExecuteEvents.updateSelectedHandler); return data.used; } |