我试图加载一个场景,但是我从标题中得到了错误,我只是不知道为什么,因为我在AssetBundle上调用Unload(false)。有人能帮我吗?谢谢。
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);
}
}发布于 2016-05-31 13:53:15
如果不想使用Unload()函数,则在使用资产包之后清除缓存:-
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
https://stackoverflow.com/questions/16938903
复制相似问题