public CAssetBundleLoader(string url, Action <string, AssetBundle, object[]> callback = null, params object[] callbackArgs) { Callback = callback; CallbackArgs = callbackArgs; RelativeResourceUrl = url; FullUrl = CResourceManager.GetResourcesPath(url); CResourceManager.LogRequest("AssetBundle", FullUrl); CResourceManager.Instance.StartCoroutine(LoadAssetBundle(RelativeResourceUrl)); }
/// <summary> /// 协和加载Assetbundle,加载完后执行callback /// </summary> /// <param name="url">资源的url</param> /// <param name="callback"></param> /// <param name="callbackArgs"></param> /// <returns></returns> IEnumerator CoLoad(string url, Action <WWW, object[]> callback = null, params object[] callbackArgs) { if (CResourceManager.LoadByQueue) { while (Loaded.Count != 0) { yield return(null); } } CResourceManager.LogRequest("WWW", url); CLoadingCache cache = null; if (!Loaded.TryGetValue(url, out cache)) { cache = new CLoadingCache(url); Loaded.Add(url, cache); System.DateTime beginTime = System.DateTime.Now; WWW www = new WWW(url); //设置AssetBundle解压缩线程的优先级 www.threadPriority = Application.backgroundLoadingPriority; // 取用全局的加载优先速度 while (!www.isDone) { Progress = www.progress; yield return(null); } yield return(www); if (!string.IsNullOrEmpty(www.error)) { IsError = true; string fileProtocol = CResourceManager.GetFileProtocol(); if (url.StartsWith(fileProtocol)) { string fileRealPath = url.Replace(fileProtocol, ""); CBase.LogError("File {0} Exist State: {1}", fileRealPath, System.IO.File.Exists(fileRealPath)); } CBase.LogError(www.error + " " + url); } CResourceManager.LogLoadTime("WWW", url, beginTime); cache.Www = www; if (WWWFinishCallback != null) { WWWFinishCallback(url); } } else { if (cache.Www != null) { yield return(null); // 确保每一次异步读取资源都延迟一帧 } while (cache.Www == null) // 未加载完 { yield return(null); } } Progress = cache.Www.progress; WwwCache = cache; if (callback != null) { callback(WwwCache.Www, callbackArgs); } }