(三)UGUI ExecuteEvent

1.前言

ExecuteEvent是一个非常有用的类,方法都是静态的。在Unity事件系统中负责执行各个事件。

2.关键方法

1)Execute
方法:
public static bool Execute(GameObject target, BaseEventData eventData, EventFunction functor) where T : IEventSystemHandler
获取target上所有组件,如果组件是functor类型时(比如时PointerDown)则执行此方法。
2)ExecuteHierarchy
方法:
public static GameObject ExecuteHierarchy(GameObject root, BaseEventData eventData, EventFunction callbackFunction) where T : IEventSystemHandler
此方法向上查找然后执行相关方法,首先获取游戏物体root的所有父类,然后依次向上查找所有父类,执行Execute方法。比如当执行PointerDown事件时,如果pointerdown时对应的游戏物体没有PointerDown对应的事件,则查找父类执行PointerDown事件,直到事件执行或者查找结束。
3)GetEventHandler
根据提供的游戏物体,依次向上查找,获取可以执行相应事件的的游戏物物体

        public static GameObject GetEventHandler<T>(GameObject root) where T : IEventSystemHandler
        {
            if (root == null)
                return null;

            Transform t = root.transform;
            while (t != null)
            {
                if (CanHandleEvent<T>(t.gameObject))
                    return t.gameObject;
                t = t.parent;
            }
            return null;
        }

4)GetEventChain
往上依次获取所有的父类

        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;
            }
        

3.结语

此类主要时方法执行。

原文地址:https://www.cnblogs.com/llstart-new0201/p/12636219.html