- Home /
What is the cost of using Resources.Load?
I'm working on a driving game that requires up to 20 traffic car prefabs in the world at a time. They will spawn and unspawn regularly, so the total number in game will probably fluctuate between 5 and 20. My question is about the most efficient way to do this (I can't find information on it anywhere). Is there much cost of creating GameObjects during gameplay?
Option 1 - Create my prefab instances on startup, store them at an unused world location, then move them in and out as I need them.
Option 2 - Make the traffic cars a resource and create them during gameplay using Resources.Load() then instantiating GameObjects.
It's frequently better not to use Resources.Load, but ins$$anonymous$$d link prefabs using public variables in the inspector. This way Unity can manage your assets intelligently.
I thought so too; I'm happy to see you agree. Linking them that way makes the editor initialize the variable at runtime, right? (Just checking to see I have this down right)
Answer by CHPedersen · Nov 25, 2011 at 09:12 PM
Resources.Load involves accessing the harddrive, which is notoriously slow because it contains moving parts. So you're better off loading all the resources you need at startup, and then either manipulate their visibility or instantiate prefabs without using Resources.Load to provide the model.
For your particular scenario, however, I'd recommend creating a public Transform variable in your script, and then drag the car prefab onto it in the editor. Calling Instantiate by passing in that prefab Transform causes objects to be spawned without constantly looking at the harddrive to acquire the model to clone.
Answer by sicklepickle · Aug 27, 2012 at 08:20 AM
Just posted this on a similar topic, but gonna repost similar info anyway.
In my case preloading multiple versions of every possible spawn (over 40 available types) was using almost all of the 3GS\Droid's memory. On the flipside not creating a pool meant tons of stuttering. The solution was to create just one temp copy of each object and use newObj = (GameObject)Instantiate( pool[index].gameObject );
In my case this solved the problem entirely - Instantiate() seems be much faster than Resources.Load().
Instantiate and Resource.Load are a different thing. To compare them performance-wise makes no sense.
Check out the documentation.
http://docs.unity3d.com/ScriptReference/Resources.Load.html
http://docs.unity3d.com/ScriptReference/Object.Instantiate.html
Your answer
Follow this Question
Related Questions
Loading resources into a ScriptableObject instead of Resources.Load? 1 Answer
ridiculous load time using Resources.Load() on android build 0 Answers
Referencing prefab in code without using editor or Resources.Load? 0 Answers
Why does resources.load not work or what am I doing wrong? 1 Answer
Resources not loading in APK 0 Answers