Esempio n. 1
0
        private static bool CreateHierarchyFolderFromMainMenuOrContextMenuEnabled(MenuCommand command)
        {
            bool prefabsAllowed = HierarchyFolderPreferences.Get().foldersInPrefabs != HierachyFoldersInPrefabs.NotAllowed;

            if (prefabsAllowed)
            {
                return(true);
            }

                        #if UNITY_2018_3_OR_NEWER
            if (PrefabStageUtility.GetCurrentPrefabStage() != null)
            {
                return(false);
            }
                        #endif

            var rightClickedGameObject = command == null ? null : command.context as GameObject;
            if (rightClickedGameObject != null && rightClickedGameObject.IsPrefabAsset())
            {
                return(false);
            }

            if (Selection.transforms.Length > 1)
            {
                var nonRootPrefabInstances = Selection.transforms.Where(t => t.gameObject.IsConnectedPrefabInstance() && !t.gameObject.IsPrefabInstanceRoot());
                if (nonRootPrefabInstances.Any())
                {
                    return(false);
                }
                return(true);
            }

            //return Selection.activeTransform == null || !Selection.activeTransform.gameObject.IsConnectedPrefabInstance();
            return(true);
        }
        private void ResubscribeToEvents(HierarchyFolderPreferences preferences)
        {
                        #if DEV_MODE && DEBUG_HIERARCHY_CHANGED
            Debug.Log("HierarchyFolderManager.ResubscribeToEvents");
                        #endif

            UnsubscribeToEvents(preferences);

            preferences.onPreferencesChanged -= OnPreferencesChanged;
            preferences.onPreferencesChanged += OnPreferencesChanged;

                        #if UNITY_2019_1_OR_NEWER
            SceneView.duringSceneGui += OnSceneGUI;
                        #else
            SceneView.onSceneGUIDelegate += OnSceneGUI;
                        #endif

            if (!EditorApplication.isPlayingOrWillChangePlaymode)
            {
                EditorApplication.hierarchyChanged += OnHierarchyChangedInEditMode;
            }
            else
            {
                if (preferences.playModeBehaviour == StrippingType.FlattenHierarchy)
                {
                    EditorApplication.hierarchyChanged += OnHierarchyChangedInPlayModeFlattened;
                }
                else
                {
                    EditorApplication.hierarchyChanged += OnHierarchyChangedInPlayModeGrouped;
                }
            }
        }
        private static void OnPostProcessScene()
        {
            // This will also get called when entering Playmode, when SceneManager.LoadScene is called,
            // but we only want to do stripping just after building the Scene.
            if (Application.isPlaying)
            {
                return;
            }

            var preferences = HierarchyFolderPreferences.Get();

            if (preferences == null)
            {
                Debug.LogWarning("Failed to find Hierarchy Folder Preferences asset; will not strip hierarchy folders from build.");
                return;
            }

            if (!preferences.removeFromScenes)
            {
                if (!preferences.warnWhenNotRemovedFromBuild || warnedAboutRemoveFromBuildDisabled)
                {
                    return;
                }

                warnedAboutRemoveFromBuildDisabled = true;
                if (EditorUtility.DisplayDialog("Warning: Hierarchy Folder Stripping Disabled", "This is a reminder that you have disabled stripping of hierarchy folders from builds. This will result in suboptimal performance and is not recommended when making a release build.", "Continue Anyway", "Enable Stripping"))
                {
                    return;
                }
            }

            HierarchyFolderUtility.ApplyStrippingTypeToAllLoadedScenes(StrippingType.FlattenHierarchyAndRemoveGameObject);
        }
Esempio n. 4
0
        private static void ProcessPrefabAtPath(string prefabPath)
        {
            var preferences = HierarchyFolderPreferences.Get();

            if (preferences == null)
            {
                EditorApplication.delayCall += () => ProcessPrefabAtPath(prefabPath);
                return;
            }

            if (preferences.foldersInPrefabs != HierachyFoldersInPrefabs.NotAllowed)
            {
                return;
            }

            var gameObject = AssetDatabase.LoadAssetAtPath <GameObject>(prefabPath);

            if (gameObject == null)
            {
                return;
            }

            if (!ContainsHierarchyFoldersInChildren(gameObject.transform))
            {
                return;
            }

            UnmakeHierarchyFoldersInChildren(gameObject.transform);
        }
