UGUI内核大探究(一)EventSystem

UGUI是Unity3D官方推出的UI系统,为了更好的使用UGUI,咱们就须要去了解它。

UGUI代码开源,咱们能够从bitbucket下载到源码ide

虽然Unity官方喜欢藏着掖着,例如RectTransform和Canvas系列的源码都放在了UnityEngine里面,可是这并不妨碍咱们对已开源的代码进行分析(其实多少也有些妨碍吧。。。)ui

今天咱们就探究了一下UGUI的事件系统EventSystem。this

虽然名字起得很大,可是实际上EventSystem处理和管理的实际上是点击、键盘输入、触摸等事件,因此应该成为输入事件系统InputEventSystem更为恰当。spa

咱们看到EventSystem最开始声明了一个List和一个变量.net

[csharp] view plain copy



private List<BaseInputModule> m_SystemInputModules = new List<BaseInputModule>();  
  
private BaseInputModule m_CurrentInputModule;  

 


系统输入模块表和当前输入模块。code

BaseInputModule是一个抽象类,PointerInputModule继承自它,一样也是抽象类,而StandaloneInputModule和TouchInputModule又继承自PointerInputModule。StandaloneInputModule是面向“PC, Mac& Linux Standalone”这个平台的输入模块,而TouchInputModule是面向“iOS”、“Android”等可触摸移动平台的输入模块。orm

EventSystem会每一帧都处理这些输入模块,首先是调用TickModules方法,更新每个InputModule。对象

而后遍历m_SystemInputModules判断这些module是否支持当前的平台(例如StandaloneInputModule须要判断是否鼠标已链接,而TouchInputModule须要判断是否支持触摸)且module是否可激活(可激活条件祥参StandaloneInputModule和TouchInputModule的源码)。blog

若是有符合条件的module便赋值给m_CurrentInputModule(当前输入模块)并break。排序

若是没有符合条件的module便选取第一个支持当前平台的module做为m_CurrentInputModule。

最后若是m_CurrentInputModule的值变化了而且m_CurrentInputModule不为null便调用:

而这个方法会将各类输入事件(如点击、拖拽等事件)传递给EventSystem当前选中的GameObject(m_CurrentSelected)。

[csharp] view plain copy



m_CurrentInputModule.Process();  

而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中实现的,之后咱们研究UGUI的输入模块的时候会深刻讲解,归纳一下就是判断当前是否存在某个键位(默认是-1鼠标左键)的数据。

最后咱们注意到EventSystem有一个static属性:

[csharp] view plain copy



public static EventSystem current { get; set; }  


由于是静态属性,因此只存在一个current,而并不会每一个实例对象会有一个.

 

当一个EventSystem组件OnEnable的时候会将这个对象赋值给current。参考OnEnabled与OnDisabled

[csharp] view plain copy



        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  
        }  


OnDisable的时候会将current赋值为null(当current==this)。参考OnEnabled与OnDisabled

 

[csharp] view plain copy



protected override void OnDisable()  
{  
    if (m_CurrentInputModule != null)  
    {  
        m_CurrentInputModule.DeactivateModule();  
        m_CurrentInputModule = null;  
    }  
  
    if (EventSystem.current == this)  
        EventSystem.current = null;  
  
    base.OnDisable();  
}  


这就是为何咱们在Unity Editor里面增长多个EventSystem的时候会报警告。

 

固然在游戏运行的时候,咱们也不能随意的增长删除EventSystem,一来彷佛并无什么必要,二来虽然EventSystem自己并无太多东西,可是毕竟输入模块里面仍是保存了一些数据的。

相关文章
相关标签/搜索