public static GameObject CreateNewPalette(string folderPath, string name, Grid.CellLayout layout, GridPalette.CellSizing cellSizing, Vector3 cellSize, Grid.CellSwizzle swizzle)
        {
            GameObject temporaryGO = new GameObject(name);
            Grid       grid        = temporaryGO.AddComponent <Grid>();

            // We set size to kEpsilon to mark this as new uninitialized palette
            // Nice default size can be decided when first asset is dragged in
            grid.cellSize    = cellSize;
            grid.cellLayout  = layout;
            grid.cellSwizzle = swizzle;
            CreateNewLayer(temporaryGO, "Layer1", layout);

            string path = AssetDatabase.GenerateUniqueAssetPath(folderPath + "/" + name + ".prefab");

            Object      prefab  = PrefabUtility.SaveAsPrefabAssetAndConnect(temporaryGO, path, InteractionMode.AutomatedAction);
            GridPalette palette = GridPalette.CreateInstance <GridPalette>();

            palette.name       = "Palette Settings";
            palette.cellSizing = cellSizing;
            AssetDatabase.AddObjectToAsset(palette, prefab);
            PrefabUtility.ApplyPrefabInstance(temporaryGO);
            AssetDatabase.Refresh();

            GameObject.DestroyImmediate(temporaryGO);
            return(AssetDatabase.LoadAssetAtPath <GameObject>(path));
        }
        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);
        }
        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);
        }
        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();
            }
        }