Esempio n. 5
0
        public static void ResubscribeToEvents()
        {
            var preferences = HierarchyFolderPreferences.Get();

            if (!preferences.enableHierarchyIcons && !preferences.doubleClickSelectsChildrens)
            {
                return;
            }

            if (preferences.enableHierarchyIcons)
            {
                if (preferences.doubleClickSelectsChildrens)
                {
                    EditorApplication.hierarchyWindowItemOnGUI -= HandleDrawIconAndDoubleClickToSelectChildren;
                    EditorApplication.hierarchyWindowItemOnGUI += HandleDrawIconAndDoubleClickToSelectChildren;
                }
                else
                {
                    EditorApplication.hierarchyWindowItemOnGUI -= HandleDrawIcon;
                    EditorApplication.hierarchyWindowItemOnGUI += HandleDrawIcon;
                }
                EditorApplication.hierarchyChanged -= OnHierarchyChanged;
                EditorApplication.hierarchyChanged += OnHierarchyChanged;
            }
            else if (preferences.doubleClickSelectsChildrens)
            {
                EditorApplication.hierarchyWindowItemOnGUI -= HandleDoubleClickToSelectChildren;
                EditorApplication.hierarchyWindowItemOnGUI += HandleDoubleClickToSelectChildren;

                EditorApplication.hierarchyChanged -= OnHierarchyChanged;
                EditorApplication.hierarchyChanged += OnHierarchyChanged;
            }
        }
Esempio n. 6
0
        private static void ApplyPreferencesWhenAssetDatabaseReady()
        {
            if (!PreferencesApplier.ReadyToApplyPreferences())
            {
                EditorApplication.delayCall += ApplyPreferencesWhenAssetDatabaseReady;
                return;
            }

            var  classType   = typeof(HierarchyFolderMenuItems);
            var  preferences = HierarchyFolderPreferences.Get();
            bool enabled     = preferences.enableMenuItems;

            PreferencesApplier.ApplyPreferences(classType,
                                                new[] { "#define ENABLE_HIERARCHY_FOLDER_MENU_ITEMS" },
                                                new[] { enabled });

            preferences.onPreferencesChanged += (changedPreferences) =>
            {
                if (changedPreferences.enableMenuItems != enabled)
                {
                    var script = PreferencesApplier.FindScriptFile(classType);
                    if (script != null)
                    {
                        AssetDatabase.ImportAsset(AssetDatabase.GetAssetPath(script));
                    }
                                        #if DEV_MODE
                    else
                    {
                        Debug.LogWarning("Could not find script asset " + classType.Name + ".cs");
                    }
                                        #endif
                }
            };
        }
Esempio n. 7
0
        private static GameObject CreateHierarchyFolderInternal(bool useRectTransform)
        {
            string name   = HierarchyFolderPreferences.Get().defaultName;
            var    folder = useRectTransform ? new GameObject(name, typeof(RectTransform), typeof(HierarchyFolder)) : new GameObject(name, typeof(HierarchyFolder));

            return(folder);
        }
Esempio n. 8
0
        private void ResubscribeToHierarchyChanged(HierarchyFolderPreferences preferences)
        {
                        #if DEV_MODE && DEBUG_HIERARCHY_CHANGED
            Debug.Log(name + ".ResubscribeToHierarchyChanged");
                        #endif

            UnsubscribeToHierarchyChanged(preferences);

            preferences.onPreferencesChanged += ResubscribeToHierarchyChanged;

            if (!EditorApplication.isPlayingOrWillChangePlaymode)
            {
                EditorApplication.hierarchyChanged += OnHierarchyChangedInEditMode;
            }
            else
            {
                if (preferences.playModeBehaviour == StrippingType.FlattenHierarchy)
                {
                    EditorApplication.hierarchyChanged += OnHierarchyChangedInPlayModeFlattened;
                }
                else
                {
                    EditorApplication.hierarchyChanged += OnHierarchyChangedInPlayModeGrouped;
                }
            }
        }
