(一)UGUI源码分析之EventSystem

更新时间:2020年2月17日14:42:48

EventSystem相关类群

EventSystem类

类成员:

静态成员:

  • static List<EventSystem> m_EventSystems; 事件系统列表(场景上所有激活的事件系统)
  • static EventSystem current; 返回当前使用的事件系统(即m_EventSystems[0])

普通成员:

  • List<BaseInputModule> m_SystemInputModules; 输入模块列表
  • BaseInputModule m_CurrentInputModule; 当前使用的输入模块
  • BaseInputModule currentInputModule; 针对m_CurrentInputModule封装的属性(readonly)
  • GameObject m_FirstSelected; 第一个选中的物体

  • GameObject firstSelectedGameObject; 针对m_FirstSelected封装的属性
  • bool m_sendNavigationEvents = true; Should the EventSystem allow navigation events (move / submit / cancel).
  • bool sendNavigationEvents; 针对sendNavigationEvents封装的属性
  • int m_DragThreshold = 10; 拖动阈值?The soft area for dragging in pixels.
  • int pixelDragThreshold; 针对m_DragThreshold封装的属性
  • GameObject m_CurrentSelected; 当前选中的物体
  • GameObject currentSelectedGameObject; 针对m_CurrentSelected封装的属性
  • bool m_HasFocus = true; 是否聚焦?
  • bool isFocused; 针对m_HasFocus封装的属性(readonly)
  • bool m_SelectionGuard;  Returns true if the EventSystem is already in a SetSelectedGameObject.避免正在发送OnSelect或OnDeselect消息时再次发送消息。
  •  bool alreadySelecting; 针对m_SelectionGuard封装的属性
  • BaseEventData m_DummyData; 虚拟数据?等于new BaseEventData(this);
  • BaseEventData baseEventDataCache; 针对m_DummyData封装的属性

类方法:

  • protected EventSystem() {}
    保护类型EventSystem无参构造方法;
    无调用者;
    个人理解:防止外部new。

     
  • public void UpdateModules()
    官方描述:Recalculate the internal list of BaseInputModules.
    翻译: 重新计算BaseInputModules的内部列表。
    内部调用者:BaseInputMoudle的OnEnable()和OnDisable();
    个人理解:重新获取所有已激活的BaseInputModules。

     
  • *public void SetSelectedGameObject(GameObject selected, BaseEventData pointer)
    官方描述:Set the object as selected. Will send an OnDeselect the the old selected object and OnSelect to the new selected object.
    翻译:选中selected物体,且将发送一个OnDeselect消息给旧选中的物体和一个OnSelect消息给到新选中的物体。
    内部调用者:PointerInputModule的ClearSelection()、DeselectIfSelectionChanged(),StandaloneInputModule的
    ActivateModule()、Selectable的Navigate()、OnPointerDown()、InputFiled的OnPointerDown()等。
    注意:
    1、当正在进行发送OnDeselect或OnSelect消息时,进入该方法会直接报错:"Attempting to select " + selected +  "while already selecting an object."
    2、当前选中物体没有发生改变时,方法不生效(即不会发送消息).
    3、先对旧选中物体发送OnDeselect()消息,然后更新m_CurrentSelected字段为新选中物体selected,再发送OnSelect消息给新选中物体。

    发送OnDeselect消息和OnSelect消息代码行:
    ExecuteEvents.Execute(m_CurrentSelected, pointer, ExecuteEvents.deselectHandler);
    ExecuteEvents.Execute(m_CurrentSelected, pointer, ExecuteEvents.selectHandler);
    ExecuteEvents讲解链接:https://blog.csdn.net/qq_39574690/article/details/104354583

     
  • public void SetSelectedGameObject(GameObject selected)
    内部调用者:Selectable, Dropdown, InputField
    执行SetSelectedGameObject(selected, baseEventDataCache);
     
  • private static int RaycastComparer(RaycastResult lhs, RaycastResult rhs)
    个人理解:针对RaycastResult类的比较器

     
  • public void RaycastAll(PointerEventData eventData, List<RaycastResult> raycastResults)
    官方描述:Raycast into the scene using all configured BaseRaycasters.
    翻译:使用所有场景中的BaseRaycaster进行射线检测
    内部调用者:PointerInputModule的GetTouchPointerEventData()和GetMousePointerEventData()
    个人理解:所有已激活的BaseRaycaster会根据传入的eventData信息(如点击位置信息)来进行射线检测,将检测到的物体信息RaycastHit再次封装为RaycastResult存入raycastResults(隐式返回值raycastResults),最后会通过上方介绍的RaycasterComparer方法进行一个排序操作。

    相关内容解释链接:(后续更新!)

     
  • public bool IsPointerOverGameObject()
    官方解释:Is the pointer with the given ID over an EventSystem object?
    翻译:具有给定ID的指针是否在EventSystem对象上?
    个人理解:鼠标是否停留在物体身上?

     
  • *public bool IsPointerOverGameObject(int pointerId)
    适用情况:'OnMouseDown()'方法体{}内、'Input.GetMouseButtonDown(0)'符合此条件代码块{}内、'Input.GetTouch(0).phase == TouchPhase.Began'符合此条件代码块{}内。
    参数说明:int pointerId 在PC端固定默认输入-1代表鼠标左键指针ID、-2代表鼠标右键指针ID、-3代表鼠标滚轮指针ID、-4代表无触屏设备时的滑动指针ID,手游则输入struct UnityEngine.Touch对象的fingerId
    方法内实现时调用输入模块的IsPointerOverGameObject(int)方法进行检测的,可自行了解输入模块BaseInputModule该方法获知更详细的情况。

     
  • protected override void OnEnable()
    Monobehaviour生命周期方法OnEnable()激活物体时执行,方法内将事件系统自身放入事件系统静态列表。

     
  • protected override void OnDisable()
    Monobehaviour生命周期方法OnDisable()禁用物体时执行,方法内将事件系统自身从事件系统静态列表移除,并禁用当前输入模块(即调用输入模块(BaseInputModule)的DeactivateModule()方法)。

     
  • private void TickModules()
    遍历所有输入模块执行其UpdateModule方法
    相关内容解释链接:(后续更新!)

     
  • protected virtual void OnApplicationFocus(bool hasFocus)
    Monobehaviour声明周期方法OnApplicationFocus(bool)弹出(false)/返回(true)游戏时调用,更新m_HasFocus字段

     
  • *protected virtual void Update()
    1、判断当前使用的事件系统是否为自己,若不是则return
    2、调用TickModules方法
    3、遍历所有输入模块,当检查到一个输入模块为【可支持、可激活】时,再进行(检查其是否不等于当前输入模块,若不等于则进行更新当前输入模块为新的输入模块并退出遍历,若等于则直接退出遍历)。
    4、若检查到当前输入模块还是为空,进行遍历所有输入模块找到一个【可支持】的输入模块,将其作为当前输入模块。
    5、若当前帧没有进行过更新当前输入模块操作且当前输入模块不为空,则进行执行当前输入模块的Process()

    相关内容解释链接:(后续更新!)

     
  •  private void ChangeEventModule(BaseInputModule module)
    更新当前输入模块:1、将旧输入模块禁用(调用模块的DeactivateModule()方法)2、启用新输入模块(调用其ActivateModule()方法)。3、将m_CurrentInputModule=新输入模块。
    相关内容解释链接:(后续更新!)

     
  • public override string ToString()
    返回:"<b>Selected:</b>" + 当前选中物体名称 + 两个换行符 + (当前输入模块名称 or "No module")

 关于相关内容解释链接,还未补充,今晚可能补充了

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值