I was researching ways to do UI, and one thing I came across was using LoadLevelAdditive, and place each menu on its own scene. This seems pretty cool (and easy), but I was wondering the best way to go about this as there doesn’t appear to be a way to unload the scene once they exit / change menus.
One thing I have read is that I could have a GameObject which is a parent to every element in the scene and then just Destroy it upon exit / change.
I know I could just test everything above, but I was wondering if anyone has gone this route, or attempted to, and any feedback they may have on it. Thinking about it seems like any easy way to decouple everything, but I will admit I’m not the most experienced with Unity.
So I was able to sit down today and try it out, and I have to say it went much smoother than I anticipated. The approach I am currently taking is that I load my “main” scene at the start, and then have a GameManager load in the menus as needed. Each menu is parented under a GameObject and is deleted when I need to “unload.”
I think Destroy() will be fine since most menus are only called once, maybe twice. The in-game menu I may need to put into the pooling system as it will be called much more often. Time will tell, although I do like this approach much more than anything else I have tried before.
I used to do this… but… it’s honestly annoying to do. You have to make sure your menus are included in the build settings, swapping out available menus is a pain, and there’s no object representation (you reference the scenes by string).
Instead, what I prefer to do, is to use prefabs.
Set up the menu its own scene, oh sure. But set it up inside some parent gameobject, and turn that into a prefab.
Now you can have a ‘UI’ variable on some script in your level scene that you stick the prefab in. This way you can swap out menus easily on the fly.
Furthermore, when debugging, if you want to just see how a menu looks in a scene… just drag it in, and bam, you got the UI in there.
One benefit of using LoadLevelAdditiveAsync is that you can load new menus asychronously and in the background and poll until they have been fully loaded, then show them in your scene. This is good if you have some pretty intensive menus, and if you’re on lower-end mobile devices can give a smoother experience.
Something to think about as well.
When you unload a scene (or gameObjects loaded additively from the scene), just destroy the root gameObject, then call Resources.UnloadUnusedAssets.
If you do go ahead with LoadLevelAdditive I would test out on a few devices as previously I had some artifacts on Android and iOS devices when menus were loaded this way.
Seems overkill for a menu. Occasionally if I need uber flexibility I’ll generate menus dynamically on the fly. (See my YouTube channel for some examples). But for most applications just setting each panel active and inactive as needed does the job fine.
If you have intensive menus with lots of textures designed for 2560x1440 resolution, this can eat up a lot of memory, in this case LoadLevelAdditiveAsync is your best option.