コード例 #1
0
        private static void SetSceneCell(GridLayout grid, Transform parent, Vector3Int position, GameObject go, Vector3 offset, Vector3 scale, Quaternion orientation)
        {
            if (parent == null || go == null)
            {
                return;
            }

            GameObject instance = null;

            if (/*PrefabUtility.GetPrefabType(go)*/ PrefabUtility.GetPrefabAssetType(go) == /*PrefabType.Prefab*/ PrefabAssetType.Regular)
            {
                instance = (GameObject)PrefabUtility.InstantiatePrefab(go);
            }
            else
            {
                instance           = Instantiate(go);
                instance.hideFlags = HideFlags.None;
                instance.name      = go.name;
            }

            Undo.RegisterCreatedObjectUndo(instance, "Paint GameObject");
            instance.transform.SetParent(parent);
            instance.transform.position      = grid.LocalToWorld(grid.CellToLocalInterpolated(new Vector3Int(position.x, position.y, position.z) + new Vector3(.5f, .5f, .5f)));
            instance.transform.localRotation = orientation;
            instance.transform.localScale    = scale;
            instance.transform.Translate(offset);
        }
コード例 #2
0
        void CalculatePrefabStatus()
        {
            m_PlayModeObjects               = false;
            m_IsAsset                       = false;
            m_ImmutableSelf                 = false;
            m_ImmutableSourceAsset          = false;
            m_IsDisconnected                = false;
            m_IsMissing                     = false;
            m_IsPrefabInstanceAnyRoot       = true;
            m_IsPrefabInstanceOutermostRoot = true;
            m_AllOfSamePrefabType           = true;
            PrefabAssetType      firstType   = PrefabUtility.GetPrefabAssetType(targets[0]);
            PrefabInstanceStatus firstStatus = PrefabUtility.GetPrefabInstanceStatus(targets[0]);

            foreach (var o in targets)
            {
                var                  go     = (GameObject)o;
                PrefabAssetType      type   = PrefabUtility.GetPrefabAssetType(go);
                PrefabInstanceStatus status = PrefabUtility.GetPrefabInstanceStatus(go);
                if (type != firstType || status != firstStatus)
                {
                    m_AllOfSamePrefabType = false;
                }

                if (Application.IsPlaying(go))
                {
                    m_PlayModeObjects = true;
                }
                if (!PrefabUtility.IsAnyPrefabInstanceRoot(go))
                {
                    m_IsPrefabInstanceAnyRoot = false; // Conservative is false if any is false
                }
                if (!m_IsPrefabInstanceAnyRoot || !PrefabUtility.IsOutermostPrefabInstanceRoot(go))
                {
                    m_IsPrefabInstanceOutermostRoot = false; // Conservative is false if any is false
                }
                if (PrefabUtility.IsPartOfPrefabAsset(go))
                {
                    m_IsAsset = true; // Conservative is true if any is true
                }
                if (m_IsAsset && PrefabUtility.IsPartOfImmutablePrefab(go))
                {
                    m_ImmutableSelf = true; // Conservative is true if any is true
                }
                GameObject originalSourceOrVariant = PrefabUtility.GetOriginalSourceOrVariantRoot(go);
                if (originalSourceOrVariant != null && PrefabUtility.IsPartOfImmutablePrefab(originalSourceOrVariant))
                {
                    m_ImmutableSourceAsset = true; // Conservative is true if any is true
                }
                if (PrefabUtility.IsDisconnectedFromPrefabAsset(go))
                {
                    m_IsDisconnected = true;
                }
                if (PrefabUtility.IsPrefabAssetMissing(go))
                {
                    m_IsMissing = true;
                }
            }
        }
コード例 #3
0
        void CachePrefabHeaderText(StageNavigationItem stage)
        {
            if (!stage.isPrefabStage)
            {
                return;
            }

            var prefabStage = stage.prefabStage;

            if (prefabStage == null)
            {
                return;
            }

            var prefabAssetPath = prefabStage.prefabAssetPath;

            m_PrefabHeaderContent      = new GUIContent();
            m_PrefabHeaderContent.text = System.IO.Path.GetFileNameWithoutExtension(prefabAssetPath);

            // Make room for version control overlay icons.
            // GUIStyles don't allow controlling the space between icon and text.
            // We could add spacing by splitting text and icon into two rects and two draw operations,
            // but just adding a space character is a lot simpler and ends up amounting to the same thing.
            // This is cached text so there is minimal overhead.
            if (VersionControl.Provider.isActive)
            {
                m_PrefabHeaderContent.text = " " + m_PrefabHeaderContent.text;
            }

            PrefabUtility.GetPrefabAssetType(prefabStage.prefabContentsRoot);
            m_PrefabHeaderContent.image = prefabStage.prefabFileIcon;
            if (!stage.prefabAssetExists)
            {
                m_PrefabHeaderContent.tooltip = L10n.Tr("Prefab asset has been deleted");
            }

            if (PrefabStageUtility.GetCurrentPrefabStage().HasSceneBeenModified())
            {
                m_PrefabHeaderContent.text += "*";
            }
        }
