LanguageEnglish
  • C#

EditorSceneManager.OpenPreviewScene

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.

Additional resources: 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); } } }

对文档有任何疑问,请移步至开发者社区提问,我们将尽快为您解答
Copyright © 2023 Unity Technologies
优美缔软件(上海)有限公司 版权所有
"Unity"、Unity 徽标及其他 Unity 商标是 Unity Technologies 或其附属机构在美国及其他地区的商标或注册商标。其他名称或品牌是其各自所有者的商标。
公安部备案号:
31010902002961