Version: 2022.3
言語: 日本語
アセットバンドル
アセットバンドル用のアセットの準備

アセットバンドルのワークフロー

AssetBundle (アセットバンドル) を使用するには、以下の手順で行います。各手順に関する詳細情報は、本セクションの他のページを参照してください。

Note: This section describes the creation of AssetBundles using the built-in BuildPipeline.BuildAssetBundles() API. A recommended, and more user friendly, alternative is to use the Addressables package.

アセットバンドルにアセットを割り当てる

AssetBundle (アセットバンドル) へのアセットの割り当ては、以下の手順で行います。

  1. Project ウィンドウから、バンドルに割り当てたいアセットを選択します。
  2. Inspector ウィンドウで該当オブジェクトを確認してください。
  3. Inspector ウィンドウの下部に、アセットバンドルとバリアントを割り当てるセクションがあります。左側のドロップダウンでアセットバンドルを割り当て、右側のドロップダウンでバリアントを割り当てます。
  4. 左側のドロップダウンで None をクリックすると、現在登録されているアセットバンドル名が表示されます。
  5. New をクリックして新しいアセットバンドルを作成します。
  6. 任意のアセットバンドル名を入力してください。アセットバンドル名によって、フォルダー構造を表現できます。サブフォルダーを加えるには、フォルダー名を / で区切ります。例えば、environment/forest というアセットバンドル名は、environment というサブフォルダー配下に forest という名前のバンドルを作成します。
  7. アセットバンドル名の選択または作成後、バリアント名の割り当てや作成を行いたい場合は、右側のドロップダウンメニューから同じ手順で行うことができます。バリアント名はアセットバンドルのビルドには必須ではありません。

Note: In the Inspector you can assign an AssetBundle to a folder in your Project. By default, all Assets in that folder are assigned to the same AssetBundle as the folder. The AssetBundle assignments for individual Assets takes precedence, however.

アセットバンドルを割り当てる上での各種方法についての詳細は、このドキュメンテーションの アセットバンドル用のアセットの準備 のページを参照してください。 。

アセットバンドルをビルドする 

Assets フォルダー内に Editor という名前のフォルダーを作成し、そのフォルダー内に、以下のスクリプトを置いてください。

using UnityEditor;
using System.IO;

public class CreateAssetBundles
{
    [MenuItem("Assets/Build AssetBundles")]
    static void BuildAllAssetBundles()
    {
        string assetBundleDirectory = "Assets/AssetBundles";
        if(!Directory.Exists(assetBundleDirectory))
        {
            Directory.CreateDirectory(assetBundleDirectory);
        }
        BuildPipeline.BuildAssetBundles(assetBundleDirectory, 
                                        BuildAssetBundleOptions.None, 
                                        BuildTarget.StandaloneWindows);
    }
}

このスクリプトは、 アセット メニューの下部に Build AssetBundles というメニュー項目を作成し、そのタグに関連付けられた関数のコードを実行します 。 Build AssetBundles をクリックすると、プログレスバーがビルドダイアログとともに表示されます。これにより、AssetBundle 名でラベル付けしたすべてのアセットを取得し、assetBundleDirectory が定義するパスのフォルダーに置きます。

Note: You can also define the content of AssetBundles programmatically instead of using the Inspector-based method described above. This is available by using a signature of BuildPipeline.BuildAssetBundles that accepts an array of AssetBundleBuild structures. In that case the list of desired Assets for each bundle are passed in, and any assignment to AssetBundles made in the Inspector is ignored.

詳細は、上の アセットバンドルをビルドする を参照してください。

アセットバンドルとアセットをロードする

ローカル ストレージからロードしたい場合は、以下のように AssetBundles.LoadFromFile APIを使用します。

public class LoadFromFileExample : MonoBehaviour {
    void Start() {
        var myLoadedAssetBundle 
            = AssetBundle.LoadFromFile(Path.Combine(Application.streamingAssetsPath, "myassetBundle"));
        if (myLoadedAssetBundle == null) {
            Debug.Log("Failed to load AssetBundle!");
            return;
        }
        var prefab = myLoadedAssetBundle.LoadAsset<GameObject>("MyObject");
        Instantiate(prefab);
    }
}

LoadFromFile はバンドルファイルのパスを必要とします。

If your AssetBundles are hosted online, or you are running on a platform that does not support direct file system access, then use the UnityWebRequestAssetBundle API. Here’s an example:

IEnumerator InstantiateObject()
{
    string url = "file:///" + Application.dataPath + "/AssetBundles/" + assetBundleName;        
    var request 
        = UnityEngine.Networking.UnityWebRequestAssetBundle.GetAssetBundle(url, 0);
    yield return request.Send();
    AssetBundle bundle = UnityEngine.Networking.DownloadHandlerAssetBundle.GetContent(request);
    GameObject cube = bundle.LoadAsset<GameObject>("Cube");
    GameObject sprite = bundle.LoadAsset<GameObject>("Sprite");
    Instantiate(cube);
    Instantiate(sprite);
}

GetAssetBundle(string, int) takes the URL of the location of the AssetBundle and the version of the bundle you want to download. This example points to a local file, but string url could point to any URL you have your AssetBundles hosted at.

UnityWebRequestAssetBundle クラスにはアセットバンドルを処理するためのハンドル DownloadHandlerAssetBundle があり、リクエストからアセットバンドルを取得します。

使用するメソッドに関わらず、これでアセットバンドルオブジェクトが利用可能になりました。そのオブジェクトから LoadAsset<T>(string) を使用する必要があります。この LoadAsset<T>(string) は、ロードしようとしているアセットのタイプ T と、文字列 (string) としてバンドル内のオブジェクトの名前を必要とします。これは、アセットバンドルから読み込んでいるオブジェクトを、どんなものでも返します。この返されたオブジェクトは、Unity 上の他のオブジェクトと全く同じように使用できます。例えば、シーン内でゲームオブジェクトを作成したい場合は、単純に Instantiate(gameObjectFromAssetBundle) を呼び出すだけで行うことができます。

アセットバンドルをロードする API に関する詳細は、Unity ドキュメンテーションの アセットバンドルを使いこなす を参照してください。

アセットバンドル
アセットバンドル用のアセットの準備
Copyright © 2023 Unity Technologies
优美缔软件(上海)有限公司 版权所有
"Unity"、Unity 徽标及其他 Unity 商标是 Unity Technologies 或其附属机构在美国及其他地区的商标或注册商标。其他名称或品牌是其各自所有者的商标。
公安部备案号:
31010902002961