public float PrefabModeButton(GameObjectTreeViewItem item, Rect selectionRect)
        {
            float contentRectRight = selectionRect.xMax;

            if (item.showPrefabModeButton)
            {
                float yOffset    = (selectionRect.height - GameObjectStyles.rightArrow.fixedWidth) / 2;
                Rect  buttonRect = new Rect(
                    selectionRect.xMax - GameObjectStyles.rightArrow.fixedWidth - GameObjectStyles.rightArrow.margin.right,
                    selectionRect.y + yOffset,
                    GameObjectStyles.rightArrow.fixedWidth,
                    GameObjectStyles.rightArrow.fixedHeight);

                int        instanceID = item.id;
                GUIContent content    = buttonRect.Contains(Event.current.mousePosition) ? PrefabStageUtility.GetPrefabButtonContent(instanceID) : GUIContent.none;
                if (GUI.Button(buttonRect, content, GameObjectStyles.rightArrow))
                {
                    GameObject go             = EditorUtility.InstanceIDToObject(instanceID) as GameObject;
                    string     assetPath      = PrefabUtility.GetPrefabAssetPathOfNearestInstanceRoot(go);
                    Object     originalSource = AssetDatabase.LoadMainAssetAtPath(assetPath);
                    if (originalSource != null)
                    {
                        var prefabStageMode = PrefabStageUtility.GetPrefabStageModeFromModifierKeys();
                        PrefabStageUtility.OpenPrefab(assetPath, go, prefabStageMode, StageNavigationManager.Analytics.ChangeType.EnterViaInstanceHierarchyRightArrow);
                    }
                }

                contentRectRight = buttonRect.xMin;
            }

            return(contentRectRight);
        }
        static string GetAssetGUID(GameObjectTreeViewItem item)
        {
            var go = (GameObject)item.objectPPTR;

            if (!go || PrefabUtility.GetNearestPrefabInstanceRoot(go) != go)
            {
                return(null);
            }

            var assetPath = PrefabUtility.GetPrefabAssetPathOfNearestInstanceRoot(go);

            return(AssetDatabase.AssetPathToGUID(assetPath));
        }
        GUIContent GetPrefabButtonContent(int instanceID)
        {
            GUIContent result;

            if (m_PrefabButtonContents.TryGetValue(instanceID, out result))
            {
                return(result);
            }

            string path     = PrefabUtility.GetPrefabAssetPathOfNearestInstanceRoot(EditorUtility.InstanceIDToObject(instanceID) as GameObject);
            string filename = System.IO.Path.GetFileNameWithoutExtension(path);

            result = new GUIContent("", null, "Open Prefab Asset '" + filename + "'");
            m_PrefabButtonContents[instanceID] = result;
            return(result);
        }
        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);
        }
        private Asset GetAsset(GameObjectTreeViewItem item)
        {
            if (!Provider.isActive)
            {
                return(null);
            }

            GameObject go = (GameObject)item.objectPPTR;

            if (!go || PrefabUtility.GetNearestPrefabInstanceRoot(go) != go)
            {
                return(null);
            }

            string assetPath = PrefabUtility.GetPrefabAssetPathOfNearestInstanceRoot(go);
            var    guid      = AssetDatabase.AssetPathToGUID(assetPath);

            Asset vcAsset = string.IsNullOrEmpty(guid) ? null : Provider.GetAssetByGUID(guid);

            return(vcAsset);
        }
        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();
            }
        }