用户工具

站点工具


侧边栏



Unity3D中文文档

script:unityengine:classes:resources:resources

Resources 资源

class in UnityEngine

Description 描述

The Resources class allows you to find and access Objects including assets.

Resources类允许你从指定的路径查找或访问资源。

In the editor, Resources.FindObjectsOfTypeAll can be used to locate assets and scene objects.

在编辑器,Resources.FindObjectsOfTypeAll能用来定位资源和场景对象。

All assets that are in a folder named “Resources” anywhere in the Assets folder can be accessed via the Resources.Load functions. Multiple “Resources” folders may exist and when loading objects each will be examined.

所有资源必须在项目Assets文件内的任意Resources文件夹中,可以通过Resources.Load访问。允许有多个Resources文件夹,每次加载对象是会自动检查。

In Unity you usually don't use path names to access assets, instead you expose a reference to an asset by declaring a member-variable, and then assign it in the inspector. When using this technique Unity can automatically calculate which assets are used when building a player. This radically minimizes the size of your players to the assets that you actually use in the built game. When you place assets in “Resources” folders this can not be done, thus all assets in the “Resources” folders will be included in a build.

在Unity中通常不需要使用路径名来访问资源,相反你可以通过声明一个成员变量来暴露一个资源的引用,然后在检视面板中指定它。使用这个技巧的时候Unity可以在构建的时候自动计算哪个资源被使用。这从根本上最大限度地减少了实际用于游戏的资源的尺寸。当你放资源在“Resources”文件夹中时,所有在“Resources”文件夹中的资源都将被打包编译包含在游戏中。

Another disadvantage of using path names is that it leads to less reusable code since scripts will have specific hard coded requirements on where the used assets are placed. On the other hand using references that are exposed in the inspector are self-documenting and immediately obvious to the user of your script.

另一个使用路径名的缺点是,缺乏可重用性,因为脚本对于使用的资源具有硬编码要求。另一方面使用暴露在检视面板中的资源引用,是自文档化的,对于使用脚本的用户来说也是立竿见影的。

However there are situations where it is more convenient to fetch an asset by its name instead of linking to it in the inspector. Essentially whenever it is inconvenient to assign the reference to the object in the inspector. For example you might want to create a game object procedurally from a script and for example assign a texture to a procedurally generated mesh.

然而,有些情况下按照名称比在检视面板中取回一个资源更方便,尤其是当在检视面板中指定引用是不方便的时候。例如你或许想从脚本创建一个游戏物体,为程序生成的网格赋值一个纹理。

Some loaded assets, most notably textures, can use up memory even when no instance exists in the scene. To reclaim this memory when the asset is no longer needed, you can use Resources.UnloadUnusedAssets.

有些已加载的资源,最明显的是纹理,即使在场景没有实例,也最占内存。当资源不再需要时,你可以使用Resources.UnloadUnusedAssets回收内存。

JavaScript:

#pragma strict
function Start() {
	var go: var = GameObject.CreatePrimitive(PrimitiveType.Plane);
	var rend: var = go.GetComponent.<Renderer>();
	rend.material.mainTexture = Resources.Load("glass") as Texture;
}

C#:

using UnityEngine;
using System.Collections;
 
public class ExampleClass : MonoBehaviour {
    void Start() {
        var go = GameObject.CreatePrimitive(PrimitiveType.Plane);
        var rend = go.GetComponent<Renderer>();
        rend.material.mainTexture = Resources.Load("glass") as Texture;
    }
}

Static Functions 静态函数

FindObjectsOfTypeAllReturns a list of all objects of Type type.
返回所有type类型对象的一个列表。
LoadLoads an asset stored at path in a Resources folder.
加载储存在Resources文件夹中path处的资源。
LoadAllLoads all assets in a folder or file at path in a Resources folder.
加载Resources文件夹中的path文件夹或者文件中的所有资源。
LoadAsyncAsynchronously loads an asset stored at path in a Resources folder.
异步加载Resources文件夹中的资源。
UnloadAssetUnloads assetToUnload from memory.
从内存卸载指定的资源。
UnloadUnusedAssetsUnloads assets that are not used.
卸载未使用的资源。
script/unityengine/classes/resources/resources.txt · 最后更改: 2016/08/02 05:27 (外部编辑)