Esempio n. 9
0
 private void UnsubscribeToHierarchyChanged(HierarchyFolderPreferences preferences)
 {
     preferences.onPreferencesChanged   -= ResubscribeToHierarchyChanged;
     EditorApplication.hierarchyChanged -= OnHierarchyChangedInEditMode;
     EditorApplication.hierarchyChanged -= OnHierarchyChangedInPlayModeFlattened;
     EditorApplication.hierarchyChanged -= OnHierarchyChangedInPlayModeGrouped;
 }
        private void Initialize()
        {
                        #if DEV_MODE
            Debug.Log("HierarchyFolderManager.Initialize() with HierarchyFolder.Manager " + (HierarchyFolder.Manager == this ? "==" : "!=") + " this");
                        #endif

            var preferences = HierarchyFolderPreferences.Get();
            if (preferences == null)
            {
                                #if DEV_MODE
                Debug.LogWarning("Preferences null. Can't initialize yet.");
                                #endif
                EditorApplication.delayCall += Initialize;
                return;
            }

            initialized = true;

            if (HierarchyFolder.Manager != this)
            {
                UnsubscribeToEvents(preferences);
                return;
            }

            ResubscribeToEvents(preferences);
        }
Esempio n. 11
0
        private void TurnIntoNormalGameObject()
        {
            UnsubscribeToHierarchyChanged(HierarchyFolderPreferences.Get());

            // Can help avoid NullReferenceExceptions via hierarchyChanged callback
            // by adding a delay between the unsubscribing and the destroying of the HierarchyFolder component
            EditorApplication.delayCall += UnmakeHierarchyFolder;
        }
        public static void ResetTransformStateWithoutAffectingChildren(Transform transform)
        {
            var gameObject = transform.gameObject;

            if (!gameObject.activeSelf)
            {
                var preferences = HierarchyFolderPreferences.Get();

                if (preferences.askAboutAllowInactiveHierarchyFolders)
                {
                    if (EditorUtility.DisplayDialog("Hierarchy Folder Active Flag Behaviour", "What would you like to happen when the active flag of a Hierarchy Folder is modified?\n\nTarget Hierarchy Folder:\nAdjust the active state of the Hierarchy Folder itself. This will have no effect in the final build since all Hierarchy Folders will be stripped even if inactive.\n\nTarget Children:\nModify active state of all child Objects and keep the Hierarchy Folder itself always active.\nThis is the recommended behaviour.\n\n(You can change your choice at any time in the preferences.)", "Target Hierarchy Folder", "Target Children"))
                    {
                        preferences.allowInactiveHierarchyFolders = true;
                    }
                    else
                    {
                        preferences.allowInactiveHierarchyFolders = false;
                    }
                    preferences.askAboutAllowInactiveHierarchyFolders = false;
                    preferences.SaveState();
                }

                if (!preferences.allowInactiveHierarchyFolders)
                {
                    Undo.RegisterFullObjectHierarchyUndo(gameObject, "Toggle Hierarchy Folder Children Active");

                    gameObject.SetActive(true);

                    if (transform.childCount > 0)
                    {
                        var children = ResetTransformStateReusableGameObjectsList;
                        gameObject.GetChildren(children, true);
                        int childCount = children.Count;
                        if (childCount > 0)
                        {
                            var  firstChild = children[0];
                            bool setActive  = !firstChild.activeSelf;

                            firstChild.SetActive(setActive);
                            for (int n = 1; n < childCount; n++)
                            {
                                children[n].SetActive(setActive);
                            }

                            ResetTransformStateReusableGameObjectsList.Clear();
                        }
                    }
                }
            }

            var rectTransform = transform as RectTransform;

            if (transform.localPosition != Vector3.zero || transform.localEulerAngles != Vector3.zero || transform.localScale != Vector3.one || (rectTransform != null && (rectTransform.anchorMin != Vector2.zero || rectTransform.anchorMax != Vector2.one || rectTransform.pivot != new Vector2(0.5f, 0.5f) || rectTransform.offsetMin != Vector2.zero || rectTransform.offsetMax != Vector2.zero)))
            {
                Undo.RegisterFullObjectHierarchyUndo(gameObject, "Reset Hierarchy Folder Transform");
                ForceResetTransformStateWithoutAffectingChildren(transform, false);
            }
        }
