public float Progress { get {return WWWLoader.Progress;}} // 進度 public CWWWDownloader(string fullUrl, string toPath) { ToPath = toPath; _SavePath = CResourceManager.GetAppDataPath() + "/" + ToPath; WWWLoader = new CWWWLoader(fullUrl); CResourceManager.Instance.StartCoroutine(StartDownload(fullUrl)); }
} // 進度 public CWWWDownloader(string fullUrl, string toPath) { ToPath = toPath; _SavePath = CResourceManager.GetAppDataPath() + "/" + ToPath; WWWLoader = new CWWWLoader(fullUrl); CResourceManager.Instance.StartCoroutine(StartDownload(fullUrl)); }
IEnumerator LoadAssetBundle(string relativeUrl) { var wwwLoader = CWWWLoader.Load(FullUrl); while (!wwwLoader.IsFinished) { Progress = wwwLoader.Progress / 2f; // 最多50%, 要算上Parser的嘛 yield return(null); } Progress = 1 / 2f; if (!wwwLoader.IsOk) { if (AssetBundlerLoaderErrorEvent != null) { AssetBundlerLoaderErrorEvent(this); } CDebug.LogError("[CAssetBundleLoader]Error Load AssetBundle: {0}", relativeUrl); OnFinish(null); wwwLoader.Release(); yield break; } else { // 提前结束 // 解密 // 释放WWW加载的字节。。释放该部分内存,因为AssetBundle已经自己有缓存了 var cloneBytes = (byte[])wwwLoader.Www.bytes.Clone(); wwwLoader.Release(); BundleParser = new CAssetBundleParser(RelativeResourceUrl, cloneBytes); while (!BundleParser.IsFinished) { if (IsReadyDisposed) // 中途释放 { OnFinish(null); yield break; } Progress = BundleParser.Progress / 2f + 1 / 2f; // 最多50%, 要算上WWWLoader的嘛 yield return(null); } Progress = 1f; var assetBundle = BundleParser.Bundle; if (assetBundle == null) { CDebug.LogError("WWW.assetBundle is NULL: {0}", FullUrl); } OnFinish(assetBundle); //Array.Clear(cloneBytes, 0, cloneBytes.Length); // 手工释放内存 } //GC.Collect(0);// 手工释放内存 }
IEnumerator LoadAssetBundle(string relativeUrl) { XLoadCache loadCache; if (!AssetBundlesCache.TryGetValue(RelativeResourceUrl, out loadCache)) { loadCache = new XLoadCache() { RelativeUrl = RelativeResourceUrl, Ab = null }; AssetBundlesCache[RelativeResourceUrl] = loadCache; CWWWLoader wwwLoader = new CWWWLoader(FullUrl); while (!wwwLoader.IsFinished) { yield return(null); } // 解密 CAssetBundleParser parser = new CAssetBundleParser(RelativeResourceUrl, wwwLoader.Www.bytes); while (!parser.IsFinished) { yield return(null); } if (parser.Bundle == null) { CBase.LogError("WWW.assetBundle is NULL: {0}", FullUrl); } loadCache.Ab = parser.Bundle; } else { if (loadCache.IsLoadedFinish) { yield return(null); // 确保每一次异步都延迟一帧 } while (!loadCache.IsLoadedFinish) { yield return(null); //Wait } } Bundle = loadCache.Ab; if (Callback != null) { Callback(FullUrl, Bundle, CallbackArgs); } }
IEnumerator StartDownload(string fullUrl) { float startTime = Time.time; if (UseCache && File.Exists(_SavePath)) { FinishedFlag = true; ErrorFlag = false; yield break; } WWWLoader = CWWWLoader.Load(fullUrl); while (!WWWLoader.IsFinished) { if (WWWLoader.Progress == 0 && Time.time - startTime > TIME_OUT_DEF) { CDebug.LogError("超時卻無下載 Timeout: {0}", fullUrl); break; } yield return(null); } if (WWWLoader.IsError || !WWWLoader.IsFinished) { CDebug.LogError("Download WWW Error: {0}", fullUrl); FinishedFlag = true; ErrorFlag = true; yield break; } string dir = Path.GetDirectoryName(_SavePath); if (!Directory.Exists(dir)) { Directory.CreateDirectory(dir); } System.IO.File.WriteAllBytes(_SavePath, WWWLoader.Www.bytes); FinishedFlag = true; // WWW没用了 WWWLoader.Release(); }
IEnumerator LoadAssetBundle(string relativeUrl) { XLoadCache loadCache; if (!AssetBundlesCache.TryGetValue(RelativeResourceUrl, out loadCache)) { loadCache = new XLoadCache() { RelativeUrl = RelativeResourceUrl, Ab = null }; AssetBundlesCache[RelativeResourceUrl] = loadCache; CWWWLoader wwwLoader = new CWWWLoader(FullUrl); while (!wwwLoader.IsFinished) yield return null; // 解密 CAssetBundleParser parser = new CAssetBundleParser(RelativeResourceUrl, wwwLoader.Www.bytes); while (!parser.IsFinished) { yield return null; } if (parser.Bundle == null) CBase.LogError("WWW.assetBundle is NULL: {0}", FullUrl); loadCache.Ab = parser.Bundle; } else { if (loadCache.IsLoadedFinish) { yield return null; // 确保每一次异步都延迟一帧 } while (!loadCache.IsLoadedFinish) { yield return null; //Wait } } Bundle = loadCache.Ab; if (Callback != null) Callback(FullUrl, Bundle, CallbackArgs); }
IEnumerator StartDownload(string fullUrl) { float startTime = Time.time; if (UseCache && File.Exists(_SavePath)) { var lastWriteTime = File.GetLastWriteTimeUtc(_SavePath); CDebug.Log("缓存文件: {0}, 最后修改时间: {1}", _SavePath, lastWriteTime); var deltaDays = CTool.GetDeltaDay(lastWriteTime); // 文件未过期 if (deltaDays < ExpireDays) { CDebug.Log("缓存文件未过期 {0}", _SavePath); FinishedFlag = true; ErrorFlag = false; yield break; } } WWWLoader = CWWWLoader.Load(fullUrl); while (!WWWLoader.IsFinished) { if (WWWLoader.Progress == 0 && Time.time - startTime > TIME_OUT_DEF) { CDebug.LogError("超時卻無下載 Timeout: {0}", fullUrl); break; } yield return null; } if (WWWLoader.IsError || !WWWLoader.IsFinished) { CDebug.LogError("Download WWW Error: {0}", fullUrl); FinishedFlag = true; ErrorFlag = true; yield break; } string dir = Path.GetDirectoryName(_SavePath); if (!Directory.Exists(dir)) Directory.CreateDirectory(dir); System.IO.File.WriteAllBytes(_SavePath, WWWLoader.Www.bytes); FinishedFlag = true; // WWW没用了 WWWLoader.Release(); }
IEnumerator StartDownload(string fullUrl) { float startTime = Time.time; if (UseCache && File.Exists(_SavePath)) { FinishedFlag = true; ErrorFlag = false; yield break; } WWWLoader = CWWWLoader.Load(fullUrl); while (!WWWLoader.IsFinished) { if (WWWLoader.Progress == 0 && Time.time - startTime > TIME_OUT_DEF) { CDebug.LogError("超時卻無下載 Timeout: {0}", fullUrl); break; } yield return null; } if (WWWLoader.IsError || !WWWLoader.IsFinished) { CDebug.LogError("Download WWW Error: {0}", fullUrl); FinishedFlag = true; ErrorFlag = true; yield break; } string dir = Path.GetDirectoryName(_SavePath); if (!Directory.Exists(dir)) Directory.CreateDirectory(dir); System.IO.File.WriteAllBytes(_SavePath, WWWLoader.Www.bytes); FinishedFlag = true; // WWW没用了 WWWLoader.Release(); }