Version: 2023.1
LanguageEnglish
  • C#

EditorSceneManager.OpenPreviewScene

Suggest a change

Success!

Thank you for helping us improve the quality of Unity Documentation. Although we cannot accept all submissions, we do read each suggested change from our users and will make updates where applicable.

Close

Submission failed

For some reason your suggested change could not be submitted. Please <a>try again</a> in a few minutes. And thank you for taking the time to help us improve the quality of Unity Documentation.

Close

Cancel

Declaration

public static SceneManagement.Scene OpenPreviewScene(string scenePath);

Parameters

scenePath Scene file to open.

Returns

Scene The created preview Scene.

Description

Opens a Scene Asset in a preview Scene.

You can use this function for tooling that needs to access GameObjects but where the scene should not be displayed in the Hierarchy. Make sure to call ClosePreviewScene to prevent leaking scenes.

See Also: NewPreviewScene, ClosePreviewScene.

using UnityEditor;
using UnityEditor.SceneManagement;
using UnityEngine;

public static class TestOpenAsPreviewScene { [MenuItem("Assets/Scene Root Names")] static void OpenContextClickedSceneInAPreviewScene() { SceneAsset sceneAsset = Selection.activeObject as SceneAsset; if (sceneAsset == null) { Debug.Log("Context click on a Scene Asset file"); return; }

var assetPath = AssetDatabase.GetAssetPath(sceneAsset); var scene = EditorSceneManager.OpenPreviewScene(assetPath); try { var rootGameObjects = scene.GetRootGameObjects();

Debug.Log($"Root GameObject Names (count: {rootGameObjects.Length})"); foreach (var gameObject in rootGameObjects) Debug.Log(gameObject.name); } finally { EditorSceneManager.ClosePreviewScene(scene); } } }