AssetBundle (アセットバンドル) を使用するには、以下の手順で行います。各手順に関する詳細情報は、本セクションの他のページを参照してください。
AssetBundle (アセットバンドル) へのアセットの割り当ては、以下の手順で行います。
アセットバンドルを割り当てる上での各種方法についての詳細は、このドキュメンテーションの アセットバンドル用のアセットの準備 のページをご覧ください 。
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);
}
}
このスクリプトは、 Assets メニューの下部に “Build AssetBundles” というメニューアイテムを作成します。このメニューアイテムは、コードを、そのタグに関連付けられた関数内で実行します。 Build AssetBundles をクリックすると、ビルドダイアログとともにプログレスバーが表示されます。これが、ある特定のアセットバンドル名でラベル付けされたアセットの全てを取得し、assetBundleDirectory によって定義されたパスにある 1 つのフォルダー内にそれらを配置します。
このコードの機能についての詳細は、Unity ドキュメンテーションの アセットバンドルのビルド を参照してください。
この手順はユーザーごとに異なります。サードパーティのホスティングサイトにアセットバンドルをアップロードする場合は、この時点で行ってください。ローカルに限定した開発を行っている場合で、全てのアセットバンドルをディスク上に置く場合は、この手順は飛ばして次の手順に進んでください。
ローカルストレージからロードする場合は、 AssetBundles.LoadFromFile API を使用できます。以下はその例です。
public class LoadFromFileExample extends MonoBehaviour {
function 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
はバンドルファイルのパスを必要とします。
アセットバンドルを独自にホスティングしていて、それをゲームにダウンロードする必要がある場合は、 UnityWebRequest API を使用できます。以下はその使用例です。
IEnumerator InstantiateObject()
{
string uri = "file:///" + Application.dataPath + "/AssetBundles/" + assetBundleName;
UnityEngine.Networking.UnityWebRequest request = UnityEngine.Networking.UnityWebRequest.GetAssetBundle(uri, 0);
yield return request.Send();
AssetBundle bundle = DownloadHandlerAssetBundle.GetContent(request);
GameObject cube = bundle.LoadAsset("Cube");
GameObject sprite = bundle.LoadAsset("Sprite");
Instantiate(cube);
Instantiate(sprite);
}
GetAssetBundle(string, int)
はアセットバンドルの場所の URI およびダウンロードしたいバンドルのバージョンを必要とします。この例ではローカルファイルを指していますが、文字列 URI はアセットバンドルがホスティングされている URL のどれでも指すことができます。
UnityWebRequest はアセットバンドルを扱うためのハンドルである 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.