ロバメモ - 素人のUnity覚書と奮闘記

素人のUnity覚書と奮闘記

AssetBundle 読み込み中を表示したい

AssetBundleを使って見る のスクリプトに追加で、AssetBundleを読み込み中に Please wait的なものを表示する方法をメモ。 robamemo.hatenablog.com

概要

ローダーをprefabにして、1秒たっても読み込みできなければprefabを表示する。
1秒置くのは、すぐに表示されるようにするとチラチラして却って目障りになるので。
読み込めたら、prefabを削除。

ローダーをつくる

f:id:nico-taniku:20170619093422p:plain:w300
こんな感じのを作り、prefabにして、Resouceフォルダに入れる。
これを、空GameObjectのstageにスクリプトで配置する。
f:id:nico-taniku:20170619093710p:plain:w500

ローダーを配置/削除するスクリプト

 GameObject loader;

    void SetLoader ()
    {
        GameObject prefab = Resources.Load<GameObject> ("loader");
        loader = Instantiate (prefab);
        GameObject stage = GameObject.Find ("stage");
        loader.transform.SetParent (stage.transform);
        loader.transform.localPosition = Vector3.zero;
        loader.transform.localScale = Vector3.one;
    }

    void DeleteLoader ()
    {
        CancelInvoke ();
        Destroy (loader);
    }

ローダーを表示するスクリプト

赤色を追加

 IEnumerator Import ()
    {
        <span style="color: #d32f2f">Invoke ("SetLoader", 1f);</span>

        //キャッシュの読み込み準備待ち
        while (!Caching.ready) {
            yield return null;
        }
        //ダウンロード
        WWW www = WWW.LoadFromCacheOrDownload (bundleURL, version);
        while (!www.isDone) {
            yield return null;
        }
        //アセットバンドルをキャッシュ
        chach = www.assetBundle;

        //dictionaryに保管
        string[] names = chach.GetAllAssetNames ();
        bundleList.Add (bundleName, chach.LoadAsset<GameObject> (names [0]));

        //リクエストの解放
        www.Dispose ();

        OnComplete ();

    }

ローダーを削除するスクリプト

 void OnComplete ()
    {
        
        GameObject prefab = bundleList [bundleName];

        if (prefab != null) {
            if (now != null) {
                Destroy (now);
            }
            now = Instantiate (prefab);
            GameObject stage = GameObject.Find ("stage");
            now.transform.SetParent (stage.transform);
            now.transform.localPosition = Vector3.zero;
            now.transform.localScale = Vector3.one;
            <span style="color: #d32f2f">DeleteLoader ();</span>

            //保存
            PlayerPrefs.SetString ("presentScene", bundleName);

        }
    }

Move.cs

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Move : MonoBehaviour
{
    static public Move instance;
    GameObject loader;
    GameObject now;

    //バージョン
    int version = 2;

    //AssetBundle URL
    string bundleURL;

    //対象のバンドル名
    string bundleName;

    //キャッシュ
    AssetBundle chach;

    //キャッシュからバンドル名で呼び出すためのdictionary
    Dictionary<string, GameObject> bundleList = new Dictionary<string, GameObject> ();


    void Awake ()
    {
        if (instance == null) {
            instance = this;
            DontDestroyOnLoad (this.gameObject);
        } else {
            Destroy (this.gameObject);
        }

    }

    public void OnMove (string sceneName)
    {
        bundleName = sceneName;

        if (!bundleList.ContainsKey (sceneName)) {
            //URL設定
           #if UNITY_EDITOR
            bundleURL = "file://" + Application.dataPath + "/AssetBundles/Build/Android/" + bundleName;
           #elif UNITY_ANDROID
            bundleURL = "http://hogehoge/Android/"+bundleName;
           #elif UNITY_IOS
            bundleURL = "http://hogehoge/iOS/"+bundleName;
           #endif

            //バージョンチェック
            if (PlayerPrefs.GetInt ("bundleVersion", -1) != version) {
                Caching.CleanCache ();
                PlayerPrefs.SetInt ("bundleVersion", version);
            }

            StartCoroutine ("Import");
        } else {
            OnComplete ();
        }
    }

    IEnumerator Import ()
    {
        Invoke ("SetLoader", 1f);

        //キャッシュの読み込み準備待ち
        while (!Caching.ready) {
            yield return null;
        }
        //ダウンロード
        WWW www = WWW.LoadFromCacheOrDownload (bundleURL, version);
        while (!www.isDone) {
            yield return null;
        }
        //アセットバンドルをキャッシュ
        chach = www.assetBundle;

        //dictionaryに保管
        string[] names = chach.GetAllAssetNames ();
        bundleList.Add (bundleName, chach.LoadAsset<GameObject> (names [0]));

        //リクエストの解放
        www.Dispose ();

        OnComplete ();

    }

    void OnComplete ()
    {
        
        GameObject prefab = bundleList [bundleName];

        if (prefab != null) {
            if (now != null) {
                Destroy (now);
            }
            now = Instantiate (prefab);
            GameObject stage = GameObject.Find ("stage");
            now.transform.SetParent (stage.transform);
            now.transform.localPosition = Vector3.zero;
            now.transform.localScale = Vector3.one;
            DeleteLoader ();

            //保存
            PlayerPrefs.SetString ("presentScene", bundleName);

        }
    }

    void SetLoader ()
    {
        GameObject prefab = Resources.Load<GameObject> ("loader");
        loader = Instantiate (prefab);
        GameObject stage = GameObject.Find ("stage");
        loader.transform.SetParent (stage.transform);
        loader.transform.localPosition = Vector3.zero;
        loader.transform.localScale = Vector3.one;
    }

    void DeleteLoader ()
    {
        CancelInvoke ();
        Destroy (loader);
    }
}