예제 #1
0
        private void GetValidTileLayerComponents(
            SerializedProperty existingTypeList,
            out List <Type> types,
            out string[] typeNames)
        {
            // First, use Unity's "Unsupported" API to get a list of all valid Component types. The commands return only IDs of the
            // Components that are valid, i.e. are in a script with the correct corresponding filename, or come from an assembly. If
            // we were to rely only on reflected Types it's possible to get MonoBehaviors that are invalid to add from the Editor due
            // to not following these constraints. Filter to the ones which are just TileLayers...
            var commands = Unsupported.GetSubmenusCommands("Component");

            types = new List <Type>();
            for (var i = 0; i < commands.Length; i++)
            {
                var command = commands[i];
                if (command.StartsWith("SCRIPT"))
                {
                    var scriptIdString = command.Substring(6);
                    if (int.TryParse(scriptIdString, out var scriptId))
                    {
                        var script = EditorUtility.InstanceIDToObject(scriptId);
                        if (script is MonoScript monoScript)
                        {
                            var type = monoScript.GetClass();
                            if (type != null && type.IsSubclassOf(typeof(TLayer)))
                            {
                                types.Add(type);
                            }
                        }
                    }
                }
            }

            // Remove any types that are already in the list and have the DisallowMultipleComponent on them.
            for (var i = 0; i < existingTypeList.arraySize; ++i)
            {
                var objectReferenceValue = existingTypeList.GetArrayElementAtIndex(i).objectReferenceValue;
                if (objectReferenceValue != null)
                {
                    var elementType = objectReferenceValue.GetType();
                    if (elementType.GetCustomAttributes(typeof(DisallowMultipleComponent), true).Length > 0)
                    {
                        types.RemoveAll(type => type == elementType);
                    }
                }
            }

            // Build a list of the simple type name for display.
            typeNames = new string[types.Count];
            for (var i = 0; i < types.Count; ++i)
            {
                typeNames[i] = types[i].Name;
            }
        }
        private static List <KeyValuePair <string, string> > GetMenuDictionary()
        {
            var menus    = Unsupported.GetSubmenus("Component");
            var commands = Unsupported.GetSubmenusCommands("Component");

            var menuDictionary = new Dictionary <string, string>(menus.Length);

            for (var i = 0; i < menus.Length; i++)
            {
                menuDictionary.Add(menus[i], commands[i]);
            }
            return(menuDictionary.ToList());
        }
예제 #3
0
        static List <MenuItemData> GetSortedMenuItems(UnityEngine.GameObject[] targets)
        {
            var menus    = Unsupported.GetSubmenus("Component");
            var commands = Unsupported.GetSubmenusCommands("Component");

            var          menuItems       = new List <MenuItemData>(menus.Length);
            var          legacyMenuItems = new List <MenuItemData>(menus.Length);
            const string kLegacyString   = "legacy";

            var hasFilterOverride = ModeService.HasExecuteHandler("inspector_filter_component");

            for (var i = 0; i < menus.Length; i++)
            {
                var  menuPath = menus[i];
                bool isLegacy = menuPath.ToLower().Contains(kLegacyString);
                var  item     = new MenuItemData
                {
                    path     = menuPath,
                    command  = commands[i],
                    isLegacy = isLegacy
                };

                if (!hasFilterOverride || ModeService.Execute("inspector_filter_component", targets, menuPath))
                {
                    if (isLegacy)
                    {
                        legacyMenuItems.Add(item);
                    }
                    else
                    {
                        menuItems.Add(item);
                    }
                }
            }

            int comparison(MenuItemData x, MenuItemData y) => string.CompareOrdinal(x.path, y.path);

            menuItems.Sort(comparison);
            legacyMenuItems.Sort(comparison);

            menuItems.AddRange(legacyMenuItems);

            return(menuItems);
        }
예제 #4
0
        static List <MenuItemData> GetSortedMenuItems()
        {
            var menus    = Unsupported.GetSubmenus("Component");
            var commands = Unsupported.GetSubmenusCommands("Component");

            var          menuItems       = new List <MenuItemData>(menus.Length);
            var          legacyMenuItems = new List <MenuItemData>(menus.Length);
            const string kLegacyString   = "legacy";

            for (var i = 0; i < menus.Length; i++)
            {
                var  menuPath = menus[i];
                bool isLegacy = menuPath.ToLower().Contains(kLegacyString);
                var  item     = new MenuItemData
                {
                    path     = menuPath,
                    command  = commands[i],
                    isLegacy = isLegacy
                };

                if (isLegacy)
                {
                    legacyMenuItems.Add(item);
                }
                else
                {
                    menuItems.Add(item);
                }
            }

            int comparison(MenuItemData x, MenuItemData y) => string.CompareOrdinal(x.path, y.path);

            menuItems.Sort(comparison);
            legacyMenuItems.Sort(comparison);

            menuItems.AddRange(legacyMenuItems);

            return(menuItems);
        }