コード例 #1
0
        internal static void ApplyPrefabAddedGameObjects(object userData)
        {
            ObjectInstanceAndSourcePathInfo[] infos = (ObjectInstanceAndSourcePathInfo[])userData;

            GameObject[] gameObjects = new GameObject[infos.Length];
            for (int i = 0; i < infos.Length; i++)
            {
                ObjectInstanceAndSourcePathInfo info = infos[i];
                gameObjects[i] = info.instanceObject as GameObject;
            }

            if (!PrefabUtility.HasSameParent(gameObjects))
            {
                throw new ArgumentException(nameof(gameObjects), "ApplyPrefabAddedGameObjects requires that GameObjects share the same parent.");
            }

            if (!HasSameAssetPath(infos))
            {
                throw new ArgumentException(nameof(infos), "ApplyPrefabAddedGameObjects requires that GameObjects share the same parent asset path.");
            }

            if (!PrefabUtility.PromptAndCheckoutPrefabIfNeeded(infos[0].assetPath, PrefabUtility.SaveVerb.Apply))
            {
                return;
            }

            PrefabUtility.ApplyAddedGameObjects(gameObjects, infos[0].assetPath, InteractionMode.UserAction);
            EditorUtility.ForceRebuildInspectors();
        }
コード例 #2
0
        internal static void ApplyPrefabObjectOverride(object userData)
        {
            ObjectInstanceAndSourcePathInfo info = (ObjectInstanceAndSourcePathInfo)userData;

            if (!PrefabUtility.PromptAndCheckoutPrefabIfNeeded(info.assetPath, PrefabUtility.SaveVerb.Apply))
            {
                return;
            }
            PrefabUtility.ApplyObjectOverride(info.instanceObject, info.assetPath, InteractionMode.UserAction);
            EditorUtility.ForceReloadInspectors();
        }
コード例 #3
0
            void Apply(object prefabAssetPathObject)
            {
                string prefabAssetPath = (string)prefabAssetPathObject;

                if (!PrefabUtility.PromptAndCheckoutPrefabIfNeeded(prefabAssetPath, PrefabUtility.SaveVerb.Apply))
                {
                    return;
                }
                m_Modification.Apply(prefabAssetPath);
                UpdateAndClose();
            }
コード例 #4
0
        internal static void ApplyPrefabRemovedComponent(object userData)
        {
            ObjectInstanceAndSourceInfo info = (ObjectInstanceAndSourceInfo)userData;
            string path = AssetDatabase.GetAssetPath(info.correspondingObjectInSource);

            if (!PrefabUtility.PromptAndCheckoutPrefabIfNeeded(path, PrefabUtility.SaveVerb.Apply))
            {
                return;
            }
            PrefabUtility.ApplyRemovedComponent((GameObject)info.instanceObject, (Component)info.correspondingObjectInSource, InteractionMode.UserAction);
            EditorUtility.ForceReloadInspectors();
        }
コード例 #5
0
            void Apply(object prefabAssetPathObject)
            {
                string prefabAssetPath = (string)prefabAssetPathObject;

                if (!PrefabUtility.PromptAndCheckoutPrefabIfNeeded(prefabAssetPath, PrefabUtility.SaveVerb.Apply))
                {
                    return;
                }
                m_Modification.Apply(prefabAssetPath);
                EditorUtility.ForceRebuildInspectors(); // handles applying RemovedComponents

                UpdateAndClose();
            }
コード例 #6
0
        internal static void ApplyPrefabPropertyOverride(object userData)
        {
            PropertyAndSourcePathInfo info = (PropertyAndSourcePathInfo)userData;

            if (!PrefabUtility.PromptAndCheckoutPrefabIfNeeded(info.assetPath, PrefabUtility.SaveVerb.Apply))
            {
                return;
            }
            for (int i = 0; i < info.properties.Length; i++)
            {
                PrefabUtility.ApplyPropertyOverride(info.properties[i], info.assetPath, InteractionMode.UserAction);
            }
            EditorUtility.ForceReloadInspectors();
        }
