- Home /
Levels changing with UI best practice
I have a game with many levels. Hovewer on each level I have the SAME UI (buttons bause, health, ect.). Here is some BEST practice to realize scene loading with UI saving?
Levels as scenes? Or other way? I have realized loading levels from prefabs, but It was not best idea... I also can put same ui in each level...
Thanks!
Answer by hyperi0n · Jun 30, 2016 at 12:08 PM
"DontDestroyOnLoad" sounds like something that might interest you:
https://docs.unity3d.com/ScriptReference/Object.DontDestroyOnLoad.html
However, it will probably not just work like magic - you will have to be careful when UI elements refer to objects (e.g. the player) that DO get destroyed when loading a new level.
So you would probably have to have some kind of script that resets the UI after a level change, fixing references to objects, resetting values etc.
However, it may still be better than creating/copying the UI in every level scene, because then when you want to make changes, you will have to do it in every scene. So I guess using don't destroy on load would be better.
As an altrnative, you could also make a prefab out of your UI and simply instantiate it after a level has been loaded.
Regarding what system to use (scenes/prefabs) for changing of levels, that depends alot on the kind of game you are building. If its e.g. a shooter where the levels must be hand built in advance, scenes is probably the way to go. If you have a simple puzzle game like breakout, then building the scene out of prefabs via a level-generator script may be better.
I hope this helps! Good Luck!
I realy know about DontDestroyOnLoad=)
However I need to thome best practice. Some advice from guys who make It in own game. DontDestroyOnLoad - ok. But if I go to main menu or level selecting I need to delete UI.
I have wariant to have some scene only with UI, put all in prefab and spawn on each level. I have some arcade with level selecting, store end levels.
Thanks!
Answer by DroidifyDevs · Jun 30, 2016 at 01:19 PM
use DontDestroyOnLoad for the UI.
As for the levels, I don't know what game you're making, but I'd do one of these:
1: Make different scenes for each level 2: Make a script to spawn objects for every level.