Can subscenes be added via code?

Hi,

i would like to generate subscenes from a procedural generated world (in editor). Though the only possibility to generate a subscene seems to be via “New SubScene from Selection”. Is there a way to create and populate a subscene via code?

Thanks!

I’m using the code below for creating new scene asset in editor, put game object into it and serialize scene.
Maybe this will be useful for you.

var scene = EditorSceneManager.NewScene(NewSceneSetup.EmptyScene, NewSceneMode.Additive);
SceneManager.MoveGameObjectToScene(prefab, scene);
var assetPath = Path.Combine(savePrefabPath, $"{assetName}.unity");
EditorSceneManager.SaveScene(scene, assetPath);
var sceneAsset = AssetDatabase.LoadAssetAtPath<SceneAsset>(assetPath);
Unity.Entities.Hash128 guid = new GUID(AssetDatabase.AssetPathToGUID(assetPath));
EditorEntityScenes.WriteEntityScene(scene, guid, 0);
SceneManager.UnloadSceneAsync(scene);
3 Likes

Great, thanks!