Esempio n. 13
0
 private static HierarchyFolderPreferences GetAsset()
 {
     if (asset == null)
     {
         var assetPath = GetProjectSettingsAssetPath(false);
         asset = AssetDatabase.LoadAssetAtPath <HierarchyFolderPreferences>(assetPath);
     }
     return(asset);
 }
        private void OnEnable()
        {
            icon = new GUIContent(HierarchyFolderPreferences.Get().GetDefaultIcon(18).closed, "Create Hierarchy Folder");
            EditorApplication.update += Update;
            var editorToolcontextType = typeof(EditorTool).Assembly.GetType("UnityEditor.EditorTools.EditorToolContext");

            getActiveTool       = editorToolcontextType.GetMethod("GetActiveTool", BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static);
            restorePreviousTool = editorToolcontextType.GetMethod("RestorePreviousTool", BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static);
        }
Esempio n. 15
0
        static HierarchyFolderIconDrawer()
        {
            var preferences = HierarchyFolderPreferences.Get();

            var icon = preferences.Icon(HierarchyIconType.Default);

            folderIconOpen   = icon.open;
            folderIconClosed = icon.closed;

            icon = preferences.Icon(HierarchyIconType.PrefabRoot);
            prefabFolderIconOpen   = icon.open;
            prefabFolderIconClosed = icon.closed;

            icon = preferences.Icon(HierarchyIconType.GameObjectAddition);
            gameObjectAdditionFolderIconOpen   = icon.open;
            gameObjectAdditionFolderIconClosed = icon.closed;

            icon = preferences.Icon(HierarchyIconType.PrefabAddition);
            prefabAdditionFolderIconOpen   = icon.open;
            prefabAdditionFolderIconClosed = icon.closed;

            icon = preferences.Icon(HierarchyIconType.PrefabVariantRoot);
            prefabVariantFolderIconOpen   = icon.open;
            prefabVariantFolderIconClosed = icon.closed;

            icon = preferences.Icon(HierarchyIconType.PrefabVariantAddition);
            prefabVariantAdditionFolderIconOpen   = icon.open;
            prefabVariantAdditionFolderIconClosed = icon.closed;

            EditorApplication.playModeStateChanged += OnPlayModeStateChanged;

            if (!preferences.enableHierarchyIcons && !preferences.doubleClickSelectsChildrens)
            {
                return;
            }

            if (preferences.enableHierarchyIcons)
            {
                if (preferences.doubleClickSelectsChildrens)
                {
                    EditorApplication.hierarchyWindowItemOnGUI += HandleDrawIconAndDoubleClickToSelectChildren;
                }
                else
                {
                    EditorApplication.hierarchyWindowItemOnGUI += HandleDrawIcon;
                }
            }
            else
            {
                EditorApplication.hierarchyWindowItemOnGUI += HandleDoubleClickToSelectChildren;
            }

            EditorApplication.hierarchyChanged += OnHierarchyChanged;

            UpdateExpandedIDs();
        }
 private static PlayModeStripper Instance()
 {
     if (instance == null)
     {
         var preferences = HierarchyFolderPreferences.Get();
         instance = new PlayModeStripper(preferences.playModeBehaviour, preferences.playModeStrippingMethod);
         preferences.onPreferencesChanged += instance.OnPreferencesChanged;
     }
     return(instance);
 }
