- Home /
Is it possible to generate scenes programmatically ?
I noticed a similar question being asked here before but with a very different use case in mind. Here is what I am trying to accomplish:
I have a Main scene with a GUI which is what the user interacts with. For simplicity let's say that interacting with this GUI they are intuitively creating a simple 3D room. After they are done I would like them to be able to "Export" their work.
What needs to be exported is a scene with the newly created room (plus some specific settings) and nothing else (e.g. the GUI that comes with the Main scene). I was thinking that, upon exporting, I could create a scene on the fly, add to it a specific camera, light, and the room with special settings and package it. This way they will be able to load it into Unity for later use. Think of it as a GUI to create specific rooms where the user only has to specify the minimum amount of details and then it's all packaged for them. Now it can't just be the 3D room but it needs to include a specific camera, a directional light and many other elements that the user does not need to worry about.
Is something like this possible in Unity ? Am I tackling this problem from the wrong angle ?
In case the explanation was confusing here is another attempt at explaining it:
The Main Scene is a 2D GUI the user interacts with to setup a custom room, the goal is that this room will be later used with Unity for another work. The reason why there's a GUI for it is because there are specific settings (like camera, lights, materials, ...) that the user does not need to worry about, but should be included when the room is exported. What the "later work" is expecting is a room with proper setup ready to be loaded in Unity and used for whatever purpose, but it's expecting the scene to be setup in a specific way (which is why I though that the "export" functionality will provide the setup).
Answer by Bunny83 · Jul 23, 2019 at 06:34 PM
At runtime in a build game that is not possible. Scenes are authored by the UnityEditor. In general you can not create any kind of Asset at runtime. You can manually create instances of certain asset types but you can not make them assets and store them in the asset database of your project as this is done by the Unity editor.
So you would need to roll your own way of saving and loading your world.
Inside the UnityEditor it is possible to create and save actual scenes. Since your question is quite abstract what kind of "details" you want to be able to edit. Since you said that only a few specific things can be adjusted by a user, it would be enough to just store this information for example as json. If you really need to make a scene out of that information, you can write an editor script which will read that information from json and build the scene inside the editor.
Thanks for the answer! I'm gonna explore a custom way to save and load the world, thanks for the advice.
What if I had an additional scene already present that I populate (with instances of assets for example) with what I need based on user input and then when they decide to "export", I simply save the additional scene as a package ?
I figured that I don't necessarily need to create a new scene at run time, I can already have it from the start and then just populate it and package it at run time (if that's possible).
Edit: Never $$anonymous$$d I can't save as... at run time so it seems like creating a metadata JSON file is the way to go.
Your answer
![](https://koobas.hobune.stream/wayback/20220612205401im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Export a scene using a script? 0 Answers
Can I build a scene once and have it be importable? 2 Answers
merge two scenes as a one 1 Answer
The application crashes when it loads the video 0 Answers
My levels don't unlock. 2 Answers