- Home /
The question is answered, right answer was accepted
Prefab of a whole scene?
Hello all,
I looked around but couldn't find a definitive answer for this one. I recently found that I could instantiate whole scenes (objects), removing the need for me to make different scenes for my game (for this area at least). This also allowed me to make scripts that would change values specific to the level depending on which was being loaded. I would add the script and it would instantiate the proper prefab (prefab of prefabs). To my dismay I find that there is a disconnect between the prefabs within the "scene" prefab and the individual prefabs it's made of. I really liked this technique, is there no way to keep the connection to each individual prefab?
Unity does not currently support nested prefabs. Watch out for future releases.
Answer by Bunny83 · Mar 05, 2012 at 11:45 AM
Like @asafsitner mentioned Unity doesn't support prefabs in prefabs at the moment. But you can use scenes as second level prefabs ;). Just create your levels as scenes, make sure your scene has a master-GameObject ( preferable at [0,0,0] ) and parent everything to this GameGbject so your scene just contains one GameObject.
Don't turn this gameobject into a prefab or, like you've mentioned, you will loose all prefab connections in the scene. Just rename the master gameobject either to your level name or some general unique name.
To load a level, use Application.LoadLevelAdditive() or if you have Unity pro maybe Application.LoadLevelAdditiveAsync(). This will add the scene content to your current scene. Since every scene is a single gameobject you can easily delete one.
If you used the level name for he gameobject, just load a new level like this:
// C#
public GameObject MyLoadLevel(string aLevelName)
{
Application.LoadLevelAdditive(aLevelName);
return GameObject.Find(aLevelName);
}
GameObject levelInstance = MyLoadLevel("Level1");
//Now you can use levelInstance like a instantiated prefab.
// Don't forget to destroy the old level by destroying the master-GameObject of the old level if you don't need the old level anymore.
If you want to use a general unique name like "LevelMasterObject", you should rename it after "Instantiate":
// C#
public GameObject MyLoadLevel(string aLevelName)
{
Application.LoadLevelAdditive(aLevelName);
GameObject instance = GameObject.Find("LevelMasterObject");
instance.name = aLevelName;
return instance;
}
Just make sure the level-scenes don't contain any other objects except the master gameobject.
I tried this out and it works well. It's a much better method and I can just attach the script to the $$anonymous$$aster object. Thank you.
I'm also trying to do something similar to this however when I add all my scene objects to the main empty $$anonymous$$aster Controller object objects that were children of other objects all become one child of the $$anonymous$$aster object. Am I not doing something correctly?
This is very useful, thanks so much!
Note that LoadLevelAdditive has been deprecated. The new way to do this is Scene$$anonymous$$anager.LoadScene ("sceneName", LoadScene$$anonymous$$ode.Additive)
You can remove the scene with Scene$$anonymous$$anager.UnloadScene ("sceneName")
Answer by clevermango · Jan 13, 2015 at 03:03 PM
I was needing to pull in an Asset Bundle of a whole scene, on a per scene basis from a Server.
I just -
1 - Create Prefab in Project Window
2 - Place an Empty Game Object inside
3 - Place the Prefab into the Scene
4 - Drag all Assets you want in the Bundle into the Prefab
5 - Drag the newly populated Prefab into the Project Window
6 - Run Asset bundle scrip with Dependencies (found here - http://docs.unity3d.com/ScriptReference/BuildPipeline.BuildAssetBundle.html)
7 - Select required save location
8 - Ready to use Asset Bundle of whole scene
Answer by astracat111 · Apr 13, 2017 at 04:40 PM
The easiest way, although it probably isn't recommended by some here, is just to go into the folder by right clicking "show in explorer", and in the explorer simply have a duplicated scene.
The only problem with prefabs is that you need to update them because you're making a copy, but then again even if you are working with prefabs, to be honest you need to have a really good base template system set before you expand and make a bigger game, so for me personally I just said 'screw it' and started copying scenes once my base template was done.