Esempio n. 17
0
        private void Reset()
        {
            if (HasSupernumeraryComponents())
            {
                Debug.LogWarning("Can't convert GameObject with extraneous components into a Hierarchy Folder.");
                TurnIntoNormalGameObject();
                return;
            }

                        #if UNITY_2018_3_OR_NEWER
            if (PrefabUtility.GetPrefabAssetType(gameObject) != PrefabAssetType.NotAPrefab)
                        #else
            var prefabType = PrefabUtility.GetPrefabType(gameObject);
            if (prefabType == PrefabType.Prefab)
                        #endif
            {
                Debug.LogWarning("Can't convert prefabs into a Hierarchy Folder. Hierarchy Folders can only exist in the scene.");
                TurnIntoNormalGameObject();
                return;
            }

                        #if UNITY_2018_3_OR_NEWER
            if (PrefabUtility.GetPrefabInstanceStatus(gameObject) == PrefabInstanceStatus.Connected)
                        #else
            if (prefabType == PrefabType.PrefabInstance)
                        #endif
            {
                Debug.LogWarning("Can't add HierarchyFolder component to a prefab instance. You need to unpack the prefab instance first.");
                TurnIntoNormalGameObject();
                return;
            }

            ResetTransformStateWithoutAffectingChildren();

            transform.hideFlags = HideFlags.HideInInspector | HideFlags.NotEditable;

            hideFlags = HideFlags.HideInInspector | HideFlags.NotEditable;
            EditorUtility.SetDirty(transform);
            gameObject.isStatic = true;
            EditorUtility.SetDirty(this);
            var preferences = HierarchyFolderPreferences.Get();
            if (preferences.autoNameOnAdd)
            {
                if (gameObject.name.Equals("GameObject", StringComparison.Ordinal) || gameObject.name.StartsWith("GameObject (", StringComparison.Ordinal))
                {
                    gameObject.name = preferences.defaultName;
                }
                else
                {
                    ApplyNamingPattern();
                }
            }

            EditorUtility.SetDirty(gameObject);
        }
        public static void UnmakeHierarchyFolder([NotNull] GameObject gameObject, [CanBeNull] HierarchyFolder hierarchyFolder)
        {
                        #if DEV_MODE && DEBUG_UNMAKE_HIERARCHY_FOLDER
            Debug.Log("UnmakeHierarchyFolder(" + gameObject.name + ")");
                        #endif

            if (hierarchyFolder != null)
            {
                                #if UNITY_EDITOR
                                #if UNITY_2018_3_OR_NEWER
                if (!Application.isPlaying || UnityEditor.PrefabUtility.GetPrefabAssetType(gameObject) != UnityEditor.PrefabAssetType.NotAPrefab)
                                #else
                if (!Application.isPlaying || UnityEditor.PrefabUtility.GetPrefabType(gameObject) == UnityEditor.PrefabType.Prefab)
                                #endif
                {
                    Object.DestroyImmediate(hierarchyFolder, true);
                }
                else
                                #endif
                {
                    Object.Destroy(hierarchyFolder);
                }
            }

            var    preferences = HierarchyFolderPreferences.Get();
            string setName     = gameObject.name;
            string prefix      = preferences.namePrefix;
            if (prefix.Length > 0 && setName.StartsWith(prefix, StringComparison.Ordinal))
            {
                setName = setName.Substring(0);
            }
            string suffix = preferences.nameSuffix;
            if (suffix.Length > 0 && setName.EndsWith(setName, StringComparison.Ordinal))
            {
                setName = setName.Substring(0, setName.Length - suffix.Length);
            }
            if (preferences.forceNamesUpperCase && setName.Length > 1)
            {
                setName = setName[0] + setName.Substring(1).ToLower();
            }

            if (!string.Equals(setName, gameObject.name))
            {
                gameObject.name = setName;
                                #if UNITY_EDITOR
                UnityEditor.EditorUtility.SetDirty(gameObject);
                                #endif
            }

            var transform = gameObject.transform;
            transform.hideFlags = HideFlags.None;
                        #if UNITY_EDITOR
            UnityEditor.EditorUtility.SetDirty(transform);
                        #endif
        }
