ExecuteEvents
这个类主要负责ugui事件的触发,事件触发时则通过下面这个函数进行调用
private static readonly ObjectPool<List<IEventSystemHandler>> s_HandlerListPool =
new ObjectPool<List<IEventSystemHandler>>(null, l => l.Clear()); //对象池
public static bool Execute<T>(GameObject target, BaseEventData eventData, EventFunction<T> functor) where T : IEventSystemHandler
{
// 1 声明一个List<IEventSystemHandler> 集合用来存放事件类
var internalHandlers = s_HandlerListPool.Get();
// 2 从指定的游戏对象身上获取所有事件存放在 internalHandlers中
GetEventList<T>(target, internalHandlers);
for (var i = 0; i < internalHandlers.Count; i++)
{
T arg;
try
{
arg = (T)internalHandlers[i];
}
catch (Exception e)
{
var temp = internalHandlers[i];
Debug.LogException(new Exception(string.Format("Type {0} expected {1} received.", typeof(T).Name, temp.GetType().Name), e));
continue;
}
try
{
// 3 依次调用对象身上的事件,EventFunction<T> functor 是一个泛型委托函数
functor(arg, eventData);
}
catch (Exception e)
{
Debug.LogException(e);
}
}
var handlerCount = internalHandlers.Count;
s_HandlerListPool.Release(internalHandlers);
return handlerCount > 0;
}
声明泛型委托类型方便根据不同事件类型对函数函数进行调用
1 声明不同类型的事件接口 2将同名函数绑定到这些事件接口上 3 通过静态属性来调用同名函数
private static readonly EventFunction<IPointerEnterHandler> s_PointerEnterHandler = Execute;
private static void Execute(IPointerEnterHandler handler, BaseEventData eventData)
{
handler.OnPointerEnter(ValidateEventData<PointerEventData>(eventData));
}
private static readonly EventFunction<IPointerExitHandler> s_PointerExitHandler = Execute;
private static void Execute(IPointerExitHandler handler,BaseEventData eventData)
{
handler.OnPointerExit(ValidateEventData<IPointerExitHandler>(eventData));
}
private static readonly EventFunction<IPointerDownHandler> s_PointerDownHandler = Execute;
private static void Execute(IPointerDownHandler handler,BaseEventData eventData)
{
handler.OnDownHandler(ValidateEventData<IPointerDownHandler>(eventData));
}
private static readonly EventFunction<IPointerUpHandler> s_PointerDownHandler = Execute;
private static void Execute(IPointerUpHandler handler,BaseEventData eventData)
{
handler.OnDownHandler(ValidateEventData<IPointerUpHandler>(eventData));
}
private static readonly EventFunction<IPointerClickHandler> s_PointerDownHandler = Execute;
private static void Execute(IPointerClickHandler handler,BaseEventData eventData)
{
handler.OnDownHandler(ValidateEventData<IPointerClickHandler>(eventData));
}
private static readonly EventFunction<IInitializePotentialDragHandler> s_InitializePotentialDragHandler = Execute;
private static void Execute(IInitializePotentialDragHandler handler, BaseEventData eventData)
{
handler.OnInitializePotentialDrag(ValidateEventData<PointerEventData>(eventData));
}
private static readonly EventFunction<IBeginDragHandler> s_BeginDragHandler = Execute;
private static void Execute(IBeginDragHandler handler, BaseEventData eventData)
{
handler.OnBeginDrag(ValidateEventData<PointerEventData>(eventData));
}
private static readonly EventFunction<IDragHandler> s_DragHandler = Execute;
private static void Execute(IDragHandler handler, BaseEventData eventData)
{
handler.OnDrag(ValidateEventData<PointerEventData>(eventData));
}
private static readonly EventFunction<IEndDragHandler> s_EndDragHandler = Execute;
private static void Execute(IEndDragHandler handler, BaseEventData eventData)
{
handler.OnEndDrag(ValidateEventData<PointerEventData>(eventData));
}
private static readonly EventFunction<IDropHandler> s_DropHandler = Execute;
private static void Execute(IDropHandler handler, BaseEventData eventData)
{
handler.OnDrop(ValidateEventData<PointerEventData>(eventData));
}
private static readonly EventFunction<IScrollHandler> s_ScrollHandler = Execute;
private static void Execute(IScrollHandler handler, BaseEventData eventData)
{
handler.OnScroll(ValidateEventData<PointerEventData>(eventData));
}
private static readonly EventFunction<IUpdateSelectedHandler> s_UpdateSelectedHandler = Execute;
private static void Execute(IUpdateSelectedHandler handler, BaseEventData eventData)
{
handler.OnUpdateSelected(eventData);
}
private static readonly EventFunction<ISelectHandler> s_SelectHandler = Execute;
private static void Execute(ISelectHandler handler, BaseEventData eventData)
{
handler.OnSelect(eventData);
}
private static readonly EventFunction<IDeselectHandler> s_DeselectHandler = Execute;
private static void Execute(IDeselectHandler handler, BaseEventData eventData)
{
handler.OnDeselect(eventData);
}
private static readonly EventFunction<IMoveHandler> s_MoveHandler = Execute;
private static void Execute(IMoveHandler handler, BaseEventData eventData)
{
handler.OnMove(ValidateEventData<AxisEventData>(eventData));
}
private static readonly EventFunction<ISubmitHandler> s_SubmitHandler = Execute;
private static void Execute(ISubmitHandler handler, BaseEventData eventData)
{
handler.OnSubmit(eventData);
}
private static readonly EventFunction<ICancelHandler> s_CancelHandler = Execute;
private static void Execute(ICancelHandler handler, BaseEventData eventData)
{
handler.OnCancel(eventData);
}
public delegate void EventFunction<T1>(T1 t1,BaseEventData data) where T:IEventSystemHander
//将不同的类型的事件接口绑定静态属性上
public static EventFunction<IPointerEnterHandler> pointerEnterHandler
{
get { return s_PointerEnterHandler; }
}
public static EventFunction<IPointerExitHandler> pointerExitHandler
{
get { return s_PointerExitHandler; }
}
public static EventFunction<IPointerDownHandler> pointerDownHandler
{
get { return s_PointerDownHandler; }
}
public static EventFunction<IPointerUpHandler> pointerUpHandler
{
get { return s_PointerUpHandler; }
}
public static EventFunction<IPointerClickHandler> pointerClickHandler
{
get { return s_PointerClickHandler; }
}
public static EventFunction<IInitializePotentialDragHandler> initializePotentialDrag
{
get { return s_InitializePotentialDragHandler; }
}
public static EventFunction<IBeginDragHandler> beginDragHandler
{
get { return s_BeginDragHandler; }
}
public static EventFunction<IDragHandler> dragHandler
{
get { return s_DragHandler; }
}
public static EventFunction<IEndDragHandler> endDragHandler
{
get { return s_EndDragHandler; }
}
public static EventFunction<IDropHandler> dropHandler
{
get { return s_DropHandler; }
}
public static EventFunction<IScrollHandler> scrollHandler
{
get { return s_ScrollHandler; }
}
public static EventFunction<IUpdateSelectedHandler> updateSelectedHandler
{
get { return s_UpdateSelectedHandler; }
}
public static EventFunction<ISelectHandler> selectHandler
{
get { return s_SelectHandler; }
}
public static EventFunction<IDeselectHandler> deselectHandler
{
get { return s_DeselectHandler; }
}
public static EventFunction<IMoveHandler> moveHandler
{
get { return s_MoveHandler; }
}
public static EventFunction<ISubmitHandler> submitHandler
{
get { return s_SubmitHandler; }
}
public static EventFunction<ICancelHandler> cancelHandler
{
get { return s_CancelHandler; }
}
调用实例
ExecuteEvents.Execute(pointerEvent.pointerPress, pointerEvent, ExecuteEvents.pointerUpHandler);
注意点
1 readonly 是一个运行时的只读字段,需要在实例化时对其进行赋值,赋值后不能被修改
2 声明不同的委托变量并将其绑定同名的函数,然后通过静态属性获取该委托函数
功能性函数
//获取指定对象的父对象并将其存放在eventChain中
private static void GetEventChain(GameObject root, IList<Transform> eventChain)
{
eventChain.Clear();
if (root == null)
return;
var t = root.transform;
while (t != null)
{
eventChain.Add(t);
t = t.parent;
}
}
//按照层级面板来执行事件
public static GameObject ExecuteHierarchy<T>(GameObject root, BaseEventData eventData, EventFunction<T> callbackFunction) where T : IEventSystemHandler
{
//找到指定对象的所有父节点每个对象都要执行该事件
GetEventChain(root, s_InternalTransformList);
for (var i = 0; i < s_InternalTransformList.Count; i++)
{
var transform = s_InternalTransformList[i];
if (Execute(transform.gameObject, eventData, callbackFunction))
return transform.gameObject;
}
return null;
}
//从指定对象上获取事件列表
private static void GetEventList<T>(GameObject go, IList<IEventSystemHandler> results) where T : IEventSystemHandler
{
if (results == null)
throw new ArgumentException("Results array is null", "results");
if (go == null || !go.activeInHierarchy)
return;
//从泛型对象池中取得指定实例
var components = ListPool<Component>.Get();
go.GetComponents(components);
for (var i = 0; i < components.Count; i++)
{
if (!ShouldSendToComponent<T>(components[i]))
continue;
results.Add(components[i] as IEventSystemHandler);
}
ListPool<Component>.Release(components);
}
//指定对象是否有指定的事件
public static bool CanHandleEvent<T>(GameObject go) where T : IEventSystemHandler
{
var internalHandlers = s_HandlerListPool.Get();
GetEventList<T>(go, internalHandlers);
var handlerCount = internalHandlers.Count;
s_HandlerListPool.Release(internalHandlers);
return handlerCount != 0;
}
//从指定对象身上找到指定的事件
public static GameObject GetEventHandler<T>(GameObject root) where T : IEventSystemHandler
{
if (root == null)
return null;
//这里使用while循环时如果找到满足条件的对象且返回该对象 这个循环本身应该不会听
Transform t = root.transform;
while (t != null)
{
if (CanHandleEvent<T>(t.gameObject))
return t.gameObject;
t = t.parent;
}
return null;
}
//判断事件接口是否满足条件
private static bool ShouldSendToComponent<T>(Component component) where T : IEventSystemHandler
{
var valid = component is T;//检测是否是事件接口的类
if (!valid)
return false;
//检测是否有Behaviour脚本如果有该脚本需要判断该脚本是否激活
var behaviour = component as Behaviour;
if (behaviour != null)
return behaviour.isActiveAndEnabled;
return true;
}
//判断指定事件类型是否是指定类型并且将其转换成目标类型
public static T ValidateEventData<T>(BaseEventData data) where T : class
{
if ((data as T) == null)
throw new ArgumentException(String.Format("Invalid type: {0} passed to event expecting {1}", data.GetType(), typeof(T)));
return data as T;
}