How to save scene generated by script

Hi there Unity community,

I am fairly new to Unity and got involved in a project which requires me to what the title says.
To be more specific:

I generate a scene using a script to process OpenStreetMap data by reading the xml file and then generate a scene according to the xml data. i.e. buildings, streets.
Problem is, I cant save the scene once the script finished cause it only does so when I press the “play” button and deletes the entire thing when I stop it.

Is there any way to save the generated scene so that I can load the scene directly by opening the Unity project without running the script again.

Hope I explain what exactly I need to do. In the attachments you will find a screenhot how my “Scene” viewport looks like while the project is “playing”. I need to save that scene so i can load it directly without running the script again.

Thank you very much in advance

The EditorSceneManager has functions to save scenes, but they cannot be used at runtime. You cannot save the scene or prefabs while the game is running because those functions belong to the UnityEditor namespace.

What you CAN do is run the game, copy all GameObjects from the scene hierarchy in the Unity Editor, stop running the game, then paste all the GameObjects and save the scene. You can not do this in a build, only in the Unity Editor.

1 Like

Tried that and unfortunately that doesnt work. I create an empty GameObject. Took at the generated GameObjects, attached them to the new GameObject and saved it as prefab. It had all the gameobjects associated but they didnt show in the viewport when I dragged it into the scene. Someone else told me thats not possible because all of my objects (house meshes) are dynamically generated via script

It sounds like if you get meshes to save you’ll probably have everything you need? I haven’t saved meshes before, but it looks like you can use “AssetDatabase.CreateAsset(Mesh, String)” and “AssetDatabase.SaveAssets()”.

Looks like there’s an example here: Save Mesh Created by Script in Editor PlayMode « Unity Coding – Unity3D

You can create empty object, drag and drop all game objects on scene into it, then drag and drop this new game object to Project (save as Prefab).

1 Like

I have the some problem, dynamically generated objects can be saved in this way, but the mesh files are lost. I have a lot objects and if I save the mesh, how can I match them?

Not possible!!!

1 Like

How about… In editor? using scripts.

I really want to know too.

But the code can be executed not only when the game is running. For example, start the “generation” of the city from the context menu of the script WITHOUT STARTING THE GAME and save the scene.

using UnityEngine;

public class CityGenerator : MonoBehaviour
{
    [ContextMenu("Generate Test")]
    void Generate()
    {
        for (int i = 0; i < 30; i++)
        {
            var cube = GameObject.CreatePrimitive(PrimitiveType.Cube);
            cube.transform.localScale = new Vector3(Random.Range(0.1f, 1f), Random.Range(0.1f, 1f), Random.Range(0.1f, 1f));
            cube.transform.position = new Vector3(Random.Range(-5, 5), cube.transform.localScale.y * 0.5f, Random.Range(-5, 5));
        }
    }
}
1 Like