UGUI学习手记-EventSystem
来源:互联网 发布:淘宝有没有死店一说 编辑:程序博客网 时间:2024/06/05 23:56
- 关于源码
- EventSystem的职责
- 本质
- 代码分析
- EventSystem 的唯一性
关于源码
关于源码可以参考这篇博客下载或调试。
EventSystem的职责
本质
实际上 EventSystem 处理和管理的其实是点击、键盘输入、触摸等事件,其实就是输入事件系统。
代码分析
EventSystem 最开始声明了一个 List 和一个变量
系统输入模块表
private List<BaseInputModule> m_SystemInputModules = new List<BaseInputModule>();
当前输入模块
private BaseInputModule m_CurrentInputModule;
BaseInputModule 是一个抽象类,PointerInputModule 继承自它,同样也是抽象类,而StandaloneInputModule 和 TouchInputModule 又继承自 PointerInputModule 。StandaloneInputModule 是面向“PC, Mac& Linux Standalone”这个平台的输入模块,而TouchInputModule 是面向“iOS”、“Android”等可触摸移动平台的输入模块。
EventSystem 会 每一帧 都处理这些输入模块
调用 TickModules 方法,更新每一个InputModule 。
遍历 m_SystemInputModules 判断这些 module 是否支持 当前的平台(例如StandaloneInputModule 需要判断是否鼠标已连接,而 TouchInputModule 需要判断是否支持触摸)且 module 是否可激活(细节参考 StandaloneInputModule 和 TouchInputModule 的源码)。
如果有符合条件的 module 便赋值给 m_CurrentInputModule (当前输入模块)并 break 。
如果没有符合条件的 module 便选取第一个支持当前平台的 module 作为 m_CurrentInputModule 。最后如果 m_CurrentInputModule 的值变化了并且
m_CurrentInputModule != null
便调用:m_CurrentInputModule.Process()
;将各种输入事件(如点击、拖拽等事件)传递给 EventSystem 当前选中的 GameObject(m_CurrentSelected)。m_CurrentSelected 大部分情况是 Selectable 组件(继承自它的有 Button 、Dropdown 、InputField 等组件)设置的。设置 m_CurrentSelected 的时候,会通过 ExecuteEvents 这个类对之前的对象执行一个被取消事件,且对新选中的对象执行一个被选中事件。这就是 OnSelect 和 OnDeselect 两个方法的由来。
- EventSystem 的 RaycastAll 方法使用射线从相机到某个点(设为点 E )照射在 UI 上,然后对被照射到的所有对象进行排序,大致是远近排序。
而这个方法是在 PointerInputModule 中使用的,如果点击(或触摸)事件移动的时候,被该事件影响的对象也会发生变化,通过 RaycastAll 方法(传入的 eventData 中的 position 属性作为点 E)获得到第一个被射线照射到的对象,如果与之前的对象不同,便变更对象(同时会回调原对象的 OnPointerExit 和新对象的 OnPointerEnter 方法)。
- IsPointerOverGameObject 是 EventSystem 类里 特别常用 的一个方法,用于判断是否点击在UI上,具体是在 PointerInputModule 中实现的。简单来说,就是 判断当前是否存在某个键位(默认是-1鼠标左键)的数据 。
EventSystem 的唯一性
EventSystem 有一个 static 属性:
public static EventSystem current { get; set; }
所以只存在一个current,而并不会每个实例对象会有一个。
当一个EventSystem组件OnEnable的时候会将这个对象赋值给current。
OnDisable的时候会将current赋值为null(当current==this)。
细节代码如下
protected override void OnEnable() { base.OnEnable(); if (EventSystem.current == null) EventSystem.current = this; #if UNITY_EDITOR else { Debug.LogWarning("Multiple EventSystems in scene... this is not supported"); } #endif }
protected override void OnDisable() { if (m_CurrentInputModule != null) { m_CurrentInputModule.DeactivateModule(); m_CurrentInputModule = null; } if (EventSystem.current == this) EventSystem.current = null; base.OnDisable(); }
- UGUI学习手记-EventSystem
- UGUI学习——EventSystem
- Unity3D之UGUI学习EventSystem
- 关于UGUI的EventSystem的学习
- UGUI学习手记-InputModule
- UGUI学习手记-CanvasUpdateRegistry
- UGUI学习手记-Graphic
- EventSystem---unity-uGUI
- (十九)unity4.6学习Ugui中文文档-------交互-EventSystem
- UGUI学习手记-ExecuteEvents & EventInterface
- UGUI学习手记-Selectable& Button
- UGUI学习手记-Toggle & ToggleGroup
- UGUI 事件响应之EventSystem
- Unity UGUI之Canvas&EventSystem
- (二十二)unity4.6学习Ugui中文文档-------交互-Eventsystem & Binding
- UGUI内核大探究(一)EventSystem
- UGUI内核大探究(一)EventSystem
- EventSystem
- 用户文件资料存取(千万级别用户资料文件夹简单保存)
- 1115. Counting Nodes in a BST (30) PAT 甲级
- 1021 Problem B按要求输出
- HMM
- [sql]truncate与delete的区别
- UGUI学习手记-EventSystem
- jQuery.cropper裁剪
- caffe中HDF5层及数据生成
- 2660 Problem C上直角三角形输出
- 144. Binary Tree Preorder Traversal
- wordpress get_terms()使用自定义字段meta_query筛选分类法
- POJ 3359 Wordfish 英文少
- c#数据结构之运行时间测试
- Manacher算法总结