- Home /
Is it possible to create scenes at runtime with the Unity engine?
My friend and I are working on a game and wish to create a game with procedurally generated landscape (terrain, buildings, flora and fauna). Assuming we created all the Assets in advance and simply gave them to a script to place them along with the randomly generated terrain is this possible with any version of Unity? Also the major part of my question is can this be done while the game is being executed, sort of Minecraft style world generation.
Answer by zeh · Feb 21, 2013 at 04:48 PM
Sort of. You wouldn't necessarily be creating scenes, but running the same scene with scripts that create your procedural content as needed. You would probably be mostly instantiating Prefabs at runtime and creating meshes dynamically. You would still need a few objects (camera, scripts, etc) placed on your scene but it would otherwise be an empty scene where everything would be instantiated while the game runs. This could be done at the Awake()
state of your scripts but of course also be re-ran at a later time to replace the current content of the scene.
Hmm, thanks! I have a ways to go before I get to that point (still working on terrain generation). Those links were pretty useful!
I would avoid instantiating prefabs directly. I would use a pooling system (like the fantastic Pool$$anonymous$$anager) to improve performance by recycling previously instantiated prefabs where possible.
Sometimes it is useful to reload the empty scene to clear everything.
@Dunlain: cool. Just to be clear, for terrain generation you should use the $$anonymous$$esh class (my link above on dynamic mesh generation has some great resources on the topic). I've recently implemented this myself and it's quite simple once you get the hang of it. If you want natural but random terrain, you'll probably need some perlin noise to create the height of your vertices correctly too.
Answer by sbuzonas · Sep 19, 2017 at 03:00 PM
You can use SceneManagement.SceneManager.CreateScene.
using UnityEngine.SceneManagement;
using UnityEngine;
public class SceneFactory : MonoBehaviour {
Scene _scene = SceneManager.CreateScene("Procedural Scene");
}
Is this "Scene _scene = ..." method available, if the name of the scene were to be in a variable, or does it have to be absolutely declared like shown above? I'm creating a game that has multiple levels that can use the same scene, but it would need a new title, so I would have the scene name created like the following:
string Level = "Level";
int LevelNum = 1;
string CurLevel = Level + int.ToString;
Scene _scene = Scene$$anonymous$$anager.CreateScene(CurLevel);
or something similar to that (I'm new to C#, so my syntax needs correcting!). Would this work?
I'm sure you meant to write "LevelNum.ToString()" but yes, this code works. void Start () { string Level = "Level"; for(int LevelNum = 1; LevelNum<=10; LevelNum++){ string CurLevel = Level + LevelNum.ToString(); Scene _scene = Scene$$anonymous$$anager.CreateScene(CurLevel); } } //creates ten levels
so far i do know this too but how can you save the created scene in runtime?
You can save a template of the scene as a binary file.
Then each time you are creating a scene an run time you can use this template to load things back in.
*By template I mean a regular class (not monobehaviour), with properties that can be serialized.