Esempio n. 19
0
        internal static void CreateHierarchyFolderParent()
        {
            int count = Selection.transforms.Length;

            var members = new Transform[count];

            Array.Copy(Selection.transforms, 0, members, 0, count);
            Array.Sort(members, SortByHierarchyOrder);

            var firstMember           = members[0];
            var hierarchyFolderParent = firstMember.parent;

            var folder = CreateHierarchyFolderInternal(firstMember is RectTransform);

            // if not all selected have the same parent, then create folders as last item in hierarchy
            for (int n = 1; n < count; n++)
            {
                if (members[n].parent != hierarchyFolderParent)
                {
                    hierarchyFolderParent = null;
                    break;
                }
            }

            if (hierarchyFolderParent != null)
            {
                folder.transform.UndoableSetParent(hierarchyFolderParent, "Hierarchy Folder Parent");
            }
            int hierarchyFolderSiblingIndex = firstMember.GetSiblingIndex();

            folder.transform.SetSiblingIndex(hierarchyFolderSiblingIndex);

            Undo.RegisterCreatedObjectUndo(folder, "Hierarchy Folder Parent");

                        #if UNITY_EDITOR
            if (EditorApplication.isPlayingOrWillChangePlaymode && HierarchyFolderPreferences.Get().playModeBehaviour == StrippingType.FlattenHierarchy)
            {
                int moveToIndex = HierarchyFolderUtility.GetLastChildIndexInFlatMode(folder);
                for (int n = count - 1; n >= 0; n--)
                {
                    Undo.SetTransformParent(members[n], hierarchyFolderParent, "Hierarchy Folder Parent");
                    members[n].SetSiblingIndex(moveToIndex);
                }
                return;
            }
                        #endif

            for (int n = 0; n < count; n++)
            {
                Undo.SetTransformParent(members[n], folder.transform, "Hierarchy Folder Parent");
                members[n].SetAsLastSibling();
            }

            Selection.activeGameObject = folder;
        }
        internal void OnReset(HierarchyFolder hierarchyFolder)
        {
            if (HierarchyFolderUtility.HasSupernumeraryComponents(hierarchyFolder))
            {
                Debug.LogWarning("Can't convert GameObject with extraneous components into a Hierarchy Folder.", hierarchyFolder.gameObject);
                TurnIntoNormalGameObject(hierarchyFolder);
                return;
            }

            var gameObject = hierarchyFolder.gameObject;

            if (HierarchyFolderPreferences.Get().foldersInPrefabs == HierachyFoldersInPrefabs.NotAllowed)
            {
                bool isPrefabInstance = gameObject.IsConnectedPrefabInstance();
                if (isPrefabInstance || gameObject.IsPrefabAssetOrOpenInPrefabStage())
                {
                    OnHierarchyFolderDetectedOnAPrefabAndNotAllowed(hierarchyFolder, isPrefabInstance);
                    return;
                }
            }

            var transform = hierarchyFolder.transform;

            HierarchyFolderUtility.ResetTransformStateWithoutAffectingChildren(transform);

            // Don't hide transform in prefabs or prefab instances to avoid internal Unity exceptions
            if (!gameObject.IsPrefabAssetOrInstance())
            {
                transform.hideFlags = HideFlags.HideInInspector | HideFlags.NotEditable;
            }
            else
            {
                transform.hideFlags = HideFlags.NotEditable;
            }
            hierarchyFolder.hideFlags = HideFlags.HideInInspector | HideFlags.NotEditable;
            EditorUtility.SetDirty(transform);
            gameObject.isStatic = true;
            EditorUtility.SetDirty(hierarchyFolder);
            var preferences = HierarchyFolderPreferences.Get();

            if (preferences.autoNameOnAdd)
            {
                if (gameObject.name.Equals("GameObject", StringComparison.Ordinal) || gameObject.name.StartsWith("GameObject (", StringComparison.Ordinal))
                {
                    gameObject.name = preferences.defaultName;
                }
                else
                {
                    ApplyNamingPattern(hierarchyFolder);
                }
            }

            EditorUtility.SetDirty(gameObject);
        }
        private void ResetInstance([CanBeNull] HierarchyFolderPreferences preferences)
        {
            if (instance != this || (preferences != null && preferences.playModeBehaviour == playModeStripping && preferences.playModeStrippingMethod == playModeStrippingMethod))
            {
                return;
            }

            SceneManager.sceneLoaded               -= OnSceneLoaded;
            SceneManager.sceneUnloaded             -= OnSceneUnloaded;
            EditorApplication.playModeStateChanged -= OnPlayModeStateChanged;
            instance = null;
        }
Esempio n. 22
0
        private void OnValidate()
        {
                        #if DEV_MODE && DEBUG_ON_VALIDATE
            Debug.Log(name + ".OnValidate");
                        #endif

            if (this == null)
            {
                return;
            }

            ResubscribeToHierarchyChanged(HierarchyFolderPreferences.Get());
        }
