- Home /
Why use Resources.Load(string path, Type systemTypeInstance);?
What is the benefit to using Resources.Load(string path, Type systemTypeInstance); over Resources.Load(string path);?
They both require a casting. Neither is easier to read (because of the required casting). There is no obvious benefit to passing in the type of the resource.
What are the benefits to including the type?
(I am referring to http://docs.unity3d.com/ScriptReference/Resources.Load.html.)
Answer by CHPedersen · Feb 02, 2015 at 09:53 PM
I think knowing the type allows it to skip searching some collections of particular assets. Suppose you have a Texture2D called "Tex1" and a Material called "Tex1" too, located in the same folder. That would be unwise, but not technically illegal. Using the one with the type allows you to specify that you want e.g. the Texture2D, not the Material.
I disagree that both require casting. The type filtered code sample in the documentation you linked to does not use a cast, it sets the mainTexture of a material directly in the example.
By the way, I do agree somewhat with your question in that I've never understood why there isn't a generic version of Resources.Load, e.g.:
Texture2D test = Resources.Load<Texture2D>("Test");
That would prevent the necessary cast too, I guess. But that doesn't exist, and there's probably a reason why. I just don't know it. ;)
Actually there is now a generic overload :) just scroll down in the docs.
Oooh. That's beautiful! Didn't see that at first. ;-) Thanks for pointing that out!
Answer by Bunny83 · Feb 02, 2015 at 10:10 PM
Its possible to have multiple assets packed into one, see AssetDatabase.AddObjectToAsset
In such a case the type parameter allows you to select a certain type. Also have a look at Resources.LoadAll