scenePath | 要打开的场景文件。 |
Scene 创建的预览场景。
在预览场景中打开场景资源。
您可以将此函数用于需要访问游戏对象但场景不应显示在层级视图中的工具。请确保调用 ClosePreviewScene 以防止场景泄漏。
其他资源: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); } } }