- Home /
What's best: Instantiate or LoadLevelAdditive?
I'm a little undecided on this one! Basically, I am loading the HUD on level start, and I have tried 2 different approaches.
My first approach was to create different HUD prefabs eg; HUD_TILT_CONTROLS.prefab, HUD_TOUCH_CONTROLS.prefabs, ect... Then I use a ControlsManager class where I assign all different HUD prefabs to an array of GameObject, in the editor. Than at runtime, I Instantiate() the chosen HUD prefab, and if the user changes the HUD in the pause menu for example, I then Destroy() the current HUD prefab in the scene, and Instantiate() the new one chosen by the user. This approach works fine, but I don't like the fact that the Instantiated HUD GameObject is a (clone), and I try not to Instantiate() too many things when I don't have to.
My second approach is to create new scenes for each HUD preset, and depending on the user's choice, I load the corresponding scene with LoadLevelAdditive(). I got the idea from the Penelope IOS tutorial. The nice thing about this approach is that I could also throw in other GameObjects in each scene, like the player for example, that way I can do all my testing/tweaking in each HUD scene without having to mess with the actual level scenes. And a bonus, at least in my mind, is that I don't have to Instantiate() anything, and I get the actual GameObject(s), instead of clones.
Is one approach better then the other? Like performance-wise, or organization-wise? What do you guys recommend for loading HUDs at runtime?
Thanks for your time!
Stephane
Answer by Tim-Michels · Oct 24, 2012 at 06:33 AM
Is it an option to have a kind of parent HUD-prefab with all presets childed to that gameObject? That way you only have one prefab you can change in whatever scene you like and it will automatically be updated in all your other scenes. The only thing you have to do "manually" is to make sure that your parent-prefab is present in each scene, but you could also Instantiate that prefab through code.
If that's not an option, I still suggest the prefabs solution, because it will be easier to maintain references to your objects and manage your HUD correctly.
Answer by Tim-Michels · Oct 23, 2012 at 09:14 AM
I personally would use prefabs instead of loading another scene additive. I don't really understand what you mean with "I don't like the fact that the Instantiated HUD GameObject is a (clone)", but I think that Instantiating your prefabs is a good choice. You could also place all HUD-objects in your scene and Enable/Disable the ones you need. That way you don't have to Instantiate/Load at runtime which will never give you any frame-hiccups. It basically all depends on the number of prefabs you want to use. If you only switch between a couple of prefabs, I would just put them all in my scene and disable the ones I don't need. Disabled gameObjects will cost practically nothing, so this is in a lot of cases the best (performance-wise) choice.
Hey Tim, thanks for your answer.
The enabling/disabling approach is nice since like you said, disabled objects don't really use any resources, and there's no need for Instantiating them. But what I forgot to mention in my question above is the fact that I don't want to have to manually add each preset to each new scene I build, so I have to create/get them at runtime.
With the Prefabs solution, I just assign each Prefab the my Controls$$anonymous$$anager GameObject - which is automatically added at, or before, runtime - that way I don't have to worry about "placing" the Prefab presets manually for each scene.
With the LoadLevelAdditive solution, it's even easier since I don't have to assign anything to Controls$$anonymous$$anager, because it doesn't need to "look" for any Prefabs, it just loads the correct HUD scene.
Like I said, both solutions works fine, but I don't know which one is the better choice for my case, and which one is faster. I am wondering if I'm overlooking any cons for the LoadLevelAdditive approach? Like what could potentially be a problem using this technique ?
Hi Tim, yes it is an option, I could definitively do that! I'll give it a try when I get home :)
If you move this comment into an answer, I'll accept it as the correct answer.
Thanks for the suggestions!
Stephane
Tim, frame hiccups is, of course, nasty. LoadLevelAdditiveAsync() may be used to throw them away. But is seems like we have no hiccups only when both scenes share same objects to load, so they are all loaded in scene A and no hiccups occur when we call LoadLevelAdditiveAsync(B). I was hoping, that using LoadLevelAdditiveAsync() will result in faster loading of calling scene. What a dissapointment :( Looks like LoadLevelAdditive may be used only to logically separate objects in scenes. So does prefabs. $$anonymous$$nowing that we have prefab editor https://bitbucket.org/becksebenius/prefab-editor, it looks like the preffered way
Your answer
Follow this Question
Related Questions
The name 'Joystick' does not denote a valid type ('not found') 2 Answers
Scripting within a area in the game 1 Answer
Creating an object and setting fields before object begins running 1 Answer
Instantantiate relative to parents position 1 Answer
AddComponent() causes a "trying to create a MonoBehaviour using the 'new' keyword" warning 2 Answers