- Home /
What is the difference between Resources.Load("url") and Resources.Load("url") as type ?
type = sprite, texture, image.... url = the path of the resource
Thanks for see my question :)
Answer by Lysander · Jan 02, 2018 at 12:23 PM
The difference is actually pretty big, sometimes. Functions that use the generic method (angle braces) can just immediately use the type argument internally, but the non-generic function could involve extra work to get the result, or boxing (casting a value type to Object) when returning the result, which is painfully inefficient. Also, functions that offer both generic and non-generic options typically require casting the result of the non-generic function to the appropriate type afterward to actually use it, the safety of which is not guaranteed at compile-time, so if the function returns an object that isn't the type you think it's going to be, and you try casting it to that type, you may throw errors as a result.
In the case of Resources.Load though, it's mostly a convenience factor. Using the generic version with angle braces automatically filters the result to that object type, so you can be sure that the operation is safely getting only the object that you specifically want it to get. If you were to use Resources.Load("SomeFileName") and "SomeFileName" matches both a texture and a sprite, you can't be 100% sure which you'll get back, whereas if you use the generic function, it'll only find the object that matches the specific type that you're looking for.
Resources.Load("SomeFileName", typeof(Texture2D)) is pretty much identical to using Texture2D in the generic version though in this case, except that it returns an Object that needs to be cast to a Texture2D (using "as Texture2D" just after it or using (Texture2D) in front of it) in order to actually use it as a Texture2D and not as an Object. You can be sure that the result (if one was found) is a Texture2D object though, so it's always safe to cast it.
You also can't use a System.Type object as a generic parameter, in those angle braces, so there are situations where the non-generic version is absolutely necessary.
I would always advocate using the generic version of a function, if one is available and if you know the type of object that you're looking for. Compile-time safety is great when it's offered, and not having to cast the result is more performant and easier to read.
Answer by ReiFlorian · Jan 02, 2018 at 11:30 AM
The correct question is: What is the difference between Resources.Load<"type">("url") and Resources.Load("url") as type ? (I don't know why the forum don't let me put <"type"> without quotes)
Because it reads anything between angle braces as being a markup tag for HT$$anonymous$$L. $$anonymous$$arkup tags can be anything at all, because just because Browser X doesn't know how to process them doesn't mean Browser Y won't- they still need to be invisible to readers.
Also, please post comments as comments ins$$anonymous$$d of answers- you can use the Options -> Convert to Comment button after the fact as well.
In one of my codes I tried to change the sprite of the image I put in the canvas, but Resources.Load<"Sprite">("img1") as Sprite doesn't works and Resources.Load("img1") as Sprite, yes, and this is my question, what is the difference between this two syntax becouse I don't understand why one works and the other doesn't.
Please don't tell me references with others program$$anonymous$$g languages, this is C#
I was telling you why you couldn't put angle braces into the website like you were wanting to do- because it's reading it as markup tags. And please convert this "answer" to a comment.
Answer by OneCept-Games · Jan 02, 2018 at 12:20 PM
Be sure to load your resource file into correct object type. If it is an image file your should load it into a Texture or Texture2D object, and then attach it to your sprite's renderer.
Renderer rend = go.GetComponent<Renderer>();
rend.material.mainTexture = Resources.Load("glass") as Texture;
Your answer
![](https://koobas.hobune.stream/wayback/20220612145640im_/https://answers.unity.com/themes/thub/images/avi.jpg)