LoginSignup
8
10

More than 5 years have passed since last update.

AssetBundleのサンプルを作成した時のメモ

Last updated at Posted at 2016-02-24

使用したUnityのバージョン

5.3.2f

AssetBundleとは

  • Asset(資源)管理の仕組み
  • インターネット経由でリソースのDLができる
  • バージョン管理ができる

AssetBundleの作成

  1. AssetBundleの名前をマーキング
  2. Editor Scriptを実行
  3. サーバにアップロード

1. AssetBundleの名前をマーキング

AssetBundleに含めるディレクトに名前をマーキングする。

スクリーンショット 2016-02-24 16.55.20.png

2. Editor Scriptを実行

AssetBundleを生成するためのスクリプトを作成し、
Editorフォルダに配置する。

ExportAssetBundle.cs
using UnityEngine;
using System.Collections;
using UnityEditor;
using System.IO;


public class ExportAssetBundle
{
    [MenuItem("Export/AssetBundle")]
    static void Export()
    {
        Directory.CreateDirectory(Application.streamingAssetsPath);
        BuildPipeline.BuildAssetBundles(Application.streamingAssetsPath);
    }
}

このスクリプトをEditorフォルダに配置すると、
Unityのメニューに「Export」という項目が増える。

スクリーンショット 2016-02-24 17.05.10.png

「Export -> AssetBundle」を選択すると、
AssetBundle生成処理が実行される。

3. サーバにアップロード

上記のスクリプトを実行すると、
StreamingAssetフォルダが生成されるので、
このフォルダをサーバにアップロードする。

AssetBundleの読み込み(サーバ)

サーバにあるAssetBundleからリソースをロードする流れは、次のとおり。

  1. WWW.LoadFromCacheOrDownloadで、AssetBundleをダウンロード
  2. ダウンロードしたAssetBundleから目的のリソースをLoadAssetメソッドで読み込む

ソースは以下のようになる。

CachingLoadExample.cs
using UnityEngine;
using System.Collections;

public class CachingLoadExample : MonoBehaviour
{
    string bundleURL = "http://localhost:9000/assets/StreamingAssets/sprites";
    string assetName = "Test";
    int version = 0;

    void Start()
    {
        StartCoroutine(DownloadAndCache());
    }

    IEnumerator DownloadAndCache()
    {
        while(!Caching.ready)
            yield return null;

        using(WWW www = WWW.LoadFromCacheOrDownload(bundleURL, version)) {
            yield return www;

            if (www.error != null) {
                throw new UnityException("WWW download had an error" + www.error);
            }

            AssetBundle bundle = www.assetBundle;

            Instantiate(bundle.LoadAsset(assetName));

            bundle.Unload(false);
        }
    }
}

AssetBundleの読み込み(ローカル)

Unityのプロジェクト内にあるStreamingAssetsからリソースを読み込むこともできる。

AssetBundleの読み込みはAssetBundle.LoadFromFileAsyncメソッドで行う。

LoadAssetBundle.cs
using UnityEngine;
using System.Collections;

public class LoadAssetBundle : MonoBehaviour
{
    string assetName = "Test";

    string AssetPath {
        get {
            return Application.streamingAssetsPath + "/sprites";
        }
    }

    IEnumerator Start()
    {
        var resultAssetBundle = AssetBundle.LoadFromFileAsync(AssetPath);

        yield return new WaitWhile(() => resultAssetBundle.isDone == false);

        var assetbundle = resultAssetBundle.assetBundle;
        var resultObject = assetbundle.LoadAssetAsync<GameObject>(assetName);

        yield return new WaitWhile(() => resultObject.isDone == false);

        GameObject.Instantiate(resultObject.asset);
        assetbundle.Unload(false);
    }
}

参考

WWW
http://docs.unity3d.com/ja/current/ScriptReference/WWW.html

AssetBundle
http://docs.unity3d.com/ja/current/ScriptReference/AssetBundle.html

Unity 5 の AssetBundle について (浅く) まとめてみた
http://www.slideshare.net/monry84/20150522-31-unity

【Unity】Unity 5.3からAssetBundleはどう変わるのか…まとめ
http://tsubakit1.hateblo.jp/entry/2015/12/16/233336

Unity5の新しいAssetBundle機能を使ってサーバーからAssetBundleをダウンロードする
http://kazuooooo.hatenablog.com/entry/2015/06/14/150019

8
10
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
8
10