private static void FindDependencies(string methodName)
        {
            List <EventReferenceInfo> depens       = UnityEventReferenceFinder.FindAllUnityEventsReferences();
            List <EventReferenceInfo> onlyWithName = new List <EventReferenceInfo>();

            foreach (EventReferenceInfo d in depens)
            {
                int[] indexes = d.MethodNames.Where(m => m.ToLower().Contains(methodName.ToLower())).Select(n => d.MethodNames.IndexOf(n)).ToArray();

                if (indexes.Length > 0)
                {
                    EventReferenceInfo info = new EventReferenceInfo();
                    info.Owner = d.Owner;

                    foreach (int i in indexes)
                    {
                        info.Listeners.Add(d.Listeners[i]);
                        info.MethodNames.Add(d.MethodNames[i]);
                    }

                    onlyWithName.Add(info);
                }
            }

            dependencies = onlyWithName.Count > 0 ? onlyWithName : depens;
        }
        private void DrawDependencies(EventReferenceInfo dependency, Vector2 position)
        {
            float width = drawableRect.width * leftColoumnRelativeWidth;

            EditorGUI.ObjectField(new Rect(position, new Vector2(width - tabulation, 16f)), dependency.Owner, typeof(MonoBehaviour), true);

            for (int i = 0; i < dependency.Listeners.Count; i++)
            {
                Vector2 positionRoot = position + Vector2.up * 16 + Vector2.up * 16 * i;
                EditorGUI.ObjectField(new Rect(positionRoot + Vector2.right * tabulation, new Vector2(width - tabulation, 16f)), dependency.Listeners[i], typeof(MonoBehaviour), true);

                Vector2 labelPosition = new Vector2(drawableRect.width * leftColoumnRelativeWidth + tabulation * 1.5f, positionRoot.y);
                EditorGUI.LabelField(new Rect(labelPosition, new Vector2(drawableRect.width * (1 - leftColoumnRelativeWidth) - tabulation / 1.5f, 16f)), dependency.MethodNames[i]);
            }
        }
Exemplo n.º 3
0
        public static List <EventReferenceInfo> FindAllUnityEventsReferences()
        {
            MonoBehaviour[] behaviours = Resources.FindObjectsOfTypeAll <MonoBehaviour>();
            Dictionary <MonoBehaviour, List <UnityEventBase> > events = new Dictionary <MonoBehaviour, List <UnityEventBase> >();

            foreach (MonoBehaviour b in behaviours)
            {
                TypeInfo         info  = b.GetType().GetTypeInfo();
                List <FieldInfo> evnts = info.DeclaredFields.Where(f => f.FieldType.IsSubclassOf(typeof(UnityEventBase))).ToList();

                foreach (FieldInfo e in evnts)
                {
                    if (!events.TryGetValue(b, out List <UnityEventBase> events_list))
                    {
                        events_list = new List <UnityEventBase>();

                        events.Add(b, events_list);
                    }

                    events_list.Add(e.GetValue(b) as UnityEventBase);
                }
            }

            List <EventReferenceInfo> infos = new List <EventReferenceInfo>();

            foreach (KeyValuePair <MonoBehaviour, List <UnityEventBase> > p in events)
            {
                foreach (UnityEventBase e in p.Value)
                {
                    int count = e.GetPersistentEventCount();
                    EventReferenceInfo info = new EventReferenceInfo();
                    info.Owner = p.Key;

                    for (int i = 0; i < count; i++)
                    {
                        Object obj    = e.GetPersistentTarget(i);
                        string method = e.GetPersistentMethodName(i);

                        info.Listeners.Add(obj);
                        info.MethodNames.Add(obj.GetType().Name.ToString() + "." + method);
                    }

                    infos.Add(info);
                }
            }

            return(infos);
        }