Ejemplo n.º 1
0
        /// <summary>
        /// Returns true if this GameObject exists in a Scene.
        /// <para>Use this to distinguish between e.g. regular GameObjects and Prefab Asset GameObjects.</para>
        /// </summary>
        public static bool ExistsInScene(this GameObject gameObject)
        {
            bool inScene;

            GameObjectEnvironments environment = gameObject.GetGameObjectEnvironment();

            switch (environment)
            {
            default:
            case GameObjectEnvironments.Scene:
            case GameObjectEnvironments.PrefabInstance:
                inScene = true;
                break;

            case GameObjectEnvironments.PrefabAsset:
            case GameObjectEnvironments.PrefabStage:
            case GameObjectEnvironments.NestedPrefabStage:
            case GameObjectEnvironments.PrefabImport:
            case GameObjectEnvironments.PreviewScene:
                inScene = false;
                break;
            }

            return(inScene);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// In Edit mode, marks the scene containing this GameObject as dirty.
        /// <para>In Prefab mode, marks the prefab asset containing this GameObject as dirty.</para>
        /// <para>In Play mode, does nothing.</para>
        /// </summary>
        /// <returns>True if the object was marked dirty, false otherwise.</returns>
        public static bool MarkSceneDirty(this GameObject gameObject)
        {
#if UNITY_EDITOR
            //skip in play mode
            if (Application.isPlaying)
            {
                return(false);
            }

            GameObjectEnvironments environment = gameObject.GetGameObjectEnvironment();
            switch (environment)
            {
            //if the GameObject is being edited as part of a Scene:
            case GameObjectEnvironments.Scene:
            case GameObjectEnvironments.PrefabInstance:
                //mark the scene containing this GameObject as dirty
                EditorSceneManager.MarkSceneDirty(gameObject.scene);
                return(true);

            //if the GameObject is being edited as part of a Prefab:
            case GameObjectEnvironments.PrefabStage:
            case GameObjectEnvironments.NestedPrefabStage:
                //mark the Prefab containing this GameObject as dirty
                //do this by using the currently open PrefabStage scene
                PrefabStage prefabStage = PrefabStageUtility.GetCurrentPrefabStage();
                if (prefabStage != null)
                {
                    EditorSceneManager.MarkSceneDirty(prefabStage.scene);
                    return(true);
                }
                else
                {
                    return(false);
                }

            //do nothing if the GameObject is from some other environment
            case GameObjectEnvironments.PrefabAsset:
            case GameObjectEnvironments.PrefabImport:
            case GameObjectEnvironments.PreviewScene:
            default:
                return(false);
            }
#else
            //do nothing outside editor
            return(false);
#endif
        }