static bool IsPartOfPrefabStage(GameObject gameObject, PrefabStage prefabStage)
 {
     if (gameObject == null)
     {
         return(false);
     }
     return(FindGameObjectRecursive(prefabStage.prefabContentsRoot.transform, gameObject));
 }
Beispiel #2
0
    void OnSceneClosing(UnityEditor.Experimental.SceneManagement.PrefabStage scene)
    {
        UnityEditor.Experimental.SceneManagement.PrefabStage.prefabStageClosing -= OnSceneClosing;
        var stage = UnityEditor.Experimental.SceneManagement.PrefabStageUtility.GetCurrentPrefabStage();

        PrefabUtility.SaveAsPrefabAsset(stage.prefabContentsRoot, stage.prefabAssetPath);
        FlushEditors();
    }
        internal static bool IsGameObjectThePrefabRootInAnyPrefabStage(GameObject gameObject)
        {
            PrefabStage prefabStage = GetCurrentPrefabStage();

            if (prefabStage != null && prefabStage.isValid)
            {
                return(prefabStage.prefabContentsRoot == gameObject);
            }
            return(false);
        }
Beispiel #4
0
        internal static PrefabStage OpenPrefabMode(string prefabAssetPath, GameObject openedFromInstance, PrefabStage.Mode prefabStageMode, StageNavigationManager.Analytics.ChangeType changeTypeAnalytics)
        {
            if (EditorApplication.isPlaying)
            {
                bool blockPrefabModeInPlaymode = CheckIfAnyComponentShouldBlockPrefabModeInPlayMode(prefabAssetPath);
                if (blockPrefabModeInPlaymode)
                {
                    return(null);
                }
            }

            PrefabStage prevPrefabStage = GetCurrentPrefabStage();
            bool        setAsFirstItemAfterMainStage = prevPrefabStage == null || !IsPartOfPrefabStage(openedFromInstance, prevPrefabStage);

            var    previousSelection = Selection.activeGameObject;
            UInt64 previousFileID    = (openedFromInstance != null) ? GetFileIDForCorrespondingObjectFromSourceAtPath(previousSelection, prefabAssetPath) : 0;

            // Ensure valid prefabStageMode (if no context then do not allow Prefab Mode in Context)
            if (openedFromInstance == null && prefabStageMode != PrefabStage.Mode.InIsolation)
            {
                prefabStageMode = PrefabStage.Mode.InIsolation;
            }

            Stage contextStage = null;

            if (prefabStageMode == PrefabStage.Mode.InContext)
            {
                var stageHistory = StageNavigationManager.instance.stageHistory;
                contextStage = stageHistory[stageHistory.Count - 1].GetContextStage();
            }

            var prefabStage = GetExistingPrefabStage(prefabAssetPath, openedFromInstance, prefabStageMode);

            if (prefabStage == null)
            {
                prefabStage = PrefabStage.CreatePrefabStage(prefabAssetPath, openedFromInstance, prefabStageMode, contextStage);
            }
            if (StageNavigationManager.instance.SwitchToStage(prefabStage, setAsFirstItemAfterMainStage, changeTypeAnalytics))
            {
                // If selection did not change by switching stage by us or user (or if current selection is not part of
                // the opened prefab stage) then handle automatic selection in new prefab mode.
                if (Selection.activeGameObject == previousSelection || !prefabStage.IsPartOfPrefabContents(Selection.activeGameObject))
                {
                    HandleSelectionWhenSwithingToNewPrefabMode(GetCurrentPrefabStage().prefabContentsRoot, previousFileID);
                }

                SceneView.RepaintAll();
                return(prefabStage);
            }
            else
            {
                // Failed to switch to new stage
                return(null);
            }
        }
        public static PrefabStage GetPrefabStage(GameObject gameObject)
        {
            // Currently there's at most one prefab stage. Refactor in future if we have multiple.
            PrefabStage prefabStage = GetCurrentPrefabStage();

            if (prefabStage != null && prefabStage.scene == gameObject.scene)
            {
                return(prefabStage);
            }

            return(null);
        }
    public static bool PrefabModeIsActive(GameObject gameObject) //https://stackoverflow.com/questions/56155148/how-to-avoid-the-onvalidate-method-from-being-called-in-prefab-mode
    {
#if UNITY_EDITOR && UNITY_2018_3_OR_NEWER
        UnityEditor.Experimental.SceneManagement.PrefabStage prefabStage = UnityEditor.Experimental.SceneManagement.PrefabStageUtility.GetPrefabStage(gameObject);
        if (prefabStage != null)
        {
            return(true);
        }
        if (UnityEditor.EditorUtility.IsPersistent(gameObject))
        {
            return(true);
        }
#endif
        return(false);
    }
        internal static bool IsGameObjectInInvalidPrefabStage(GameObject gameObject)
        {
            PrefabStage prefabStage = GetCurrentPrefabStage();

            return(prefabStage != null && prefabStage.scene == gameObject.scene && !prefabStage.isValid);
        }