コード例 #7
0
        bool ApplyAll()
        {
            // Collect Prefab Asset paths and also check if there's more than one of the same.
            HashSet <string> prefabAssetPaths = new HashSet <string>();
            bool             multipleOfSame   = false;

            for (int i = 0; i < m_SelectedGameObjects.Length; i++)
            {
                string prefabAssetPath = PrefabUtility.GetPrefabAssetPathOfNearestInstanceRoot(m_SelectedGameObjects[i]);
                if (prefabAssetPaths.Contains(prefabAssetPath))
                {
                    multipleOfSame = true;
                }
                else
                {
                    prefabAssetPaths.Add(prefabAssetPath);
                }
            }

            // If more than one instance of the same Prefab Asset, show dialog to user.
            if (multipleOfSame && !EditorUtility.DisplayDialog(
                    L10n.Tr("Multiple instances of same Prefab Asset"),
                    L10n.Tr("Multiple instances of the same Prefab Asset were detected. Potentially conflicting overrides will be applied sequentially and will overwrite each other."),
                    L10n.Tr("OK"),
                    L10n.Tr("Cancel")))
            {
                return(false);
            }

            // Make sure assets are checked out in version control.
            if (!PrefabUtility.PromptAndCheckoutPrefabIfNeeded(prefabAssetPaths.ToArray(), PrefabUtility.SaveVerb.Apply))
            {
                return(false);
            }

            // Apply sequentially.
            AssetDatabase.StartAssetEditing();
            for (int i = 0; i < m_SelectedGameObjects.Length; i++)
            {
                PrefabUtility.ApplyPrefabInstance(m_SelectedGameObjects[i], InteractionMode.UserAction);
            }
            AssetDatabase.StopAssetEditing();

            EditorUtility.ForceRebuildInspectors();
            return(true);
        }
コード例 #8
0
        bool ApplyAll()
        {
            // Collect Prefab Asset paths and also check if there's more than one of the same.
            HashSet <string> prefabAssetPaths = new HashSet <string>();
            bool             multipleOfSame   = false;

            for (int i = 0; i < m_SelectedGameObjects.Length; i++)
            {
                string prefabAssetPath = PrefabUtility.GetPrefabAssetPathOfNearestInstanceRoot(m_SelectedGameObjects[i]);
                if (prefabAssetPaths.Contains(prefabAssetPath))
                {
                    multipleOfSame = true;
                }
                else
                {
                    prefabAssetPaths.Add(prefabAssetPath);
                }
            }

            // If more than one instance of the same Prefab Asset, show dialog to user.
            if (multipleOfSame && !EditorUtility.DisplayDialog(
                    L10n.Tr("Multiple instances of same Prefab Asset"),
                    L10n.Tr("Multiple instances of the same Prefab Asset were detected. Potentially conflicting overrides will be applied sequentially and will overwrite each other."),
                    L10n.Tr("OK"),
                    L10n.Tr("Cancel")))
            {
                return(false);
            }

            // Make sure assets are checked out in version control.
            if (!PrefabUtility.PromptAndCheckoutPrefabIfNeeded(prefabAssetPaths.ToArray(), PrefabUtility.SaveVerb.Apply))
            {
                return(false);
            }

            var undoStructs = new List <ApplyAllUndo>();
            var actionName  = "ApplyAll";

            for (var i = 0; i < m_SelectedGameObjects.Length; i++)
            {
                var us = new ApplyAllUndo();
                us.correspondingSourceObject = (GameObject)PrefabUtility.GetCorrespondingObjectFromSource(m_SelectedGameObjects[i]);
                Undo.RegisterFullObjectHierarchyUndo(us.correspondingSourceObject, actionName); // handles changes to existing objects and object what will be deleted but not objects that are created
                GameObject prefabInstanceRoot = PrefabUtility.GetOutermostPrefabInstanceRoot(m_SelectedGameObjects[i]);
                Undo.RegisterFullObjectHierarchyUndo(prefabInstanceRoot, actionName);

                us.prefabHierarchy = new HashSet <int>();
                PrefabUtility.GetObjectListFromHierarchy(us.prefabHierarchy, us.correspondingSourceObject);
                undoStructs.Add(us);
            }

            // Apply sequentially.
            AssetDatabase.StartAssetEditing();
            try
            {
                foreach (var t in m_SelectedGameObjects)
                {
                    PrefabUtility.ApplyPrefabInstance(t, InteractionMode.UserAction);
                }
            }
            finally
            {
                AssetDatabase.StopAssetEditing();
            }

            foreach (var t in undoStructs)
            {
                PrefabUtility.RegisterNewObjects(t.correspondingSourceObject, t.prefabHierarchy, actionName);
            }

            EditorUtility.ForceRebuildInspectors();
            return(true);
        }