コード例 #4
0
        void CalculatePrefabStatus()
        {
            m_IsPrefabInstanceAnyRoot = false;
            m_IsAsset             = false;
            m_AllOfSamePrefabType = true;
            PrefabAssetType      firstType   = PrefabUtility.GetPrefabAssetType(targets[0]);
            PrefabInstanceStatus firstStatus = PrefabUtility.GetPrefabInstanceStatus(targets[0]);

            foreach (GameObject go in targets)
            {
                PrefabAssetType      type   = PrefabUtility.GetPrefabAssetType(go);
                PrefabInstanceStatus status = PrefabUtility.GetPrefabInstanceStatus(go);
                if (type != firstType || status != firstStatus)
                {
                    m_AllOfSamePrefabType = false;
                }

                if (PrefabUtility.IsAnyPrefabInstanceRoot(go))
                {
                    m_IsPrefabInstanceAnyRoot = true;
                }
                if (m_IsPrefabInstanceAnyRoot)
                {
                    m_IsPrefabInstanceOutermostRoot = PrefabUtility.IsOutermostPrefabInstanceRoot(go);
                }
                if (PrefabUtility.IsPartOfPrefabAsset(go))
                {
                    m_IsAsset = true;
                }
                if (m_IsAsset && PrefabUtility.IsPartOfImmutablePrefab(go))
                {
                    m_ImmutableSelf = true;
                }
                GameObject originalSourceOrVariant = PrefabUtility.GetOriginalSourceOrVariantRoot(go);
                if (originalSourceOrVariant != null && PrefabUtility.IsPartOfImmutablePrefab(originalSourceOrVariant))
                {
                    m_ImmutableSourceAsset = true;
                }
            }
        }
コード例 #5
0
        internal static UInt64 GetOrGenerateFileIDHint(UnityEngine.Object obj)
        {
            UInt64 fileID = Unsupported.GetFileIDHint(obj);

            if (fileID == 0)
            {
                // GenerateFileIDHint only work on saved nested prefabs instances.
                var instanceHandle = PrefabUtility.GetPrefabInstanceHandle(obj);
                if (instanceHandle != null)
                {
                    bool isPrefabInstanceSaved = Unsupported.GetFileIDHint(instanceHandle) != 0;
                    if (isPrefabInstanceSaved && PrefabUtility.IsPartOfNonAssetPrefabInstance(obj) && PrefabUtility.GetPrefabAssetType(obj) != PrefabAssetType.MissingAsset)
                    {
                        fileID = Unsupported.GenerateFileIDHint(obj);
                    }
                }
            }

            return(fileID);
        }