Esempio n. 23
0
 private static void ApplyPreferencesWhenAssetDatabaseReady()
 {
     if (!PreferencesApplier.ReadyToApplyPreferences())
     {
         EditorApplication.delayCall += ApplyPreferencesWhenAssetDatabaseReady;
     }
     else
     {
         PreferencesApplier.ApplyPreferences(typeof(HierarchyFolderExtensions),
                                             new[] { "#define HIERARCHY_FOLDER_EXTENSIONS_IN_GLOBAL_NAMESPACE" },
                                             new[] { HierarchyFolderPreferences.Get().extensionMethodsInGlobalNamespace });
     }
 }
        private void UnsubscribeToEvents(HierarchyFolderPreferences preferences)
        {
            preferences.onPreferencesChanged -= OnPreferencesChanged;

            EditorApplication.hierarchyChanged -= OnHierarchyChangedInEditMode;
            EditorApplication.hierarchyChanged -= OnHierarchyChangedInPlayModeFlattened;
            EditorApplication.hierarchyChanged -= OnHierarchyChangedInPlayModeGrouped;

                        #if UNITY_2019_1_OR_NEWER
            SceneView.duringSceneGui -= OnSceneGUI;
                        #else
            SceneView.onSceneGUIDelegate -= OnSceneGUI;
                        #endif
        }
        private void OnHierarchyChangedInEditMode()
        {
            hierarchyFolders.RemoveAll(isNull);
            hierarchyFolders.Sort(compareHierarchyDepth);

                        #if DEV_MODE && DEBUG_HIERARCHY_CHANGED
            Debug.Log("OnHierarchyChangedInEditMode with hierarchyFolders=" + hierarchyFolders.Count);
                        #endif

            bool prefabsNotAllowed = HierarchyFolderPreferences.Get().foldersInPrefabs == HierachyFoldersInPrefabs.NotAllowed;

            for (int n = 0, count = hierarchyFolders.Count - 1; n < count; n++)
            {
                var hierarchyFolder = hierarchyFolders[n];

                // Only process scene objects, not prefabs.
                if (!hierarchyFolder.gameObject.scene.IsValid())
                {
                    continue;
                }

                if (prefabsNotAllowed && hierarchyFolder.gameObject.IsConnectedPrefabInstance())
                {
                    OnHierarchyFolderDetectedOnAPrefabAndNotAllowed(hierarchyFolder, true);
                    count = hierarchyFolders.Count;
                    continue;
                }

                // If has RectTransform child convert Transform component into RectTransform
                // to avoid child RectTransform values being affected by the parent hierarchy folders.
                // For performance reasons only first child is checked.
                var transform = hierarchyFolder.transform;
                if (transform.GetFirstChild(true) is RectTransform && !(transform is RectTransform))
                {
                                        #if DEV_MODE
                    Debug.LogWarning("Converting Hierarchy Folder " + hierarchyFolder.name + " Transform into RectTransform because it had a RectTransform child.", hierarchyFolder);
                                        #endif

                    HierarchyFolderUtility.ForceResetTransformStateWithoutAffectingChildren(transform, true);
                }

                ApplyNamingPattern(hierarchyFolder);

                OnHierarchyChangedShared(hierarchyFolder);
            }

            hierarchyFolders.RemoveAll(isNull);
        }
Esempio n. 26
0
 private static PlayModeStripper Instance()
 {
     if (instance == null)
     {
         if (!EditorApplication.isPlayingOrWillChangePlaymode)
         {
             instance = new PlayModeStripper(StrippingType.None, default(PlayModeStrippingMethod));
         }
         else
         {
             var preferences = HierarchyFolderPreferences.Get();
             instance = new PlayModeStripper(preferences.playModeBehaviour, preferences.playModeStrippingMethod);
         }
     }
     return(instance);
 }
