Tuesday, 7 May 2013

Sublevels in Unity3D (when prefabs aren't enough)

Most Unity developers will use 'prefabs' sooner or later. For those who are unfamiliar, they are basically a way to clump together a group of objects or behaviours so that you can clone them repeatedly and have them all look and act the same way.

In Luminesca, planktids are prefabs so they can be easily dropped into a level in large numbers, and any time I need to make a change I just edit the prefab and it perpetuates all the way down through all the individual instances.

In the screenshot above, the planktids are two instances of a single prefab. The glowpod is also a prefab. The plants are very simple prefabs, and even the large rocks are too. So they're pretty versatile and will save you a ton of work, but sometimes you need something extra.

During development of Luminesca, I've been tackling a scenario where a large background area of one level is clearly visible at the start of the next level. This area is made of multiple prefabs and is quite complex. Prefabs aren't really appropriate here because you're doubling things up too much, and you might want certain minor elements to differ between levels.

The solution I implemented is a nifty trick I learned from using Unreal Development Kit, and it's a special way of loading content called 'sublevels'. The way sublevels work is that you can run a script at any time which loads the contents of one level over the top of the level you are already in.

A sublevel can contain whatever you want (it's essentially just a bog-standard Unity scene) and you can load it into as many other levels as you like. You just need to be a bit savvy to make sure you have the right objects saved in the right scene, and that their coordinates line up properly. I achieved this quite easily by building everything in one scene, copying all the shared objects over to the sublevel, and then deleting them from the original scene (or, more specifically, I just disabled them for now so I can easily peek at where they line up).

The script to perform this is Application.LoadLevelAdditive (the standard method would be to use Application.LoadLevel, which automatically unloads the current level). Then it's just a case of putting the script on an object in each scene that needs to use the sublevel.

Sublevels can be pretty handy if you have more than one person working on the same level, because they can both be working in separate scenes which then get loaded together.

You could even get really clever with this and create your own level streaming system, so objects are loaded/unloaded as you get near them. Using this method you could have one giant, continuous world with no loading screens.

Finally, there's the added benefit of drastically de-cluttering your main scene so Unity runs nice and smooth!


  1. I've read about this before when investigating creating a loading screen. Seems very useful, too bad Application.LoadLevelAdditive isn't available in the free version.

    1. I'm using the free version and it's worked just fine for me!

  2. does unity have this as a suggestion? i'm trying unity out but really wish it has persistent/sub-levels like udk where people can work on different and connected levels at once

    1. No this is just something I was able to put together using the tools they provide and a bit of intuition. UDK's support is much better to be honest. I hear the streaming tools are going to be much better in Unity 5 though!