Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Voting for the Unity Awards are OPEN! We’re looking to celebrate creators across games, industry, film, and many more categories. Cast your vote now for all categories
    Dismiss Notice
  3. Dismiss Notice

How to save scene generated by script

Discussion in 'Editor & General Support' started by BCuracao, Mar 7, 2018.

  1. BCuracao

    BCuracao

    Joined:
    Apr 22, 2017
    Posts:
    17
    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
     

    Attached Files:

  2. MSplitz-PsychoK

    MSplitz-PsychoK

    Joined:
    May 16, 2015
    Posts:
    1,278
    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.

    https://docs.unity3d.com/ScriptReference/SceneManagement.EditorSceneManager.html

    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.
     
    sandolkakos likes this.
  3. BCuracao

    BCuracao

    Joined:
    Apr 22, 2017
    Posts:
    17

    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
     
  4. MSplitz-PsychoK

    MSplitz-PsychoK

    Joined:
    May 16, 2015
    Posts:
    1,278
  5. LeonardK

    LeonardK

    Joined:
    Mar 13, 2013
    Posts:
    10
    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).
     
    Joe-Censored likes this.
  6. daooo

    daooo

    Joined:
    Jul 25, 2019
    Posts:
    2
    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?
     
  7. estehak

    estehak

    Joined:
    Apr 17, 2021
    Posts:
    1
    Not possible!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
     
    Rachan likes this.
  8. frozpros

    frozpros

    Joined:
    Nov 11, 2019
    Posts:
    2
    How about... In editor? using scripts.
     
  9. Rachan

    Rachan

    Joined:
    Dec 3, 2012
    Posts:
    654
    I really want to know too.
     
  10. samana1407

    samana1407

    Joined:
    Aug 23, 2015
    Posts:
    69
    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.

    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. public class CityGenerator : MonoBehaviour
    4. {
    5.     [ContextMenu("Generate Test")]
    6.     void Generate()
    7.     {
    8.         for (int i = 0; i < 30; i++)
    9.         {
    10.             var cube = GameObject.CreatePrimitive(PrimitiveType.Cube);
    11.             cube.transform.localScale = new Vector3(Random.Range(0.1f, 1f), Random.Range(0.1f, 1f), Random.Range(0.1f, 1f));
    12.             cube.transform.position = new Vector3(Random.Range(-5, 5), cube.transform.localScale.y * 0.5f, Random.Range(-5, 5));
    13.         }
    14.     }
    15. }