Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why Resources.Load does not work without systemTypeInstance

Unity Resource.Load function has two overloaded methods - one just with path to asset and another asks for Type systemTypeInstance. There is no info why I should use one method or another. But when I tried Resources.Load with only one param it returned null, and with adding second param everything was ok. For example Sprite asd = Resources.Load("test") as Sprite; returns null, but Sprite asd = Resources.Load("test", typeof(Sprite)) as Sprite; returns sprite at "Assets/Resources/test". What for I have to add second param? Why is it neccessary?

like image 736
leshanbog Avatar asked Dec 05 '25 01:12

leshanbog


1 Answers

What for I have to add second param? Why is it necessary?

1.Sprite asd = Resources.Load("test") as Sprite; which do not work and returns null uses the overload below:

public static Object Load(string path);

2.Sprite asd = Resources.Load("test", typeof(Sprite)) as Sprite; which works as expected is using the overload below:

public static Object Load(string path, Type systemTypeInstance);

There are other overloads which uses generics.


There are other overloads which uses generics but these two matter in this question.

When you import your image and set the Texture Type to Sprite(2D and UI), it becomes a type of Sprite. The problem is that the first overload in #1 is using Unity's Object as the default Type not Sprite. It also returns Unity Object. It looks like this:

public static Object Load(string path)
{
    return Load(path, typeof(Object));
}

The issue with the first code is when you performed the casting from Unity Object to Sprite with as Sprite. If you do the casting as Unity's Object, it should work.

Example:

The following which uses the Resources.Load function without the second parameter works:

//OK
UnityEngine.Object asd = Resources.Load("test") as UnityEngine.Object;

The asd variable should not be null. It should load the object but you can't cast it to Sprite.

Now, when you cast it to Sprite, it will be null because you can't convert Unity's Object to Sprite. Read the comments from the code below:

//OK
UnityEngine.Object asd = Resources.Load("test") as UnityEngine.Object;
//NOT OK(NULL)
Sprite obj1 = asd as Sprite; 
//NOT OK(NULL) + InvalidCastException
Sprite obj2 = (Sprite)asd;

Again, it returns null due to the cast to Sprite.


This is why the second load function is added:

public static Object Load(string path, Type systemTypeInstance);

There is no info why I should use one method or another.

1.The first one is used to load a prefab. When you create a GameObject, you can make it a prefab and put it in the Resources folder.

//OK
UnityEngine.Object asd = Resources.Load("test") as UnityEngine.Object;
//OK
GameObject obj2 = asd as GameObject;

OR

//OK
UnityEngine.Object asd = (GameObject)Resources.Load("test");

2.The second one is used to load files such as images(.png, jpeg) and audio, video, fbx. files.

For example, loading fbx file in the Resources folder:

//OK
Mesh model = Resources.Load("test", typeof(Mesh)) as Mesh;
//Not Ok
Mesh model2 = Resources.Load("test") as Mesh;

Finally, you can't convert UnityEngine.Object to any other type if the Object to load is not a prefab. You can if it is a prefab. If it is a file like image, audio, video, it would be impossible to covert the Object to that type of file. It will even be impossible to convert it to GameObject.

like image 196
Programmer Avatar answered Dec 07 '25 15:12

Programmer



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!