(二)UGUI源码分析之ExecuteEvents
更新时间:2020年2月17日11:02:54
静态类ExecuteEvents相关类群
内部定义委托:
- public delegate void EventFunction<T1>(T1 handler, BaseEventData eventData);
类成员:
-
public static T ValidateEventData<T>(BaseEventData data) where T : class
通过as强制转换,判断data是否可强转T,若可强转则转为T,否则报错抛出如下异常:
throw new ArgumentException(String.Format("Invalid type: {0} passed to event expecting {1}", data.GetType(), typeof(T)));
-
private static readonly EventFunction<IPointerEnterHandler> s_PointerEnterHandler = Execute;
public static EventFunction<IPointerEnterHandler> pointerEnterHandler 针对s_PointerEnterHandler封装的属性
静态委托对象成员s_PointerEnterHandler,指向如下方法:
private static void Execute(IPointerEnterHandler handler, BaseEventData eventData)
方法内调用IPointerEnterHandler接口的OnPointerEnter接口方法,将eventData利用ValidateEventData方法强转为PointerEventData对象再传入接口方法。
-
private static readonly EventFunction<IPointerExitHandler> s_PointerExitHandler = Execute;
public static EventFunction<IPointerExitHandler> pointerExitHandler 针对s_PointerExitHandler封装的属性
静态委托对象成员s_PointerExitHandler,指向如下方法:
private static void Execute(IPointerExitHandler handler, BaseEventData eventData)
方法内调用IPointerExitHandler接口的OnPointerExit接口方法,将eventData利用ValidateEventData方法强转为PointerEventData对象再传入接口方法。(其他接口同理,不在阐述)
-
private static void GetEventChain(GameObject root, IList<Transform> eventChain)
获取root物体和其所有父物体,存入eventChain(即一条物体关联链)
-
private static readonly ObjectPool<List<IEventSystemHandler>> s_HandlerListPool = new ObjectPool<List<IEventSystemHandler>>(null, l => l.Clear());
List<IEventSystemHandler>对象池,ObjectPool构造方法的参数1传入获取对象回调方法,参数2传入回收对象回调方法
-
private static bool ShouldSendToComponent<T>(Component component) where T : IEventSystemHandler
通过component is T来判断component脚本组件是否继承了T(IEventSystemHandler接口群体成员),满足继承且脚本**返回true,否则false
-
private static void GetEventList<T>(GameObject go, IList<IEventSystemHandler> results) where T : IEventSystemHandler
获取go物体身上的所有继承于T(IEventSystemHandler接口群体成员)的脚本(类)并存入results
1、判断go是否在InHierarchy面板**,若处于**继续执行,否则直接return
2、使用ListPool类静态方法Get方法,从列表对象池中获取一个组件列表List<Component>对象components
3、通过go.GetComponents(components)方法获取物体身上所有组件脚本
4、遍历获取到的组件脚本列表,通过ShouldSendToComponent方法判断脚本类是否继承于T,若继承则存入results列表,遍历结束就完成了【获取go物体身上的所有继承于T(IEventSystemHandler接口群体成员)的脚本(类)】
5、回收components
-
*public static bool Execute<T>(GameObject target, BaseEventData eventData, EventFunction<T> functor) where T : IEventSystemHandler
获取target物体身上所有继承于T(IEventSystemHandler接口群体成员)的脚本类,调用functor委托来执行脚本类实现的T接口方法,接口方法传入eventData
1、通过var internalHandlers = s_HandlerListPool.Get(); 从List<IEventSystemHandler>对象池中获取一个List<IEventSystemHandler>对象internalHandlers
2、调用GetEventList<T>(target, internalHandlers); 从target身上获取所有继承于T的脚本类存入internalHandlers
3、遍历internalHandlers (List<IEventSystemHandler>),将IEventSystemHandler成员强转为T类型, 然后执行functor委托:functor(T接口对象, eventData); functor委托会进行调用T接口的接口方法,接口方法传入eventData
4、回收internalHandlers
5、根据internalHandlers列表大于0则返回true,否则false
-
public static bool CanHandleEvent<T>(GameObject go) where T : IEventSystemHandler
判断go物体是否可处理T接口的事件
利用GetEventList获取go物体身上的所有继承于T的脚本类,若获取到了则返回true(表示可以处理事件),否则返回false
-
public static GameObject GetEventHandler<T>(GameObject root) where T : IEventSystemHandler
冒泡方式逐层往上(包括root自身)搜索出一个可处理T接口事件的物体,并返回这个物体。
利用CanHandleEvent方法判断物体是否可处理T接口事件,从自身开始判断,若自身不可处理,则找向父物体继续判断
-
private static readonly List<Transform> s_InternalTransformList = new List<Transform>(30);
一个上限为30的静态只读Transform列表
-
public static GameObject ExecuteHierarchy<T>(GameObject root, BaseEventData eventData, EventFunction<T> callbackFunction) where T : IEventSystemHandler
利用GetEventChain方法来获取root物体的物体关联链存入s_InternalTransformList,接着遍历s_InternalTransformList,执行Execute(s_InternalTransformList[i].gameObject, eventData, callbackFunction)即传递T接口消息给物体,若物体成功处理T消息,则返回该物体,否则继续遍历
希望以后我看回来的时候能理解我在说什么~