- Home /
Resources.Load() or Prefab manager?
I have a few Prefabs that I need to instantiate/use at runtime, and so far I have been using Resources.Load() to add them to the scene during gameplay.
But I was wondering if having a Prefab manager, present in every scene, would be a better idea. For example, I could set the number of Prefabs I will need for each scene, and drag&drop them in the Prefab manager in the inspector. That way, the Prefabs are ready and I can use PrefabManager.AddPrefab(myPrefab), which would instantiate a new copy, or perhaps from another class: GameObject prefabCopy = Instantiate(PrefabManager.prefab01)...
Is Resources.Load() slow? Not slow enough to worry about performance? What do you guys think?
Thanks!
Stephane
Answer by cdrandin · Aug 23, 2013 at 06:38 AM
Both Resources.Load and Instantiate are relatively slow, but a better solution could be to pool/cache your GameObject. Obviously at first you are going to need to Instantiate your object at least once, but if you have multiples of the object in your scene and these objects are recurring. When said object is no longer needed you simply "hide" it and move it away from the scene and then when an object of that type is needed you simply show it back again and the object will start up like before, this depends on how you set up your scripts.
This way is far less expensive and not at all hard to manage.
Edit: With your gameobject and you could even create its own Instantiate function which takes whatever parameters needed that will re-position it to where it is needed and make sure to reset variables if need be, example an enemies health back to 100 instead of its dead state (0 hp).
Thanks for the quick answer cdrandin!
The only time I currently use Resources.Load() is during setup (Start/Awake) or when I need to display an on-screen message to the player, for example, I use a custom SR$$anonymous$$essage.Show$$anonymous$$essage(...) function which uses Resources.Load() to load a HUD$$anonymous$$essage Prefab into the scene, and then destroys it a few seconds later.
Considering this, I don't think a pool manager would be a benefit. I am developing for IOS/Android platforms, and I don't know if Resources.Load() is the better option there. Are there any issues concerning the use of Resources.Load() on mobile devices?
Thanks!