2016-05-16 86 views
1

我正在使用LoadFromCacheOrDownload函數,我只是不確定如何才能從統一內下載資產捆綁包,然後爲本地加載資產捆綁包裝置。謝謝!下面是我目前使用的代碼:如何下載AssetBundle然後將其保存在設備中

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"); 
} 
} 
+0

那麼哪部分代碼不起作用?我看着它,它看起來很好... – Programmer

+0

嗯,它第一次工作,但是當我嘗試再次運行時,它試圖再次下載該捆綁包,Unity不讓我。使用Unload()對我來說並不理想,因爲我們將來會使用大型資產包。 –

+0

這就是AssetBundle的用途。它會下載並緩存資產。爲了再次下載資產,您首先必須先卸載它。你爲什麼試圖多次下載它? – Programmer

回答

1

,您應該使用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");

+0

非常感謝,它的工作!我需要做的唯一修改是添加yield返回InitializeLevelAsync(sceneName,true);在其他地方。 –

+0

@IsabelleBrandelero我剛剛做到了,我也添加了一種方法來重置它。看看更新的代碼。不用謝! – Programmer

相關問題