文档

Unity Engine

Unity Engine

Resources

The Resources class allows you to find and access objects including assets.
阅读时间3 分钟最后更新于 12 小时前

Definition

  • Type: Class
  • Namespace: UnityEngine
  • Assembly: UnityEngine.CoreModule
public sealed class Resources

Remarks

When you build your project, Unity includes all assets in every
Resources
folder in the built Player. You can then load these assets at runtime using Resources.Load or Resources.LoadAsync. Unity doesn't create a
Resources
folder when you create a project, so you must create one yourself.
Your project can contain any number of
Resources
folders. To add another
Resources
folder, create a new folder named
Resources
anywhere inside your project's
Assets
folder or its subfolders, or inside a package. Unity treats every folder with this name as a
Resources
folder and combines the contents of all of them. Unity doesn't include
Resources
folders that are inside an
Editor
folder in Player builds.
When you load an asset, the path you pass to Resources.Load or Resources.LoadAsync starts from the
Resources
folder that contains the asset, and doesn't include the file extension. For example, to load
Assets/Resources/Enemies/Goblin.prefab
or
Assets/Characters/Resources/Enemies/Goblin.prefab
, you pass the same path:
Enemies/Goblin
. If both assets exist, you can't control which one Unity loads, so give assets in different
Resources
folders unique paths or names.
Use Resources.FindObjectsOfTypeAll to locate assets and scene objects in the Editor.
One way to access assets in Unity is to expose a reference to an asset by declaring a member-variable, then assign it in the Inspector, instead of using the direct path to an asset. This allows Unity to automatically calculate which assets are used when building a Player and minimizes the size of the Player to only contain the assets used in your application. However, this method fixes the references in the Inspector which makes them less flexible.
You can use the Resources class to load assets at runtime by specifying their path as a string. For example, you might want to create a GameObject procedurally from a script and 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.
For more information, refer to the Introduction to the Resources system.

Examples

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

Static Methods

Method

Description

EntityIdIsValidChecks whether an EntityId corresponds to a valid Object in memory. The Object could have been deleted or not loaded into memory yet.
EntityIdsToObjectListTranslates an array of entity IDs to a list of Object references.
EntityIdsToValidArrayConverts an array of EntityIds to an array of bool values indicating whether a given EntityId from entityIds corresponds to a valid Object in memory. The Object could have been deleted or not loaded into memory yet.
EntityIdToObjectTranslates an EntityId to an object reference.
FindObjectsOfTypeAllObtains all objects of type
type
.
LoadLoads an asset stored at
path
in a Resources folder using an optional
systemTypeInstance
filter.
LoadAllLoads all assets in a folder or file at
path
in a Resources folder.
LoadAsyncAsynchronously loads an asset stored at
path
in a Resources folder.
UnloadAssetUnloads
assetToUnload
from memory.
UnloadUnusedAssetsUnloads assets that are not used.