Esempio n. 27
0
        static HierarchyFolderIconDrawer()
        {
            var preferences = HierarchyFolderPreferences.Get();

            var icon = preferences.Icon();

            folderIconOpen   = icon.open;
            folderIconClosed = icon.closed;

            EditorApplication.playModeStateChanged += OnPlayModeStateChanged;

            if (!preferences.enableHierarchyIcons && !preferences.doubleClickSelectsChildrens)
            {
                return;
            }

            if (EditorApplication.isPlayingOrWillChangePlaymode)
            {
                switch (preferences.playModeBehaviour)
                {
                case StrippingType.RemoveComponent:
                case StrippingType.FlattenHierarchyAndRemoveComponent:
                case StrippingType.FlattenHierarchyAndRemoveGameObject:
                    return;
                }
            }

            if (preferences.enableHierarchyIcons)
            {
                if (preferences.doubleClickSelectsChildrens)
                {
                    EditorApplication.hierarchyWindowItemOnGUI += HandleDrawIconAndDoubleClickToSelectChildren;
                }
                else
                {
                    EditorApplication.hierarchyWindowItemOnGUI += HandleDrawIcon;
                }
            }
            else
            {
                EditorApplication.hierarchyWindowItemOnGUI += HandleDoubleClickToSelectChildren;
            }

            EditorApplication.hierarchyChanged += OnHierarchyChanged;

            UpdateExpandedIDs();
        }
        private void Initialize()
        {
            var preferences = HierarchyFolderPreferences.Get();

            if (preferences == null)
            {
                                #if DEV_MODE
                Debug.LogWarning("Preferences null. Can't initialize yet.");
                                #endif
                EditorApplication.delayCall += Initialize;
                return;
            }

            initialized = true;

            ResubscribeToEvents(preferences);
        }
Esempio n. 29
0
        public void SaveState()
        {
            if (IsDefaultState())
            {
                ClearSavedState();
            }
            else
            {
                if (forceNamesUpperCase)
                {
                    defaultName = defaultName.ToUpper();
                }

                if (!defaultName.StartsWith(namePrefix, StringComparison.Ordinal))
                {
                    for (int c = namePrefix.Length - 1; c >= 0 && !defaultName.StartsWith(namePrefix, StringComparison.Ordinal); c--)
                    {
                        defaultName = namePrefix[c] + defaultName;
                    }
                }

                if (!defaultName.EndsWith(nameSuffix, StringComparison.Ordinal))
                {
                    for (int c = 0, count = nameSuffix.Length; c < count && !defaultName.EndsWith(nameSuffix, StringComparison.Ordinal); c++)
                    {
                        defaultName += nameSuffix[c];
                    }
                }

                string serializedState = EditorJsonUtility.ToJson(this);
                EditorPrefs.SetString(EditorPrefsKey, serializedState);

                var assetPath = GetProjectSettingsAssetPath(true);

                asset    = null;
                instance = null;
                AssetDatabase.CreateAsset(Instantiate(this), assetPath);
                AssetDatabase.SaveAssets();
                AssetDatabase.Refresh();
            }

            if (onPreferencesChanged != null)
            {
                onPreferencesChanged(this);
            }
        }
Esempio n. 30
0
        private void Start()
        {
            var hierarchyFolders = FindObjectsOfType <HierarchyFolder>();
            int count            = hierarchyFolders.Length;
            var scene            = SceneManager.GetActiveScene();

                        #if UNITY_EDITOR
            if (count > 0)
            {
                var playModeBehaviour = HierarchyFolderPreferences.Get().playModeBehaviour;
                if (count > 0 && playModeBehaviour != StrippingType.None && playModeBehaviour != StrippingType.FlattenHierarchy)
                {
                    Debug.LogError(string.Format("Number of Hierarchy Folders in scene: {0}\nScene name: \"{1}\"", count, scene.name));
                }
            }
                        #if LOG_ZERO_COUNT
            else
            {
                Debug.Log(string.Format("<color=green>Number of Hierarchy Folders in scene: 0\nScene name: \"{0}\"</color>", scene.name));
            }
                        #endif
                        #else
            if (count > 0)
            {
                Debug.LogError(string.Format("Number of Hierarchy Folders in scene: {0}\nScene name: \"{1}\"", count, scene.name));
            }
                        #if LOG_ZERO_COUNT
            else
            {
                Debug.Log(string.Format("Number of Hierarchy Folders in scene: 0\nScene name: \"{0}\"", scene.name));
            }
                        #endif
                        #endif

                        #if LOG_ROOT_OBJECTS
            var sb = new System.Text.StringBuilder();
            foreach (var rootObject in scene.GetRootGameObjects())
            {
                sb.Append('\n');
                sb.Append(rootObject.name);
            }
            Debug.Log(string.Format("{0} root GameObjects: {1}", scene.name, sb.ToString()));
                        #endif
        }