- Home /
Should an entire level be a prefab?
As far as ican see there's 2 ways to create different levels. Either save them as a scene, then load them using:
Application.LoadLevel ("Level2");
But this might become cumbersome if I have many levels. I have to open them individually.
So the other idea was to save each level as a prefab and spawn the relevant one, and destroy the old when switching levels, but keep it within the same scene. What I want to check is that this is good practice? Is there an advantage of keeping levels in separate scenes? At least with the spawn/destroy method I can compare scenes side by side if needed.
I'd like to know your opinion, and possibly any link to tutorials if possible. Thanks!
Answer by TonyLi · Jul 11, 2014 at 03:51 PM
Like @meat5000 writes, additive loading may be your best solution. I put together a free package and short tutorial on this at: http://www.pixelcrushers.com/scene-loader/
Not only can it take a long time to load a big prefab scene, but you can't nest prefabs, so you lose the advantages of prefab objects within the scene.
Answer by kannan21 · Jul 04, 2014 at 12:49 PM
If you load too much stuff in a single scene like you said most likely there will be a heavy performance issue. All of those objects will be in memory that is the main reason why we use scene system. If you put all the objects in one scene unity editor will keep crashing and the same happens to your game too. Instantiate is a really heavy function and so does destroy. So create separate scenes and iterate though them when ever it is necessary. But the things in your scene if they are very small in size and not heavy sized 3d objects you can keep all of them in one scene and you can enable and disable desired objects.
I assume loadLevel is pretty heavy too. I'd be loading the same assets whether I'm loading a scene, or just deleting the old prefab and instantiating another. Surely it'd be the same?
Answer by MDarkwing · Jul 04, 2014 at 12:54 PM
Scenes are definitely better approach than instantiating whole level prefab which can be small (plane + few cubes, in which case it doesn't really matter) or ultra big (some castle environment with building, dungeons, trees, etc).
Another approach would be to have all the level prefabs in one scene then just enable/disable them correspondingly to what level do you need. This approach saves you trouble of data persistence throughout loading new scenes.
In my early days of development I would also put stuff in one scene but multiple scene is definitely good approach in most of cases.
On top of it all, I think you are missing the point of what prefabs are meant to be used. Prefab is a container to place some stuff you don't want to make again (in plain words). Example is you have some environment models: trees, rocks, grass, stumps, and you have to make a scenery out of it. It is more easy to make say 10 prefabs with different setting on them, then put those prefabs to make a scenery around a level, then to every time manualy put trees,rocks etc... This is the basic idea behind the prefab. Also one of the advantages of prefabs is, say now you want to make the winter setting of the map, and you have models that are covered in snow, but would be tiresome to put them all 1 by 1. You just get the prefab change the environment props in it, then save it and all objects based on that prefab will change also.
All of my scenes will have the same assets anyway. 3 types of asteroids, plus 4 invisible boundary walls. So the reason I suggested saving it as a prefab would be to contain it, so it can easily be instantiated or destroyed.
Sry didn't see your comment on time. As I've said, you can do it whatever the way you see it fit, just instantiating and destroying the environment prefabs that can have lot of objects on them, can be performance costly. The technique you want to use is very subjective to your project type i would need more info to give you the best advice. For now, best solution that I see is to have scenes and prefabs. if you still want to have 1 scene, than make prefabs but dont destroy them, just enable/disable them based on what level you are playing.
Your answer
Follow this Question
Related Questions
Switching between several levels 1 Answer
UnityEngine.SceneManagement contains no methods? 1 Answer
Help with loading scenes 2 Answers
Using parameters of activeSceneChanged? 0 Answers
Scene freezes after loaded 1 Answer