Resources.Load

描述

加载存储在 Resources 文件夹中 path 的指定类型资产。

如果找到该方法,该方法将返回 path 处的资产,否则返回 null。
请注意,path 不区分大小写,并且不能包含文件扩展名。Unity 中的所有资产名称和路径都使用正斜杠,因此在 path 中使用反斜杠将不起作用

path 相对于项目 Assets 文件夹中的任何名为 Resources 的文件夹。可以使用多个 Resources 文件夹。如果有多个 Resources 文件夹,则不能重复使用资产名称。

例如,一个项目可能具有名为 Assets / Resources/Assets / Guns / Resources/Resources 文件夹。路径不需要在字符串中包含 AssetsResources,例如,加载 Assets / Guns / Resources / Shotgun.prefab 中的 GameObject 只需要 Shotgun 作为 path。此外,如果存在 Assets / Resources / Guns / Missiles / PlasmaGun.prefab,则可以使用 Guns / Missiles / PlasmaGun 作为 path 字符串进行加载。
如果有多个 Resources 文件夹,则不能重复使用资产名称。

如果有多个不同类型的资产具有相同的名称,并且您没有指定类型,那么 Unity 返回的对象将是非确定性的,因为潜在的候选对象不会以任何特定方式排序。相反,请使用 Resources.Load<T>(path) 来指定所需的资产。


声明

public static T Load(string 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"); } }

声明

public static Object Load(string path);

声明

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

参数

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: