Esempio n. 1
0
        public static void ShowAddActionMenu(Vector2 position, Action <Events.Action> action, UnityEngine.Object instance, bool acceptCoroutine = false)
        {
            List <ItemSelector.CustomItem> customItems = new List <ItemSelector.CustomItem>();
            var actions = GetActionMenus(acceptCoroutine);

            foreach (var a in actions)
            {
                var type = a.type;
                customItems.Add(new ItemSelector.CustomItem(a.name,
                                                            delegate() {
                    Events.Action act;
                    if (type.IsSubclassOf(typeof(Events.Action)))
                    {
                        act = ReflectionUtils.CreateInstance(type) as Events.Action;
                    }
                    else
                    {
                        act = new HLAction()
                        {
                            type = MemberData.CreateFromType(type)
                        };
                    }
                    action(act);
                }, a.category));
            }
            ItemSelector.SortCustomItems(customItems);
            FilterAttribute filter = new FilterAttribute()
            {
                InvalidTargetType = MemberData.TargetType.Values |
                                    MemberData.TargetType.Null |
                                    //MemberData.TargetType.Constructor |
                                    MemberData.TargetType.SelfTarget,
                MaxMethodParam = int.MaxValue,
                Instance       = true,
                VoidType       = true
            };
            ItemSelector w = ItemSelector.ShowWindow(instance, filter, (member) => {
                if (!(member.instance is UnityEngine.Object) && instance != null && !member.isStatic)
                {
                    Type startType = member.startType;
                    if (startType != null && member.instance == null)
                    {
                        if (instance.GetType().IsCastableTo(startType))
                        {
                            member.instance = instance;
                        }
                        else if (member.IsTargetingUNode)
                        {
                            if (member.instance == null)
                            {
                                member.instance = instance;
                            }
                        }
                        else if (instance is Component)
                        {
                            if (startType == typeof(GameObject))
                            {
                                member.instance = (instance as Component).gameObject;
                            }
                            else if (startType.IsSubclassOf(typeof(Component)))
                            {
                                member.instance = (instance as Component).GetComponent(startType);
                            }
                        }
                        else if (instance is GameObject)
                        {
                            if (startType == typeof(GameObject))
                            {
                                member.instance = instance as GameObject;
                            }
                            else if (startType.IsSubclassOf(typeof(Component)))
                            {
                                member.instance = (instance as GameObject).GetComponent(startType);
                            }
                        }
                        if (member.instance == null && ReflectionUtils.CanCreateInstance(startType))
                        {
                            member.instance = ReflectionUtils.CreateInstance(startType);
                        }
                    }
                    if (member.instance == null)
                    {
                        member.instance = MemberData.none;
                    }
                }
                switch (member.targetType)
                {
                case MemberData.TargetType.uNodeFunction:
                case MemberData.TargetType.Constructor:
                case MemberData.TargetType.Method: {
                    if (member.type != typeof(void))
                    {
                        GenericMenu menu = new GenericMenu();
                        menu.AddItem(new GUIContent("Invoke"), false, () => {
                                action(onAddGetAction(member));
                            });
                        menu.AddItem(new GUIContent("Compare"), false, () => {
                                action(onAddCompareAction(member));
                            });
                        menu.ShowAsContext();
                    }
                    else
                    {
                        action(onAddGetAction(member));
                    }
                    break;
                }

                case MemberData.TargetType.Field:
                case MemberData.TargetType.Property:
                case MemberData.TargetType.uNodeProperty:
                case MemberData.TargetType.uNodeVariable:
                case MemberData.TargetType.uNodeLocalVariable:
                case MemberData.TargetType.uNodeGroupVariable:
                case MemberData.TargetType.uNodeParameter: {
                    GenericMenu menu = new GenericMenu();
                    var members      = member.GetMembers();
                    if (members == null || members[members.Length - 1] == null || ReflectionUtils.CanGetMember(members[members.Length - 1]))
                    {
                        menu.AddItem(new GUIContent("Get"), false, () => {
                                action(onAddGetAction(member));
                            });
                    }
                    if (members == null || members[members.Length - 1] == null || ReflectionUtils.CanSetMember(members[members.Length - 1]))
                    {
                        menu.AddItem(new GUIContent("Set"), false, () => {
                                action(onAddSetAction(member));
                            });
                    }
                    menu.AddItem(new GUIContent("Compare"), false, () => {
                            action(onAddCompareAction(member));
                        });
                    menu.ShowAsContext();
                    break;
                }

                default:
                    throw new Exception("Unsupported target kind:" + member.targetType);
                }
            }, false, customItems).ChangePosition(position);

            w.displayNoneOption     = false;
            w.displayCustomVariable = false;
            w.customItems           = customItems;
        }