Example #1
0
        private static void HandlePrefabOrPrefabInstanceStateLocking(HierarchyFolder hierarchyFolder, bool isPrefabAsset)
        {
            var transform = hierarchyFolder.transform;

            transform.hideFlags = HideFlags.NotEditable;

            var gameObject = transform.gameObject;

            if (gameObject.layer != DefaultLayer)
            {
                gameObject.layer = DefaultLayer;
            }

            hierarchyFolder.hideFlags = HideFlags.HideInInspector;

            if (!isPrefabAsset)
            {
                return;
            }

            if (HierarchyFolderUtility.HasSupernumeraryComponents(hierarchyFolder))
            {
                HierarchyFolderUtility.UnmakeHierarchyFolder(gameObject, hierarchyFolder);
                return;
            }

            HierarchyFolderUtility.ResetTransformStateWithoutAffectingChildren(transform);
        }
        private void OnSceneGUI(SceneView sceneView)
        {
            var eventType = Event.current.type;

            // Reset hierarchy folder transform states immediately on mouse up in case user
            // just finished moving a hierarchy folder transform in the hierarchy view.
            switch (eventType)
            {
            case EventType.MouseDown:
                                #if DISABLE_TRANSFORM_GIZMOS
                if (Tools.current != Tool.Move && Tools.current != Tool.Transform && Tools.current != Tool.Rotate && Tools.current != Tool.Scale && Tools.current != Tool.Rect)
                {
                    break;
                }
                var selected = Selection.gameObjects;
                for (int n = selected.Length - 1; n >= 0; n--)
                {
                    if (selected[n].IsHierarchyFolder())
                    {
                        editorToolTempDisabled = Tools.current;
                        Tools.current          = Tool.View;
                        break;
                    }
                }
                break;
                                #endif
            case EventType.MouseUp:
            case EventType.MouseLeaveWindow:
                                #if DISABLE_TRANSFORM_GIZMOS
                if (editorToolTempDisabled != Tool.None)
                {
                    Tools.current          = editorToolTempDisabled;
                    editorToolTempDisabled = Tool.None;
                }
                break;
                                #endif
            case EventType.DragExited:
            case EventType.DragPerform:
            case EventType.Ignore:
            case EventType.Used:
            case EventType.KeyDown:
            case EventType.KeyUp:
                                        #if DEV_MODE && DEBUG_ON_SCENE_GUI
                Debug.Log("HierarchyFolderManager.OnSceneGUI(" + Event.current.type + ")");
                                        #endif
                break;

            default:
                return;
            }

            hierarchyFolders.RemoveAll(isNull);
            hierarchyFolders.Sort(compareHierarchyDepth);

            for (int n = hierarchyFolders.Count - 1; n >= 0; n--)
            {
                HierarchyFolderUtility.ResetTransformStateWithoutAffectingChildren(hierarchyFolders[n].transform);
            }
        }
        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);
        }
        internal void OnHierarchyChangedShared(HierarchyFolder hierarchyFolder)
        {
            if (HierarchyFolderUtility.HasSupernumeraryComponents(hierarchyFolder))
            {
                // Prevent warning message being logged multiple times.
                if (!destroying.Add(hierarchyFolder))
                {
                    return;
                }

                Debug.LogWarning("Hierarchy Folder \"" + hierarchyFolder.name + "\" contained extraneous components.\nThis is not supported since Hierarchy Folders are stripped from builds. Converting into a normal GameObject now.", hierarchyFolder.gameObject);

                                #if DEV_MODE
                foreach (var component in hierarchyFolder.gameObject.GetComponents <Component>())
                {
                    Debug.Log(component.GetType().Name);
                }
                                #endif

                TurnIntoNormalGameObject(hierarchyFolder);
            }

            HierarchyFolderUtility.ResetTransformStateWithoutAffectingChildren(hierarchyFolder.transform);
        }
Example #5
0
        protected override void OnHeaderGUI()
        {
            if (!staticSetupDone)
            {
                StaticSetup();
            }

            if (defaultEditor == null)
            {
                defaultEditor = CreateEditor(targets, defaultEditorType);
                if (defaultEditor == null)
                {
                    return;
                }
            }

            var    gameObject = targets.Length == 0 ? null : targets[0] as GameObject;
            bool   isHierarchyFolder;
            string tagWas;
            int    layerWas;

            if (gameObject != null)
            {
                tagWas            = gameObject.tag;
                layerWas          = gameObject.layer;
                isHierarchyFolder = gameObject.IsHierarchyFolder();
            }
            else
            {
                isHierarchyFolder = false;
                tagWas            = null;
                layerWas          = 0;
            }

            defaultOnHeaderGUI.Invoke(defaultEditor, null);

            if (!isHierarchyFolder || gameObject == null)
            {
                return;
            }

                        #if POWER_INSPECTOR
            if (InspectorUtility.NowDrawingInspectorPart != InspectorPart.None)
            {
                return;
            }
                        #endif

            if (!gameObject.CompareTag(tagWas))
            {
                if (EditorUtility.DisplayDialog("Change Tag", "Do you want to set tag to " + gameObject.tag + " for all child objects?", "Yes, change children", "Cancel"))
                {
                    gameObject.SetTagForAllChildren(gameObject.tag);
                }
                gameObject.tag = "Untagged";
            }

            if (gameObject.layer != layerWas)
            {
                gameObject.layer = layerWas;
            }

            bool isHierarchyFolderPrefab = gameObject.IsPrefabAssetOrOpenInPrefabStage();
            var  label = isHierarchyFolderPrefab ? prefabHierarchyFolderInfoLabel : hierarchyFolderInfoLabel;
            EditorGUILayout.LabelField(label, EditorStyles.helpBox);

            // Ensure the transform state is kept reset if users somehow manage to modify it for example through a custom transform editor.
            // The transform should have at least the NotEditable flag though, so this is unlikely to occur.
            for (int i = targets.Length - 1; i >= 0; i--)
            {
                gameObject = targets[i] as GameObject;
                if (gameObject == null)
                {
                    continue;
                }

                var  transform         = gameObject.transform;
                bool transformIsHidden = (transform.hideFlags & HideFlags.HideInInspector) == HideFlags.HideInInspector;
                if (transformIsHidden)
                {
                    continue;
                }

                bool transformNotEditable = (transform.hideFlags & HideFlags.NotEditable) == HideFlags.NotEditable;
                if (transformNotEditable)
                {
                    continue;
                }

                HierarchyFolderUtility.ResetTransformStateWithoutAffectingChildren(transform);
            }

            if (isHierarchyFolderPrefab)
            {
                return;
            }

            var labelRect = GUILayoutUtility.GetLastRect();

            var backgroundRect = labelRect;
            backgroundRect.x     += 0f;
            backgroundRect.y      = 5f;
            backgroundRect.width  = 36f;
            backgroundRect.height = 36f;
            EditorGUI.DrawRect(backgroundRect, backgroundColor);

            var folderIconRect = labelRect;
            folderIconRect.x     += 3f;
            folderIconRect.y      = 5f;
            folderIconRect.width  = 30f;
            folderIconRect.height = 30f;
            GUI.DrawTexture(folderIconRect, folderIcon);
        }