コード例 #9
0
        public override void OnGUI(Rect rect)
        {
            Rect headerRect = GUILayoutUtility.GetRect(20, 10000, k_HeaderHeight, k_HeaderHeight);

            EditorGUI.DrawRect(headerRect, headerBgColor);

            float labelSize = EditorStyles.boldLabel.CalcSize(Styles.instanceLabel).x;

            headerRect.height = EditorGUIUtility.singleLineHeight;

            Rect labelRect   = new Rect(headerRect.x + k_HeaderLeftMargin, headerRect.y, labelSize, headerRect.height);
            Rect contentRect = headerRect;

            contentRect.xMin = labelRect.xMax;

            GUI.Label(labelRect, Styles.instanceLabel, Styles.boldRightAligned);
            GUI.Label(contentRect, m_InstanceContent, EditorStyles.boldLabel);

            labelRect.y   += EditorGUIUtility.singleLineHeight;
            contentRect.y += EditorGUIUtility.singleLineHeight;
            GUI.Label(labelRect, Styles.contextLabel, Styles.boldRightAligned);
            GUI.Label(contentRect, m_StageContent, EditorStyles.boldLabel);

            GUILayout.Space(k_TreeViewPadding.top);

            if (!IsDisconnected())
            {
                Rect treeViewRect = GUILayoutUtility.GetRect(100, 1000, 0, 1000);
                m_TreeView.OnGUI(treeViewRect);
            }

            if (IsShowingActionButton())
            {
                if (IsDisconnected())
                {
                    EditorGUILayout.HelpBox("Disconnected. Cannot show overrides.", MessageType.Warning);
                }
                else if (m_TreeView.hasModifications)
                {
                    if (m_InvalidComponentOnAsset)
                    {
                        EditorGUILayout.HelpBox(
                            "Click on individual items to review and revert.\nThe Prefab file contains an invalid script. Applying is not possible. Enter Prefab Mode and remove the script.",
                            MessageType.Info);
                    }
                    else if (m_InvalidComponentOnInstance)
                    {
                        EditorGUILayout.HelpBox(
                            "Click on individual items to review and revert.\nThe Prefab instance contains an invalid script. Applying is not possible. Remove the script.",
                            MessageType.Info);
                    }
                    else if (PrefabUtility.IsPartOfModelPrefab(m_SelectedGameObject))
                    {
                        EditorGUILayout.HelpBox(
                            "Click on individual items to review and revert.\nApplying to a model Prefab is not possible.",
                            MessageType.Info);
                    }
                    else if (m_Immutable)
                    {
                        EditorGUILayout.HelpBox(
                            "Click on individual items to review and revert.\nThe Prefab file is immutable. Applying is not possible.",
                            MessageType.Info);
                    }
                    else
                    {
                        EditorGUILayout.HelpBox("Click on individual items to review, revert and apply.",
                                                MessageType.Info);
                    }
                }

                GUILayout.BeginHorizontal();

                GUILayout.FlexibleSpace();

                using (new EditorGUI.DisabledScope(m_InvalidComponentOnAsset))
                {
                    if (GUILayout.Button(m_RevertAllContent, GUILayout.Width(k_ButtonWidth)))
                    {
                        PrefabUtility.RevertPrefabInstance(m_SelectedGameObject, InteractionMode.UserAction);

                        if (editorWindow != null)
                        {
                            editorWindow.Close();
                            GUIUtility.ExitGUI();
                        }
                    }

                    using (new EditorGUI.DisabledScope(m_Immutable || m_InvalidComponentOnInstance))
                    {
                        if (GUILayout.Button(m_ApplyAllContent, GUILayout.Width(k_ButtonWidth)))
                        {
                            string assetPath =
                                PrefabUtility.GetPrefabAssetPathOfNearestInstanceRoot(m_SelectedGameObject);
                            if (PrefabUtility.PromptAndCheckoutPrefabIfNeeded(assetPath, PrefabUtility.SaveVerb.Apply))
                            {
                                PrefabUtility.ApplyPrefabInstance(m_SelectedGameObject, InteractionMode.UserAction);

                                if (editorWindow != null)
                                {
                                    editorWindow.Close();
                                    GUIUtility.ExitGUI();
                                }
                            }
                        }
                    }
                }
            }

            // Escape closes the window
            if (Event.current.type == EventType.KeyDown && Event.current.keyCode == KeyCode.Escape)
            {
                editorWindow.Close();
                GUIUtility.ExitGUI();
            }
        }