Search Unity

Can subscenes be added via code?

Discussion in 'Entity Component System' started by daschatten, Jul 3, 2019.

  1. daschatten

    daschatten

    Joined:
    Jul 16, 2015
    Posts:
    208
    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!
     
  2. alexnown

    alexnown

    Joined:
    Oct 25, 2018
    Posts:
    22
    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.
    Code (CSharp):
    1. var scene = EditorSceneManager.NewScene(NewSceneSetup.EmptyScene, NewSceneMode.Additive);
    2. SceneManager.MoveGameObjectToScene(prefab, scene);
    3. var assetPath = Path.Combine(savePrefabPath, $"{assetName}.unity");
    4. EditorSceneManager.SaveScene(scene, assetPath);
    5. var sceneAsset = AssetDatabase.LoadAssetAtPath<SceneAsset>(assetPath);
    6. Unity.Entities.Hash128 guid = new GUID(AssetDatabase.AssetPathToGUID(assetPath));
    7. EditorEntityScenes.WriteEntityScene(scene, guid, 0);
    8. SceneManager.UnloadSceneAsync(scene);
     
    Enzi, Opeth001 and starikcetin like this.
  3. daschatten

    daschatten

    Joined:
    Jul 16, 2015
    Posts:
    208
    Great, thanks!