コード例 #6
0
        private void DoPrefabButtons()
        {
            if (!m_IsPrefabInstanceAnyRoot || m_IsAsset)
            {
                return;
            }

            using (new EditorGUI.DisabledScope(m_PlayModeObjects))
            {
                EditorGUILayout.BeginHorizontal(Styles.prefabButtonsHorizontalLayout);

                // Prefab information
                PrefabAssetType      singlePrefabType     = PrefabUtility.GetPrefabAssetType(target);
                PrefabInstanceStatus singleInstanceStatus = PrefabUtility.GetPrefabInstanceStatus(target);
                GUIContent           prefixLabel;
                if (targets.Length > 1)
                {
                    prefixLabel = Styles.goTypeLabelMultiple;
                }
                else
                {
                    prefixLabel = Styles.goTypeLabel[(int)singlePrefabType, (int)singleInstanceStatus];
                }

                if (prefixLabel != null)
                {
                    EditorGUILayout.BeginHorizontal(GUILayout.Width(kIconSize + Styles.tagFieldWidth));
                    GUILayout.FlexibleSpace();
                    if (m_IsDisconnected || m_IsMissing)
                    {
                        GUI.contentColor = GUI.skin.GetStyle("CN StatusWarn").normal.textColor;
                        GUILayout.Label(prefixLabel, EditorStyles.whiteLabel, GUILayout.ExpandWidth(false));
                        GUI.contentColor = Color.white;
                    }
                    else
                    {
                        GUILayout.Label(prefixLabel, GUILayout.ExpandWidth(false));
                    }
                    EditorGUILayout.EndHorizontal();
                }

                if (!m_IsMissing)
                {
                    using (new EditorGUI.DisabledScope(targets.Length > 1))
                    {
                        if (singlePrefabType == PrefabAssetType.Model)
                        {
                            // Open Model Prefab
                            if (GUILayout.Button(Styles.openString, EditorStyles.miniButtonLeft))
                            {
                                GameObject asset = PrefabUtility.GetOriginalSourceOrVariantRoot(target);
                                AssetDatabase.OpenAsset(asset);
                                GUIUtility.ExitGUI();
                            }
                        }
                        else
                        {
                            // Open non-Model Prefab
                            using (new EditorGUI.DisabledScope(m_ImmutableSourceAsset))
                            {
                                if (GUILayout.Button(Styles.openString, EditorStyles.miniButtonLeft))
                                {
                                    GameObject asset = PrefabUtility.GetOriginalSourceOrVariantRoot(target);
                                    PrefabStageUtility.OpenPrefab(AssetDatabase.GetAssetPath(asset), (GameObject)target, StageNavigationManager.Analytics.ChangeType.EnterViaInstanceInspectorOpenButton);
                                    GUIUtility.ExitGUI();
                                }
                            }
                        }
                    }

                    // Select prefab
                    if (GUILayout.Button(Styles.selectString, EditorStyles.miniButtonRight))
                    {
                        HashSet <GameObject> selectedAssets = new HashSet <GameObject>();
                        for (int i = 0; i < targets.Length; i++)
                        {
                            GameObject prefabGo = PrefabUtility.GetOriginalSourceOrVariantRoot(targets[i]);

                            // Because of legacy prefab references we have to have this extra step
                            // to make sure we ping the prefab asset correctly.
                            // Reason is that scene files created prior to making prefabs CopyAssets
                            // will reference prefabs as if they are serialized assets. Those references
                            // works fine but we are not able to ping objects loaded directly from the asset
                            // file, so we have to make sure we ping the metadata version of the prefab.
                            var assetPath = AssetDatabase.GetAssetPath(prefabGo);
                            selectedAssets.Add((GameObject)AssetDatabase.LoadMainAssetAtPath(assetPath));
                        }

                        Selection.objects = selectedAssets.ToArray();
                        if (Selection.gameObjects.Length == 1)
                        {
                            EditorGUIUtility.PingObject(Selection.activeObject);
                        }
                    }

                    // Should be EditorGUILayout.Space, except it does not have ExpandWidth set to false.
                    // Maybe we can change that?
                    GUILayoutUtility.GetRect(6, 6, GUILayout.ExpandWidth(false));

                    // Reserve space regardless of whether the button is there or not to avoid jumps in button sizes.
                    Rect rect = GUILayoutUtility.GetRect(Styles.overridesContent, Styles.overridesDropdown);
                    if (m_IsPrefabInstanceOutermostRoot)
                    {
                        if (EditorGUI.DropdownButton(rect, Styles.overridesContent, FocusType.Passive))
                        {
                            if (targets.Length > 1)
                            {
                                PopupWindow.Show(rect, new PrefabOverridesWindow(targets.Select(e => (GameObject)e).ToArray()));
                            }
                            else
                            {
                                PopupWindow.Show(rect, new PrefabOverridesWindow((GameObject)target));
                            }
                            GUIUtility.ExitGUI();
                        }
                    }
                }
                EditorGUILayout.EndHorizontal();
            }
        }
