Version: 2018.2

Resources

class in UnityEngine

マニュアルに切り替える

説明

アセットを含むオブジェクトにアクセスすることができます。

エディター上で 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.

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.

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.

いくつかの読み込み済みのアセット、特にテクスチャではシーン上にインスタンスが存在しない場合もメモリを多く消費します。アセットが不要となったときに、このメモリを再び使用可能とするには Resources.UnloadUnusedAssets を使用できます。

Note: The Resources folder in Assets needs to be created before it is used. It is not created when a new Project is created.

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 関数

FindObjectsOfTypeAll type で指定した型のすべてのオブジェクトを取得します
Loadパスを設定し、Resources フォルダーにあるアセットを読み込みます
LoadAllResources フォルダー内にあるフォルダー・ファイルのパスを設定し、すべてを読み込みます。
LoadAsyncパスを設定し、Resources フォルダーにあるアセットを非同期で読み込みます
UnloadAssetメモリから assetToUnload をアンロードします
UnloadUnusedAssets使用していないアセットをアンロードします
Copyright © 2023 Unity Technologies
优美缔软件(上海)有限公司 版权所有
"Unity"、Unity 徽标及其他 Unity 商标是 Unity Technologies 或其附属机构在美国及其他地区的商标或注册商标。其他名称或品牌是其各自所有者的商标。
公安部备案号:
31010902002961