加载存储在 Resources 文件夹中 path
的指定类型资产。
如果找到该方法,该方法将返回 path
处的资产,否则返回 null。
请注意,path
不区分大小写,并且不能包含文件扩展名。Unity 中的所有资产名称和路径都使用正斜杠,因此在 path
中使用反斜杠将不起作用。path
相对于项目 Assets 文件夹中的任何名为 Resources
的文件夹。可以使用多个 Resources 文件夹。如果有多个 Resources 文件夹,则不能重复使用资产名称。
例如,一个项目可能具有名为 Assets / Resources/
和 Assets / Guns / Resources/
的 Resources 文件夹。路径不需要在字符串中包含 Assets
和 Resources
,例如,加载 Assets / Guns / Resources / Shotgun.prefab
中的 GameObject 只需要 Shotgun
作为 path
。此外,如果存在 Assets / Resources / Guns / Missiles / PlasmaGun.prefab
,则可以使用 Guns / Missiles / PlasmaGun
作为 path
字符串进行加载。
如果有多个 Resources 文件夹,则不能重复使用资产名称。
如果有多个不同类型的资产具有相同的名称,并且您没有指定类型,那么 Unity 返回的对象将是非确定性的,因为潜在的候选对象不会以任何特定方式排序。相反,请使用 Resources.Load<T>(path)
来指定所需的资产。
path | 要加载的目标资源的路径。 |
T 请求的泛型参数类型的对象。
使用类型 T
的泛型参数类型过滤器,加载存储在 Resources 文件夹中 path
的指定类型资产。
如果找到该方法,并且其类型与请求的泛型参数类型匹配,则该方法将返回 path
处的资产,否则返回 null。您可以使用此重载在代码中减少类型转换,方法是提供泛型类型参数。这允许 Unity 为您执行 C# 类型转换。
// Loading assets from the Resources folder using the generic Resources.Load<T>(path) method using UnityEngine;
public class ExampleClass : MonoBehaviour { void Start() { //Load a text file (Assets/Resources/Text/textFile01.txt) var textFile = Resources.Load<TextAsset>("Text/textFile01");
//Load text from a JSON file (Assets/Resources/Text/jsonFile01.json) var jsonTextFile = Resources.Load<TextAsset>("Text/jsonFile01"); //Then use JsonUtility.FromJson<T>() to deserialize jsonTextFile into an object
//Load a Texture (Assets/Resources/Textures/texture01.png) var texture = Resources.Load<Texture2D>("Textures/texture01");
//Load a Sprite (Assets/Resources/Sprites/sprite01.png) var sprite = Resources.Load<Sprite>("Sprites/sprite01");
//Load an AudioClip (Assets/Resources/Audio/audioClip01.mp3) var audioClip = Resources.Load<AudioClip>("Audio/audioClip01"); } }
path | 要加载的目标资源的路径。 |
systemTypeInstance | 返回对象的类型过滤器。 |
Object 作为 Object 返回的请求资产。
使用可选的 systemTypeInstance
过滤器,加载存储在 Resources 文件夹中 path
的资产。
如果找到该方法,并且其类型与可选的 systemTypeInstance
参数匹配,则该方法将返回 path
处的资产,否则返回 null。
您可能需要将返回的对象强制转换为资产的实际关联 C# 类型,以便访问其方法和属性,或者将其与其他 Unity API 一起使用。
// Loading assets from the Resources folder using the Resources.Load(path) using UnityEngine; using System.Collections;
public class ExampleClass : MonoBehaviour { // Assigns a texture named "Assets/Resources/glass" to a Plane. void Start() { GameObject go = GameObject.CreatePrimitive(PrimitiveType.Plane); var rend = go.GetComponent<Renderer>(); rend.material.mainTexture = Resources.Load("glass") as Texture; } }
// Loading assets from the Resources folder using the Resources.Load(path, systemTypeInstance) using UnityEngine; using System.Collections;
public class ExampleClass : MonoBehaviour { // Instantiates a Prefab named "enemy" located in any Resources folder in your project's Assets folder. void Start() { GameObject instance = Instantiate(Resources.Load("enemy", typeof(GameObject))) as GameObject; } }
Did you find this page useful? Please give it a rating:
Thanks for rating this page!
What kind of problem would you like to report?
Thanks for letting us know! This page has been marked for review based on your feedback.
If you have time, you can provide more information to help us fix the problem faster.
Provide more information
You've told us this page needs code samples. If you'd like to help us further, you could provide a code sample, or tell us about what kind of code sample you'd like to see:
You've told us there are code samples on this page which don't work. If you know how to fix it, or have something better we could use instead, please let us know:
You've told us there is information missing from this page. Please tell us more about what's missing:
You've told us there is incorrect information on this page. If you know what we should change to make it correct, please tell us:
You've told us this page has unclear or confusing information. Please tell us more about what you found unclear or confusing, or let us know how we could make it clearer:
You've told us there is a spelling or grammar error on this page. Please tell us what's wrong:
You've told us this page has a problem. Please tell us more about what's wrong:
Thank you for helping to make the Unity documentation better!
Your feedback has been submitted as a ticket for our documentation team to review.
We are not able to reply to every ticket submitted.