All Projects → kamanii24 → AssetBundleManager

kamanii24 / AssetBundleManager

Licence: other
This script for easily use AssetBundle.

Programming Languages

C#
18002 projects

Projects that are alternatives of or similar to AssetBundleManager

AssetBundleCompiler
📦 Node.js wrapper around Unity3D's BuildPipeline to create AssetBundles from any files
Stars: ✭ 47 (-40.51%)
Mutual labels:  assetbundle
Utinyripper
GUI and API library to work with Engine assets, serialized and bundle files
Stars: ✭ 1,871 (+2268.35%)
Mutual labels:  assetbundle
UnityHotFix
Unity assetsbundle hotfix。Unity 资源热更新方案。
Stars: ✭ 24 (-69.62%)
Mutual labels:  assetbundle
CatAsset
Unity资源管理框架
Stars: ✭ 130 (+64.56%)
Mutual labels:  assetbundle
assetUpdater-core
AssetUpdater is a Unity plugin which helps developers build assetbundles and download it easily
Stars: ✭ 38 (-51.9%)
Mutual labels:  assetbundle
CosmosFramework
CosmosFramework is a lightweight plug-in Unity development framework . Has a rich Unity method extensions and toolchain. async/await syntax support, multi-network channel support.Long term support for this project
Stars: ✭ 176 (+122.78%)
Mutual labels:  assetbundle

AssetBundleManager

Imgur

初期設定

AssetBundleManagerを使用するクラスに using を追加します。

using KM2;

AssetBundleManagerの初期化

AssetBundleManagerを使用する前には必ずInitializeメソッドをコールし、初期化を行う必要があります。

AssetBundleManifestとAssetBundleが同じ階層に存在する場合

Initializeメソッドの引数に、AssetBundleManifest(AssetBundleビルド時に生成されるプラットフォーム名が付いたファイル)のパスを指定します。

引数にManifestのパスのみを指定した場合、AssetBundleがManifestと同階層にあるとして処理されます。

一度設定すれば以降、設定する必要はありません。

AssetBundleManager.Initialize(manifestURL, (bool isComplete)=>
{
	// 初期化完了
});

AssetBundleManifestとAssetBundleが異なる階層に存在する場合

AssetBundleがManifestファイルと異なる階層に存在する場合は、引数としてassetBundleDirectoryURLを指定することができます。

こちらも一度設定すれば以降、値の変更しない限り設定し直す必要はありません。

AssetBundleManager.Initialize(manifestURL, assetBundleDirectoryURL, (bool isComplete)=>
{
	// 初期化完了
});

AssetBundleのダウンロード

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.4.8f1 macOS Mojave 10.14.6

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].