为什么 Resources.Load 在没有 systemTypeInstance 的情况下不起作用
Why Resources.Load does not work without systemTypeInstance
Unity Resource.Load
function 有两个重载方法 - 一个只包含资产路径,另一个要求类型 systemTypeInstance。没有关于为什么我应该使用一种或另一种方法的信息。但是当我尝试 Resources.Load 只有一个参数时它返回 null,并且添加第二个参数一切正常。例如
Sprite asd = Resources.Load("test") as Sprite;
returns 空,但是
Sprite asd = Resources.Load("test", typeof(Sprite)) as Sprite;
returns 精灵在 "Assets/Resources/test"
。为什么我必须添加第二个参数?为什么需要它?
在 Unity 中,Sprite
是一种纹理类型,因此不带类型参数的调用很可能会返回 Texture2D
然后无法投射(精灵本质上是一种子资产如果您在 Unity 的 Project window.
中查看纹理
这应该很容易通过一些调试来确定。尝试先将其保存为 Object
,附加调试器,然后在强制转换之前查看该调用实际返回的内容:
Object testObj = Resources.Load("test");
Texture text = testObj as Texture2D; // Probably a valid Texture2D
Sprite spr = testObj as Sprite; // Probably null
What for I have to add second param? Why is it necessary?
1.Sprite asd = Resources.Load("test") as Sprite;
not 工作并且 returns null 使用下面的重载:
public static Object Load(string path);
2.Sprite asd = Resources.Load("test", typeof(Sprite)) as Sprite;
按预期工作是使用下面的重载:
public static Object Load(string path, Type systemTypeInstance);
还有其他使用泛型的重载。
还有其他使用 generics 的重载,但这两个在这个问题中很重要。
当您导入图像并将 Texture Type 设置为 Sprite(2D and UI) 时,它会变成Sprite
. The problem is that the first overload in #1 is using Unity's Object
的类型作为默认类型而不是 Sprite
。它还returns统一Object
。它看起来像这样:
public static Object Load(string path)
{
return Load(path, typeof(Object));
}
第一个代码的问题是当您使用 as Sprite
执行从 Unity Object
到 Sprite
的转换时。如果您按照 Unity 的 Object
进行转换,它应该可以工作。
示例:
以下使用不带第二个参数的 Resources.Load
函数 works:
//OK
UnityEngine.Object asd = Resources.Load("test") as UnityEngine.Object;
asd 变量 不应 为 null
。它应该加载该对象,但您 不能 将其转换为 Sprite
。
现在,当您将其转换为 Sprite
时,它将是 null
,因为您 无法 将 Unity 的 Object
转换为 Sprite
。阅读以下代码的评论:
//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;
同样,它 returns null
由于转换为 Sprite
。
这就是添加第二个加载函数的原因:
public static Object Load(string path, Type systemTypeInstance);
There is no info why I should use one method or another.
1.第一个用于加载预制件。当你创建一个GameObject时,你可以把它做成一个prefab然后放到Resources文件夹中。
//OK
UnityEngine.Object asd = Resources.Load("test") as UnityEngine.Object;
//OK
GameObject obj2 = asd as GameObject;
或
//OK
UnityEngine.Object asd = (GameObject)Resources.Load("test");
2.第二个用于加载图片(.png,jpeg)和音频、视频、fbx等文件。文件。
例如加载Resources文件夹下的fbx文件:
//OK
Mesh model = Resources.Load("test", typeof(Mesh)) as Mesh;
//Not Ok
Mesh model2 = Resources.Load("test") as Mesh;
最后,如果要加载的 Object
不是 预制件,则无法将 UnityEngine.Object
转换为任何其他类型。如果它是预制件,则可以。如果它是图像、音频、视频之类的文件,则无法将 Object
转换为该类型的文件。甚至无法将其转换为GameObject
.
Unity Resource.Load
function 有两个重载方法 - 一个只包含资产路径,另一个要求类型 systemTypeInstance。没有关于为什么我应该使用一种或另一种方法的信息。但是当我尝试 Resources.Load 只有一个参数时它返回 null,并且添加第二个参数一切正常。例如
Sprite asd = Resources.Load("test") as Sprite;
returns 空,但是
Sprite asd = Resources.Load("test", typeof(Sprite)) as Sprite;
returns 精灵在 "Assets/Resources/test"
。为什么我必须添加第二个参数?为什么需要它?
在 Unity 中,Sprite
是一种纹理类型,因此不带类型参数的调用很可能会返回 Texture2D
然后无法投射(精灵本质上是一种子资产如果您在 Unity 的 Project window.
这应该很容易通过一些调试来确定。尝试先将其保存为 Object
,附加调试器,然后在强制转换之前查看该调用实际返回的内容:
Object testObj = Resources.Load("test");
Texture text = testObj as Texture2D; // Probably a valid Texture2D
Sprite spr = testObj as Sprite; // Probably null
What for I have to add second param? Why is it necessary?
1.Sprite asd = Resources.Load("test") as Sprite;
not 工作并且 returns null 使用下面的重载:
public static Object Load(string path);
2.Sprite asd = Resources.Load("test", typeof(Sprite)) as Sprite;
按预期工作是使用下面的重载:
public static Object Load(string path, Type systemTypeInstance);
还有其他使用泛型的重载。
还有其他使用 generics 的重载,但这两个在这个问题中很重要。
当您导入图像并将 Texture Type 设置为 Sprite(2D and UI) 时,它会变成Sprite
. The problem is that the first overload in #1 is using Unity's Object
的类型作为默认类型而不是 Sprite
。它还returns统一Object
。它看起来像这样:
public static Object Load(string path)
{
return Load(path, typeof(Object));
}
第一个代码的问题是当您使用 as Sprite
执行从 Unity Object
到 Sprite
的转换时。如果您按照 Unity 的 Object
进行转换,它应该可以工作。
示例:
以下使用不带第二个参数的 Resources.Load
函数 works:
//OK
UnityEngine.Object asd = Resources.Load("test") as UnityEngine.Object;
asd 变量 不应 为 null
。它应该加载该对象,但您 不能 将其转换为 Sprite
。
现在,当您将其转换为 Sprite
时,它将是 null
,因为您 无法 将 Unity 的 Object
转换为 Sprite
。阅读以下代码的评论:
//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;
同样,它 returns null
由于转换为 Sprite
。
这就是添加第二个加载函数的原因:
public static Object Load(string path, Type systemTypeInstance);
There is no info why I should use one method or another.
1.第一个用于加载预制件。当你创建一个GameObject时,你可以把它做成一个prefab然后放到Resources文件夹中。
//OK
UnityEngine.Object asd = Resources.Load("test") as UnityEngine.Object;
//OK
GameObject obj2 = asd as GameObject;
或
//OK
UnityEngine.Object asd = (GameObject)Resources.Load("test");
2.第二个用于加载图片(.png,jpeg)和音频、视频、fbx等文件。文件。
例如加载Resources文件夹下的fbx文件:
//OK
Mesh model = Resources.Load("test", typeof(Mesh)) as Mesh;
//Not Ok
Mesh model2 = Resources.Load("test") as Mesh;
最后,如果要加载的 Object
不是 预制件,则无法将 UnityEngine.Object
转换为任何其他类型。如果它是预制件,则可以。如果它是图像、音频、视频之类的文件,则无法将 Object
转换为该类型的文件。甚至无法将其转换为GameObject
.