AssetBundle (アセットバンドル) を使用するには、以下の手順で行います。各手順に関する詳細情報は、本セクションの他のページを参照してください。
AssetBundle (アセットバンドル) へのアセットの割り当ては、以下の手順で行います。
/
で区切ります。例えば、environment/forest
というアセットバンドル名は、environment
というサブフォルダー配下に forest
という名前のバンドルを作成します。ノート: プロジェクト内のフォルダーにアセットバンドルとラベルを割り当てることができます。デフォルトでは、そのフォルダー内のすべてのアセットがアセットバンドルに割り当てられ、フォルダーと同じラベルが付けられます。ただし、個々のアセットに対するアセットバンドルの割り当てが優先されます。
アセットバンドルを割り当てる上での各種方法についての詳細は、このドキュメンテーションの アセットバンドル用のアセットの準備 のページを参照してください。 。
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
が定義するパスのフォルダーに置きます。
詳細は、上の アセットバンドルをビルドする を参照してください。
ローカル ストレージからロードしたい場合は、以下のように 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
はバンドルファイルのパスを必要とします。
アセットバンドルを独自にホスティングしていて、それをアプリケーションにダウンロードする必要がある場合は、UnityWebRequestAssetBundle
API を使用できます。以下はその使用例です。
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)
はアセットバンドルの場所の URL およびダウンロードしたいバンドルのバージョンを必要とします。この例ではローカルファイルを指していますが、string url
はアセットバンドルがホスティングされている URL のどれでも指すことができます。
UnityWebRequestAssetBundle クラスにはアセットバンドルを処理するためのハンドル DownloadHandlerAssetBundle
があり、リクエストからアセットバンドルを取得します。
使用するメソッドに関わらず、これでアセットバンドルオブジェクトが利用可能になりました。そのオブジェクトから LoadAsset<T>(string)
を使用する必要があります。この LoadAsset<T>(string)
は、ロードしようとしているアセットのタイプ T
と、文字列 (string) としてバンドル内のオブジェクトの名前を必要とします。これは、アセットバンドルから読み込んでいるオブジェクトを、どんなものでも返します。この返されたオブジェクトは、Unity 上の他のオブジェクトと全く同じように使用できます。例えば、シーン内でゲームオブジェクトを作成したい場合は、単純に Instantiate(gameObjectFromAssetBundle)
を呼び出すだけで行うことができます。
アセットバンドルをロードする API に関する詳細は、Unity ドキュメンテーションの アセットバンドルを使いこなす を参照してください。
Did you find this page useful? Please give it a rating:
Thanks for rating this page!
What kind of problem would you like to report?
Thanks for letting us know! This page has been marked for review based on your feedback.
If you have time, you can provide more information to help us fix the problem faster.
Provide more information
You've told us this page needs code samples. If you'd like to help us further, you could provide a code sample, or tell us about what kind of code sample you'd like to see:
You've told us there are code samples on this page which don't work. If you know how to fix it, or have something better we could use instead, please let us know:
You've told us there is information missing from this page. Please tell us more about what's missing:
You've told us there is incorrect information on this page. If you know what we should change to make it correct, please tell us:
You've told us this page has unclear or confusing information. Please tell us more about what you found unclear or confusing, or let us know how we could make it clearer:
You've told us there is a spelling or grammar error on this page. Please tell us what's wrong:
You've told us this page has a problem. Please tell us more about what's wrong:
Thank you for helping to make the Unity documentation better!
Your feedback has been submitted as a ticket for our documentation team to review.
We are not able to reply to every ticket submitted.