2013-06-05 31 views
1

我想加載一個場景,但我從標題中得到錯誤,而我根本不知道爲什麼,因爲我在AssetBundle上調用Unload(false)。有人能幫我嗎?謝謝。無法加載緩存的AssetBundle。一個相同名稱的文件已經從另一個AssetBundle加載

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

IEnumerator DownloadAndCache(){ 
    // Wait for the Caching system to be ready 
    while (!Caching.ready) 
     yield return null; 

    // Load the AssetBundle file from Cache if it exists with the same version or download and store it in the cache 
     using(WWW www = WWW.LoadFromCacheOrDownload (BundleURL, version)){ 
      yield return www; 

      if (www.error != null) 
       throw new Exception("WWW download had an error:" + www.error); 
      AssetBundle bundle = www.assetBundle; 

      bundle.LoadAll(); 

      AsyncOperation async = Application.LoadLevelAsync("main"); 
      Debug.Log (async.progress); 
      yield return async; 

      bundle.Unload(false); 
     } 
} 
+0

在這行不錯誤發生?捆綁包中包含級別'main'? – Kay

+1

我'解決'(不)問題。在Unity 4.1.2中,當我停止遊戲時,從AssetBundle崩潰加載場景。 http://answers.unity3d.com/questions/382154/problem-in-loading-scene-from-asset-bundle.html#comment-469348 and here http://answers.unity3d.com/questions/ 373556 /如何-可以-I-停我的編輯器 - 從 - 崩潰 - 每次-i.html – ahsoka

回答

2

,如果你不希望使用卸載()函數清除緩存資產捆綁使用後: -

Caching.CleanCache(); 

而且每個事物將正常工作,但你必須每一個下載資產包您可以在使用軟件包後清除緩存。

或者你也可以做到這一點的方式

首先調用DoNotDestroyOnLoad()的(保持通過了參考)函數start()函數,使一個靜態變量來存儲資產包的參考當您使用WWW.LoadFromCacheOrDownload下載資產時,請將引用分配給靜態變量並在使用後卸載資產。如果您不卸載資產捆綁包並再次使用WWW.LoadFromCacheOrDownload,則會引發與您所述相同的錯誤。

假設如果您使用資產包加載場景,那麼在退出場景之前卸載存儲在該靜態變量中的資產包引用。這就是爲什麼使用靜態變量的原因,以便我們可以從任何腳本訪問它並在需要時小心版本時卸載它。

class LoadScene:MonoBehaviour{ 
***public static AssetBundle refrenceOfAsset;*** 
private AssetBundle assetBundle; 

void Start(){ 
***DoNotDestroyOnLoad(gameObject);*** } 
protected IEnumerator LoadTheScene() 
{ 
    if (!Caching.IsVersionCached(url, version)){ 
     WWW www = WWW.LoadFromCacheOrDownload(url, version); 
     yeild return www; assetBundle = www.assetBundle; 
     ***refrenceOfAsset = assetBundle;*** 
     www.Dispose(); 
     // Do what ever you want to do with the asset bundle but do not for get to unload it 
     refrenceOfAsset.Unload(true); 
     } 
    } 
    else{ 
     Debug.Log("Asset Already Cached..."); 
     if(refrenceOfAsset!=null) 
     refrenceOfAsset.Unload(true); 
    } 

} 

或者您可以訪問統一http://docs.unity3d.com/Manual/keepingtrackofloadedassetbundles.html

相關問題