1

LoadFromCacheOrDownload 関数を使用していますが、Unity 内からアセット バンドルをダウンロードしてから、デバイスのローカルにアセット バンドルをロードする方法がわかりません。ありがとうございました!現在使用しているコードは次のとおりです。

using UnityEngine.UI;
using System;


public class LoadScenes : MonoBehaviour
{
public string sceneAssetBundle;
public string sceneName;
public string sName;
public string bName;
public string BundleURL;
public int version;
public int downloaded = 0;


IEnumerator Start() {
    if (downloaded == 0){
        using (WWW www = WWW.LoadFromCacheOrDownload (BundleURL, version)) {
            yield return www;
            if (www.error != null)
                throw new Exception ("WWW download had an error:" + www.error);
            if (www.error == null) {
                AssetBundle bundle = www.assetBundle;
            }
        }
            if (Caching.ready == true) {
                downloaded = 1;
                yield return InitializeLevelAsync (sceneName, true);

            }
        }
    }


public void getScene(string sName){
    sceneName = sName;

}

public void getBundle(string bName){
    sceneAssetBundle = bName;

}


public IEnumerator InitializeLevelAsync (string levelName, bool isAdditive)
{
    // This is simply to get the elapsed time for this phase of AssetLoading.
    float startTime = Time.realtimeSinceStartup;

    // Load level from assetBundle.
    AssetBundleLoadOperation request = AssetBundleManager.LoadLevelAsync(sceneAssetBundle, levelName, isAdditive);
    if (request == null)
        yield break;
    yield return StartCoroutine(request);

    // Calculate and display the elapsed time.
    float elapsedTime = Time.realtimeSinceStartup - startTime;
    Debug.Log("Finished loading scene " + levelName + " in " + elapsedTime + " seconds" );
}
}
4

2 に答える 2

2

アセットがいつダウンロードされるかを知るために使用する必要PlayerPrefsがあります。再度ダウンロードする前に、ダウンロードされているかどうかを確認してください。ここに例があります

if (PlayerPrefs.GetInt("AssetLoaded", 0) == 0)
{
    //Save that we have down loaded Asset 
    PlayerPrefs.SetInt("AssetLoaded", 1);
    Debug.Log("Asset has NOT been downloaded. Downloading....");

    //DOWNLOAD ASSET HERE
    //.......
}
else
{
    Debug.Log("Asset already loaded. Can't download it again!");
}

これを質問のコードに組み込むには:

IEnumerator Start() {
    if (PlayerPrefs.GetInt("AssetLoaded", 0) == 0){
        Debug.Log("Asset has NOT been downloaded. Downloading....");
        using (WWW www = WWW.LoadFromCacheOrDownload (BundleURL, version)) {
            yield return www;
            if (www.error != null)
                throw new Exception ("WWW download had an error:" + www.error);
            if (www.error == null) {
                AssetBundle bundle = www.assetBundle;
            }
        }
            if (Caching.ready == true) {
                downloaded = 1;
                //Save that we have down loaded Asset 
                PlayerPrefs.SetInt("AssetLoaded", 1);
                yield return InitializeLevelAsync (sceneName, true);

            }
        }else
          {
             Debug.Log("Asset already loaded. Can't download it again! Loading it instead");
             yield return InitializeLevelAsync (sceneName, true);
          }
    }

リセットするには、 を呼び出すだけPlayerPrefs.DeleteKey("AssetLoaded");です。

于 2016-05-16T19:31:35.950 に答える