/// <summary> /// 监视器协程 /// 超过最大WWWLoader时,挂起~ /// 后来的新loader会被优先加载 /// </summary> /// <returns></returns> protected static IEnumerator WWWLoaderMonitorCoroutine() { //yield return new WaitForEndOfFrame(); // 第一次等待本帧结束 yield return(null); while (WWWLoadersStack.Count > 0) { if (KResourceModule.LoadByQueue) { while (ABManager.GetCount <KWWWLoader>() != 0) { yield return(null); } } while (WWWLoadingCount >= MAX_WWW_COUNT) { yield return(null); } var wwwLoader = WWWLoadersStack.Pop(); wwwLoader.StartLoad(); } KResourceModule.Instance.StopCoroutine(CachedWWWLoaderMonitorCoroutine); CachedWWWLoaderMonitorCoroutine = null; }
/// <summary> /// 协程加载Assetbundle,加载完后执行callback /// </summary> /// <param name="url">资源的url</param> /// <param name="callback"></param> /// <param name="callbackArgs"></param> /// <returns></returns> private IEnumerator CoLoad(string url) { if (AppConfig.IsLogAbInfo) { Log.Info("[Request] WWW, {1}", url); } // 潜规则:不用LoadFromCache~它只能用在.assetBundle #pragma warning disable 0618 Www = new WWW(url); #pragma warning restore 0618 BeginLoadTime = Time.time; WWWLoadingCount++; //设置AssetBundle解压缩线程的优先级 Www.threadPriority = Application.backgroundLoadingPriority; // 取用全局的加载优先速度 while (!Www.isDone) { Progress = Www.progress; yield return(null); } yield return(Www); WWWLoadingCount--; Progress = 1; if (IsReadyDisposed) { Log.Error("[KWWWLoader]Too early release: {0}", url); OnFinish(null); yield break; } if (!string.IsNullOrEmpty(Www.error)) { if (Application.platform == RuntimePlatform.Android) { // TODO: Android下的错误可能是因为文件不存在! } if (url.StartsWith(KResourceModule.GetFileProtocol)) { string fileRealPath = url.Replace(KResourceModule.GetFileProtocol, ""); Log.Error("File {0} Exist State: {1}", fileRealPath, System.IO.File.Exists(fileRealPath)); } Log.Error("[KWWWLoader:Error]{0} {1}", Www.error, url); OnFinish(null); yield break; } else { if (WWWFinishCallback != null) { WWWFinishCallback(url); } Desc = string.Format("{0}K", Www.bytes.Length / 1024f); OnFinish(Www); } // 预防WWW加载器永不反初始化, 造成内存泄露~ if (Application.isEditor) { while (ABManager.GetCount <KWWWLoader>() > 0) { yield return(null); } yield return(new WaitForSeconds(5f)); while (!IsReadyDisposed) { Log.Error("[KWWWLoader]Not Disposed Yet! : {0}", this.Url); yield return(null); } } }