Releases: kamanii24/AssetBundleManager
AssetBundleManager v1.6
更新内容
AssetBundleの差分チェックがCRCからHash照合へ変更しました。
この変更に伴い、AssetBundleManager.Initialize の引数の渡し方が変わります。
v1.5以前
AssetBundleManager.Initialize (bundleDirURL);
↓
v1.6
AssetBundleManager.Initialize(manifestURL, (bool isComplete)=>
{
// 初期化完了
});
manifestURLとは、AssetBundleのビルド時に生成されるプラットフォーム名が命名されたAssetBundleManifestファイル(iOS、Androidなど)のパスのことを指します。
また、旧バージョンでは同期的に行われていたInitializeが、UnityWebRequestで応答を待つためコールバックで処理待ちを行うように変更されています。
詳細は README で
コールバック以外では AssetBundleManager.Initialized
プロパティで初期化済みの判定を取得できます。
ビルド環境
Unity 2018.4.8f1 macOS
Mojave 10.14.6
AssetBundleManager v1.5
更新内容
-
AssetBundleダウンロード後に行われていたAssetBundleの自動ロードを無効にするbool引数を追加。
DownloadAssetBundleの第二引数にfalseを渡すことでダウンロード後にAssetBundleをメモリへロードする処理が実行されなくなります。
DownloadAssetBundle(string downloadAssetBundle, false, AssetBundleDownloadProgress handler);
-
Unloadメソッドにparams引数(可変長引数)を追加。
ビルド環境
Unity 2018.2.11f1
macOS Mojave 10.14.2
AssetBundleManager v1.4
更新内容
- ダウンロードしたAssetBundleがキャッシュされず、毎回ダウンロードされてしまう問題を修正。
ビルド環境
Unity 2018.2.11f1
macOS Mojave 10.14
AssetBundleManager v1.3
更新内容
- GetAssetAsyncで無限ループに入る問題の修正。
- Unity2017で使用する際に、
UnityWebRequestAssetBundle.GetAssetBundle
でエラーを吐く問題の修正。 - LoadAssetBundleで指定したAssetBundleが全てロード済みだった場合、
IndexOutofRangeException array index is out of range.
エラーが発生する問題の修正。
ビルド環境
Unity 2018.2.11f1
macOS Mojave 10.14
AssetBundleManager v1.2
更新内容
- AssetBundleLabelExporter追加
UnityメニューのTools>AssetBundle Labels>Exportから、AssetBundleのラベルをcsvでエクスポートできます。
ビルド環境
Unity 2018.2.11f1
macOS Mojave 10.14
AssetBundleManager v1.1
更新内容
- AssetBundleのAES暗号化機能の廃止
ダウンロード、キャッシュロード時の復号化処理で端末によってはプチフリーズが起き、ユーザビリティに大きく影響を与えてしまうということと、暗号化したAssetBundleの運用が本アセットに依存してしまうと判断したため廃止しました。 - AssetBundleのビルド機能の廃止
AssetBundleBrowserとの併用を推奨します。 - namespaceの追加、実装部分の命名変更、コールバック引数の変更
詳細は下記よりご確認ください。
リファレンス
初期設定
AssetBundleManagerを使用するクラスに using を追加します。
using KM2;
初回にInitializeをコールし、AssetBundleが含まれるサーバ上のディレクトリパスを指定します。
一度設定すれば以降、値の変更しない限り設定する必要はありません。
AssetBundleManager.Initialize (bundleDirURL);
AssetBundleのダウンロード
関数
DownloadAssetBundle(string downloadAssetBundle, AssetBundleDownloadProgress handler);
DownloadAssetBundle(string[] downloadAssetBundles, AssetBundleDownloadProgress handler);
デリゲート
AssetBundleDownloadProgress(ulong downloadedBytes, ulong totalBytes, int fileIndex, bool completed, string error);
UnityWebRequest によるGET通信で AssetBundleManager.Initialize で指定したリモートディレクトリから downloadAssetBundles 指定したAssetBundleをダウンロードします。
コールバック引数でダウンロード対象のAssetBundleのファイルサイズを受け取れるので、進捗値をディスプレイすることが可能です(実装は下記参照)。
実装
private void Start()
{
string[] downloadAssetBundles = { bundle_1, bundle_2, bundle_3 };
AssetBundleManager.DownloadAssetBundle(downloadAssetBundles, Downloading);
}
// ダウンロード更新
private void Downloading(ulong downloadedBytes, ulong totalBytes, int fileIndex, bool isComplete, string error)
{
// ダウンロードBytesサイズ更新
print(downloadedBytes + " bytes / "+ totalBytes + "bytes");
// ダウンロード完了
if (isComplete)
{
print("Donwload completed.");
}
}
AssetBundleのロード
関数
LoadAssetBundle(string loadAssetBundle, AssetBundleLoaded handler);
LoadAssetBundle(string[] loadAssetBundles, AssetBundleLoaded handler);
デリゲート
AssetBundleLoaded(AssetBundle[] loadedAssetBundles, string error);
ロードの完了通知はコールバックデリゲートで受け取れます。
loadAssetBundles で指定したAssetBundleがキャッシュ内に存在しない場合は、ダウンロードします。
DownloadAssetBundleとの違いは、delegateで完了通知だけを受け取ることができるので、ダウンロードのプログレス更新が不要な場合や解放されているAssetBundleを個別にロードする場合に使用されます。
実装
private void Start()
{
string[] loadAssetBundles = { bundle_1, bundle_2, bundle_3 };
AssetBundleManager.LoadAssetBundle(loadAssetBundles, Loaded);
}
// AssetBundleのロード完了
private void Loaded(AssetBundle[] assetBundles, string error)
{
if(error != null)
{
foreach(var ab in assetBundles) print(ab.name + " is loaded.");
}
}
ロードしたAssetBundleからアセットを取得する
型指定なし
Object obj = AssetBundleManager.GetAsset (bundleName, assetName);
ジェネリック型指定
AudioClip clip = AssetBundleManager.GetAsset<AudioClip> (bundleName, assetName);
非同期 ジェネリック型指定
GetAssetAsync<GameObject> (bundleName, assetName, (GameObject go) => {
if (go != null) {
Instantiate (go, Vector3.zero, Quaternion.identity);
}
});
ロードされているAssetBundleを確認する
AssetBundle本体
AssetBundle[] ab = AssetBundleManager.GetAllLoadedAssetBundles();
AssetBundle名
string[] abNames = AssetBundleManager.GetAllLoadedAssetBundleNames();
AssetBundleを破棄する
ロードしたAssetBundleは明示的に破棄するまでメモリに保持され続けるため、不要になったAssetBundleはUnloadで破棄する必要があります。
AssetBundleManager.Unload(true); // trueにした場合、AssetBundleからロード済みのアセットも破棄されます
AssetBundle名を指定して個別に破棄することも可能です。
AssetBundleManager.Unload(true, bundleName);
ビルド環境
Unity 2018.2.11f1
macOS Mojave 10.14
AssetBundleManager v1.0
お知らせ
Unity5.6以下に対応する最初で最後のリリースです。
含まれている機能
- AssetBundleのダウンロード・差分更新
- AssetBundleビルド時に圧縮形式の選択(LZMA, LZ4, 非圧縮)
- AESによるAssetBundleの暗号化・復号化
リファレンス(v1.0)
初期設定
初回にInitializeをコールし、AssetBundleが含まれるサーバ上のディレクトリとAssetBundleのバージョンを指定します。
バージョン指定は任意です。
一度設定すれば以降、値の変更しない限り設定する必要はありません。
AssetBundleManager.Initialize (bundleDirURL, 1);
AssetBundleのダウンロード
DownloadAssetBundleでダウンロードするAssetBundle名を指定します。
コールバックデリゲートでダウンロードの進捗値を取得できます。
string[] bundleNames = { bundle_1, bundle_2, bundle_3 };
AssetBundleManager.DownloadAssetBundle (bundleNames, ((float progress, int fileIndex, bool isComplete, string error) => {
// エラー処理
if (error != null) {
Debug.Log("ダウンロードエラー");
}
// 進捗処理
else if (isComplete) {
Debug.Log("ダウンロード完了");
}
else {
int per = (int)(progress*100f);
Debug.Log(per+"%");
Debug.LoG(fileIndex + "/" + bundleNames.Length);
}
}));
AssetBundleのロード
LoadAssetBundleでロードするAssetBundle名を指定します。
ロードの完了通知はコールバックデリゲートで受け取れます。
string[] bundleNames = { bundle_1, bundle_2 };
AssetBundleManager.LoadAssetBundle (bundleNames, ((bool isSuccess, string error) => {
if (isSuccess) {
Debug.Log("ロード成功");
}
else {
Debug.Log("ロード失敗 : "+error);
}
}));
アセットの取得
ロードしたAssetBundleから必要なアセットを取得するにはGetAssetに取得したいアセット名と、それが含まれるAssetBundle名をしていします。
取得したアセットはObject型なので適当な型にキャストします。
型指定なし
Object obj = AssetBundleManager.GetAsset (bundleName, assetName);
ジェネリック型指定
AudioClip clip = AssetBundleManager.GetAsset<AudioClip> (bundleName, assetName);
非同期
GetAssetAsync (bundleName, assetName, ((Object asset, bool isSuccess) => {
if (isSuccess) {
Instantiate ((GameObject)asset, Vector3.zero, Quaternion.identity);
}
}));
ロードされているAssetBundleを確認する
GetAllAssetBundleNameで現在ロードされているAssetBundleをstring配列で全て取得できます。
string[] bundles = GetAllAssetBundleName();
AssetBundleを破棄する
ロードしたAssetBundleは明示的に破棄するまでメモリに保持され続けるため、不要になったAssetBundleはUnloadで破棄する必要があります。
AssetBundleManager.Unload();
AssetBundle名を指定して個別に破棄することも可能です。
AssetBundleManager.Unload(bundleName);
暗号化したAssetBundleをビルドする
Unityのメニューバー AssetBundles/Open Config を選択し、暗号化のためのキー文字列を設定します。
Saltは8文字以上である必要があります。
キー文字列の設定後、AssetBundles/Enable AES Cryption にチェックをつけて Build AssetBundles でビルドします。
暗号化されたAssetBundleもダウンロード時に自動的に復号され、通常のAssetBundleと同等に扱うことができます。
ビルド環境
Unity 2017.3.0p3
macOS High Sierra 10.13.3
Unity-Chan ライセンス
本リポジトリには、UnityChanがAssetsとして含まれています。 以下のライセンスに従います。
このアセットは、『ユニティちゃんライセンス』で提供されています。このアセットをご利用される場合は、『キャラクター利用のガイドライン』も併せてご確認ください。