Version: Unity 6.3 LTS (6000.3)
LanguageEnglish
  • C#

Resources

class in UnityEngine

/

Implemented in:UnityEngine.CoreModule

Suggest a change

Success!

Thank you for helping us improve the quality of Unity Documentation. Although we cannot accept all submissions, we do read each suggested change from our users and will make updates where applicable.

Close

Submission failed

For some reason your suggested change could not be submitted. Please <a>try again</a> in a few minutes. And thank you for taking the time to help us improve the quality of Unity Documentation.

Close

Cancel

Description

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

When you build your project, all assets in any Resources folders are included in the built Player. You can then load these assets at runtime using Resources.Load. You must create the Resources folder. Unity doesn't automatically create a Resources folder when you create a project. Your project can contain multiple Resources folders.

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.

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
EntityIdsToObjectListTranslates an array of entity IDs to a list of Object references.
EntityIdToObjectTranslates an EntityId to an object reference.
FindObjectsOfTypeAllReturns a list of all objects of Type T.
InstanceIDsToValidArrayTranslates an array of instance IDs to an array of bools indicating whether a given instance ID from instanceIDs corresponds to a valid Object in memory. The Object could have been deleted or not loaded into memory yet.
LoadLoads the asset of the requested type stored at path in a Resources folder.
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.