summaryrefslogtreecommitdiff
path: root/Assets/uGUI-2017.1/UnityEngine.UI/EventSystem/InputModules
diff options
context:
space:
mode:
authorchai <chaifix@163.com>2020-10-11 09:12:08 +0800
committerchai <chaifix@163.com>2020-10-11 09:12:08 +0800
commitb1276a1b76ac3b87add90e0c6b887d5afea1cfea (patch)
treeae12e1bc24f6907c9870bf062d020d6ce1db6fa1 /Assets/uGUI-2017.1/UnityEngine.UI/EventSystem/InputModules
parent8d89ca7b0662cff2a93b33ed92205ff3f6170436 (diff)
*event system鍒濇帰
Diffstat (limited to 'Assets/uGUI-2017.1/UnityEngine.UI/EventSystem/InputModules')
-rw-r--r--Assets/uGUI-2017.1/UnityEngine.UI/EventSystem/InputModules/BaseInput.cs3
-rw-r--r--Assets/uGUI-2017.1/UnityEngine.UI/EventSystem/InputModules/BaseInputModule.cs19
-rw-r--r--Assets/uGUI-2017.1/UnityEngine.UI/EventSystem/InputModules/PointerInputModule.cs24
-rw-r--r--Assets/uGUI-2017.1/UnityEngine.UI/EventSystem/InputModules/StandaloneInputModule.cs59
-rw-r--r--Assets/uGUI-2017.1/UnityEngine.UI/EventSystem/InputModules/TouchInputModule.cs5
5 files changed, 76 insertions, 34 deletions
diff --git a/Assets/uGUI-2017.1/UnityEngine.UI/EventSystem/InputModules/BaseInput.cs b/Assets/uGUI-2017.1/UnityEngine.UI/EventSystem/InputModules/BaseInput.cs
index a761057..8d22d04 100644
--- a/Assets/uGUI-2017.1/UnityEngine.UI/EventSystem/InputModules/BaseInput.cs
+++ b/Assets/uGUI-2017.1/UnityEngine.UI/EventSystem/InputModules/BaseInput.cs
@@ -1,5 +1,8 @@
namespace UnityEngine.EventSystems
{
+
+ //c 对UnityEngine.Input进行一次简单包装,方便调试
+ // 另外如果希望自定义输入,继承这个类,并实现这些属性
public class BaseInput : UIBehaviour
{
public virtual string compositionString
diff --git a/Assets/uGUI-2017.1/UnityEngine.UI/EventSystem/InputModules/BaseInputModule.cs b/Assets/uGUI-2017.1/UnityEngine.UI/EventSystem/InputModules/BaseInputModule.cs
index 7125a4d..7661a8e 100644
--- a/Assets/uGUI-2017.1/UnityEngine.UI/EventSystem/InputModules/BaseInputModule.cs
+++ b/Assets/uGUI-2017.1/UnityEngine.UI/EventSystem/InputModules/BaseInputModule.cs
@@ -14,9 +14,12 @@ namespace UnityEngine.EventSystems
private EventSystem m_EventSystem;
private BaseEventData m_BaseEventData;
- protected BaseInput m_InputOverride;
- private BaseInput m_DefaultInput;
-
+#region 输入系统
+ // 如果m_InputOverride不为空就用它,否则用默认的m_DefaultInput
+ // 如果要自己实现BaseInput,继承此类并给这个字段赋值
+ // StandaloneInputModule没有自定义,直接采用的是BaseInput
+ protected BaseInput m_InputOverride;
+ private BaseInput m_DefaultInput;
public BaseInput input
{
get
@@ -40,12 +43,12 @@ namespace UnityEngine.EventSystems
if (m_DefaultInput == null)
m_DefaultInput = gameObject.AddComponent<BaseInput>();
}
-
return m_DefaultInput;
}
}
+#endregion
- protected EventSystem eventSystem
+ protected EventSystem eventSystem
{
get { return m_EventSystem; }
}
@@ -54,13 +57,13 @@ namespace UnityEngine.EventSystems
{
base.OnEnable();
m_EventSystem = GetComponent<EventSystem>();
- m_EventSystem.UpdateModules();
+ m_EventSystem.UpdateModules(); // 把此input module加入EventSystem
}
protected override void OnDisable()
{
- m_EventSystem.UpdateModules();
- base.OnDisable();
+ m_EventSystem.UpdateModules();// 把此input module移出EventSystem
+ base.OnDisable();
}
public abstract void Process();
diff --git a/Assets/uGUI-2017.1/UnityEngine.UI/EventSystem/InputModules/PointerInputModule.cs b/Assets/uGUI-2017.1/UnityEngine.UI/EventSystem/InputModules/PointerInputModule.cs
index d31f433..9103162 100644
--- a/Assets/uGUI-2017.1/UnityEngine.UI/EventSystem/InputModules/PointerInputModule.cs
+++ b/Assets/uGUI-2017.1/UnityEngine.UI/EventSystem/InputModules/PointerInputModule.cs
@@ -16,7 +16,7 @@ namespace UnityEngine.EventSystems
protected bool GetPointerData(int id, out PointerEventData data, bool create)
{
- if (!m_PointerData.TryGetValue(id, out data) && create)
+ if (!m_PointerData.TryGetValue(id, out data) && create) // 一个池子
{
data = new PointerEventData(eventSystem)
{
@@ -33,29 +33,33 @@ namespace UnityEngine.EventSystems
m_PointerData.Remove(data.pointerId);
}
+ //c 根据touch数据得到pointEventData,并且保存raycaster结果
protected PointerEventData GetTouchPointerEventData(Touch input, out bool pressed, out bool released)
{
PointerEventData pointerData;
- var created = GetPointerData(input.fingerId, out pointerData, true);
+ var created = GetPointerData(input.fingerId, out pointerData, true); // 拿到存储结构,返回是否是新建结构
pointerData.Reset();
- pressed = created || (input.phase == TouchPhase.Began);
- released = (input.phase == TouchPhase.Canceled) || (input.phase == TouchPhase.Ended);
+ // pressed和released用来判断抬起或者放下
- if (created)
+ pressed = created || (input.phase == TouchPhase.Began); // 如果结构是新建的或input phase是bagan,说明是刚按下
+ released = (input.phase == TouchPhase.Canceled) || (input.phase == TouchPhase.Ended); // 如果phase是这两种,说明是抬起来了
+
+ if (created)//如果是新建的,记录下起始位置,否则还保留旧值,下面用来计算delta
pointerData.position = input.position;
- if (pressed)
+ if (pressed)//如果是刚按下,delta置为0
pointerData.delta = Vector2.zero;
else
- pointerData.delta = input.position - pointerData.position;
+ pointerData.delta = input.position - pointerData.position; // 计算delta
- pointerData.position = input.position;
+ pointerData.position = input.position; // 计算完delta后覆盖
- pointerData.button = PointerEventData.InputButton.Left;
+ pointerData.button = PointerEventData.InputButton.Left; // 这里不知道为什么
- eventSystem.RaycastAll(pointerData, m_RaycastResultCache);
+ // 用这个触摸数据做射线检测,检测结果在m_RaycastResultCache
+ eventSystem.RaycastAll(pointerData, m_RaycastResultCache);
var raycast = FindFirstRaycast(m_RaycastResultCache);
pointerData.pointerCurrentRaycast = raycast;
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;
}
diff --git a/Assets/uGUI-2017.1/UnityEngine.UI/EventSystem/InputModules/TouchInputModule.cs b/Assets/uGUI-2017.1/UnityEngine.UI/EventSystem/InputModules/TouchInputModule.cs
index 4f04f42..58ac97c 100644
--- a/Assets/uGUI-2017.1/UnityEngine.UI/EventSystem/InputModules/TouchInputModule.cs
+++ b/Assets/uGUI-2017.1/UnityEngine.UI/EventSystem/InputModules/TouchInputModule.cs
@@ -4,13 +4,15 @@ using UnityEngine.Serialization;
namespace UnityEngine.EventSystems
{
- [Obsolete("TouchInputModule is no longer required as Touch input is now handled in StandaloneInputModule.")]
+ //c TouchInputModule被废弃了,统一在standaloneInputModule中处理
+ [Obsolete("TouchInputModule is no longer required as Touch input is now handled in StandaloneInputModule.")]
[AddComponentMenu("Event/Touch Input Module")]
public class TouchInputModule : PointerInputModule
{
protected TouchInputModule()
{}
+ // 触摸点位置
private Vector2 m_LastMousePosition;
private Vector2 m_MousePosition;
@@ -31,6 +33,7 @@ namespace UnityEngine.EventSystems
set { m_ForceModuleActive = value; }
}
+ //c 更新鼠标(触摸点)位置
public override void UpdateModule()
{
m_LastMousePosition = m_MousePosition;