コード例 #7
0
        private void DoPrefabButtons(GameObject go)
        {
            // @TODO: If/when we support multi-editing of prefab/model instances,
            // handle it here. Only show prefab bar if all are same type?
            if (!m_IsPrefabInstanceAnyRoot)
            {
                return;
            }

            using (new EditorGUI.DisabledScope(EditorApplication.isPlayingOrWillChangePlaymode && PrefabStageUtility.GetPrefabStage(go) == null))
            {
                EditorGUILayout.BeginHorizontal(s_Styles.prefabButtonsHorizontalLayout);

                // Prefab information
                PrefabAssetType      prefabType     = PrefabUtility.GetPrefabAssetType(go);
                PrefabInstanceStatus instanceStatus = PrefabUtility.GetPrefabInstanceStatus(go);
                GUIContent           prefixLabel;
                if (targets.Length > 1)
                {
                    prefixLabel = s_Styles.goTypeLabelMultiple;
                }
                else
                {
                    prefixLabel = s_Styles.goTypeLabel[(int)prefabType, (int)instanceStatus];
                }

                if (prefixLabel != null)
                {
                    EditorGUILayout.BeginHorizontal(GUILayout.Width(kIconSize + s_Styles.tagFieldWidth));
                    GUILayout.FlexibleSpace();
                    if (PrefabUtility.IsDisconnectedFromPrefabAsset(go) || PrefabUtility.IsPrefabAssetMissing(go))
                    {
                        GUI.contentColor = GUI.skin.GetStyle("CN StatusWarn").normal.textColor;
                        GUILayout.Label(prefixLabel, EditorStyles.whiteLabel, GUILayout.ExpandWidth(false));
                        GUI.contentColor = Color.white;
                    }
                    else
                    {
                        GUILayout.Label(prefixLabel, GUILayout.ExpandWidth(false));
                    }
                    EditorGUILayout.EndHorizontal();
                }

                if (targets.Length > 1)
                {
                    GUILayout.Label("Instance Management Disabled", s_Styles.instanceManagementInfo);
                }
                else
                {
                    if (!PrefabUtility.IsPrefabAssetMissing(go))
                    {
                        if (prefabType == PrefabAssetType.Model)
                        {
                            // Open Model Prefab
                            if (GUILayout.Button("Open", "MiniButtonLeft"))
                            {
                                GameObject asset = PrefabUtility.GetOriginalSourceOrVariantRoot(target);
                                AssetDatabase.OpenAsset(asset);
                                GUIUtility.ExitGUI();
                            }
                        }
                        else
                        {
                            // Open non-Model Prefab
                            using (new EditorGUI.DisabledScope(m_ImmutableSourceAsset))
                            {
                                if (GUILayout.Button("Open", "MiniButtonLeft"))
                                {
                                    GameObject asset = PrefabUtility.GetOriginalSourceOrVariantRoot(target);
                                    PrefabStageUtility.OpenPrefab(AssetDatabase.GetAssetPath(asset), (GameObject)target, StageNavigationManager.Analytics.ChangeType.EnterViaInstanceInspectorOpenButton);
                                    GUIUtility.ExitGUI();
                                }
                            }
                        }

                        // Select prefab
                        if (GUILayout.Button("Select", "MiniButtonRight"))
                        {
                            Selection.activeObject = PrefabUtility.GetOriginalSourceOrVariantRoot(target);

                            // Because of legacy prefab references we have to have this extra step
                            // to make sure we ping the prefab asset correctly.
                            // Reason is that scene files created prior to making prefabs CopyAssets
                            // will reference prefabs as if they are serialized assets. Those references
                            // works fine but we are not able to ping objects loaded directly from the asset
                            // file, so we have to make sure we ping the metadata version of the prefab.
                            var assetPath = AssetDatabase.GetAssetPath(Selection.activeObject);
                            Selection.activeObject = AssetDatabase.LoadMainAssetAtPath(assetPath);

                            EditorGUIUtility.PingObject(Selection.activeObject);
                        }

                        // Should be EditorGUILayout.Space, except it does not have ExpandWidth set to false.
                        // Maybe we can change that?
                        GUILayoutUtility.GetRect(6, 6, GUILayout.ExpandWidth(false));

                        // Reserve space regardless of whether the button is there or not to avoid jumps in button sizes.
                        Rect rect = GUILayoutUtility.GetRect(s_Styles.overridesContent, s_Styles.overridesDropdown);
                        if (m_IsPrefabInstanceOutermostRoot)
                        {
                            if (EditorGUI.DropdownButton(rect, s_Styles.overridesContent, FocusType.Passive))
                            {
                                PopupWindow.Show(rect, new PrefabOverridesWindow(go));
                                GUIUtility.ExitGUI();
                            }
                        }
                    }
                }
                EditorGUILayout.EndHorizontal();
            }
        }
