type = sprite, texture, image…
url = the path of the resource
Thanks for see my question
type = sprite, texture, image…
url = the path of the resource
Thanks for see my question
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.
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)
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;