コード例 #8
0
        internal static void CreateEmptyParent()
        {
            Transform[] selected                     = Selection.transforms;
            GameObject  defaultParentObject          = SceneView.GetDefaultParentObjectIfSet()?.gameObject;
            string      defaultParentObjectSceneGUID = defaultParentObject?.scene.guid;

            // Clear default parent object so we could always reparent and move the new parent to the scene we need
            if (defaultParentObject != null)
            {
                SceneHierarchy.ClearDefaultParentObject(defaultParentObjectSceneGUID);
            }

            // If selected object is a prefab, get the its root object
            if (selected.Length > 0)
            {
                for (int i = 0; i < selected.Length; i++)
                {
                    if (PrefabUtility.GetPrefabAssetType(selected[i].gameObject) != PrefabAssetType.NotAPrefab)
                    {
                        selected[i] = PrefabUtility.GetOutermostPrefabInstanceRoot(selected[i].gameObject).transform;
                    }
                }
            }

            // Selection.transform does not provide correct list order, so we have to do it manually
            selected = selected.ToList().OrderBy(g => g.GetSiblingIndex()).ToArray();

            GameObject go = ObjectFactory.CreateGameObject("GameObject");

            if (Selection.activeGameObject == null && Selection.gameObjects != null)
            {
                Selection.activeGameObject = Selection.gameObjects[0];
            }

            if (Selection.activeGameObject != null)
            {
                go.transform.position = Selection.activeGameObject.transform.position;
            }

            GameObject parent  = Selection.activeTransform != null ? Selection.activeTransform.gameObject : null;
            Transform  sibling = null;

            if (parent != null)
            {
                sibling = parent.transform;
                parent  = parent.transform.parent != null ? parent.transform.parent.gameObject : null;
            }

            Place(go, parent, false);
            var rectTransform = go.GetComponent <RectTransform>();

            // If new parent is RectTransform, make sure its position and size matches child rect transforms
            if (rectTransform != null && selected != null && selected.Length > 0)
            {
                CenterRectTransform(selected, rectTransform);
            }

            if (parent == null && sibling != null)
            {
                Undo.MoveGameObjectToScene(go, sibling.gameObject.scene, "Move To Scene");
            }

            if (parent == null && sibling == null)
            {
                go.transform.SetAsLastSibling();
            }
            else
            {
                go.transform.MoveAfterSibling(sibling, true);
            }

            // At this point, RecordStructureChange is already ongoing (from the CreateGameObject call).
            // We need to flush the stack to finalise the RecordStructureChange before any of following SetTransformParent calls takes place.
            Undo.FlushTrackedObjects();

            // Put gameObjects under a created parent
            if (selected.Length > 0)
            {
                foreach (var gameObject in selected)
                {
                    if (gameObject != null)
                    {
                        Undo.SetTransformParent(gameObject.transform, go.transform, "Reparenting");
                        gameObject.transform.SetAsLastSibling();
                    }
                }

                SceneHierarchyWindow.lastInteractedHierarchyWindow.SetExpanded(go.GetInstanceID(), true);
            }

            // Set back default parent object if we have one
            if (defaultParentObject != null)
            {
                SceneHierarchy.UpdateSessionStateInfoAndActiveParentObjectValuesForScene(defaultParentObjectSceneGUID, defaultParentObject.GetInstanceID());
            }
        }
コード例 #9
0
        internal static void CreateEmptyParent()
        {
            Transform[] selected = Selection.transforms;

            // If selected object is a prefab, get the its root object
            if (selected.Length > 0)
            {
                for (int i = 0; i < selected.Length; i++)
                {
                    if (PrefabUtility.GetPrefabAssetType(selected[i].gameObject) != PrefabAssetType.NotAPrefab)
                    {
                        selected[i] = PrefabUtility.GetOutermostPrefabInstanceRoot(selected[i].gameObject).transform;
                    }
                }
            }

            // Selection.transform does not provide correct list order, so we have to do it manually
            selected = selected.ToList().OrderBy(g => g.GetSiblingIndex()).ToArray();

            GameObject parent  = Selection.activeTransform != null ? Selection.activeTransform.gameObject : null;
            Transform  sibling = null;

            if (parent != null)
            {
                sibling = parent.transform;
                parent  = parent.transform.parent != null ? parent.transform.parent.gameObject : null;
            }

            GameObject go = ObjectFactory.CreateGameObject("GameObject");

            Place(go, parent);

            if (parent == null && sibling != null)
            {
                Undo.MoveGameObjectToScene(go, sibling.gameObject.scene, "Move To Scene");
            }

            if (parent == null && sibling == null)
            {
                go.transform.SetAsLastSibling();
            }
            else
            {
                go.transform.MoveAfterSibling(sibling, true);
            }

            // Put gameObjects under a created parent
            if (selected.Length > 0)
            {
                foreach (var gameObject in selected)
                {
                    if (gameObject != null)
                    {
                        Undo.SetTransformParent(gameObject.transform, go.transform, "Reparenting");
                        gameObject.transform.SetAsLastSibling();
                    }
                }

                SceneHierarchyWindow.lastInteractedHierarchyWindow.SetExpanded(go.GetInstanceID(), true);
            }
        }