private IEnumerator LoadManifest_Async(string rManifrestUrl) { isManifestLoadCompleted = false; WWW www = WWWAssist.Load(rManifrestUrl); yield return(www); if (!string.IsNullOrEmpty(www.error)) { isManifestLoadCompleted = true; yield break; } this.manifest = www.assetBundle.LoadAsset("AssetBundleManifest") as AssetBundleManifest; WWWAssist.Destroy(ref www); string[] rALLAB = this.manifest.GetAllAssetBundles(); assetLoadInfos = new Dict <string, AssetLoadInfo>(); for (int i = 0; i < rALLAB.Length; i++) { // 开始构建AssetLoadInfo AssetLoadInfo rAssetLoadInfo = new AssetLoadInfo(); rAssetLoadInfo.abPath = rALLAB[i]; rAssetLoadInfo.abName = rALLAB[i]; string[] rDependABs = this.manifest.GetDirectDependencies(rALLAB[i]); rAssetLoadInfo.abDependNames = rDependABs; rAssetLoadInfo.isLoading = false; rAssetLoadInfo.isLoadCompleted = false; rAssetLoadInfo.cacheAsset = null; rAssetLoadInfo.refCount = 0; assetLoadInfos.Add(rAssetLoadInfo.abName, rAssetLoadInfo); } isManifestLoadCompleted = true; }
private IEnumerator LoadAsset_Async(LoaderRequest rRequest) { // 确认Manifest已经加载完成 while (!isManifestLoadCompleted) { yield return(0); } AssetLoadInfo rAssetLoadInfo = null; if (!assetLoadInfos.TryGetValue(rRequest.path, out rAssetLoadInfo)) { Debug.LogErrorFormat("找不到该资源 -- {0}", rRequest.path); rRequest.asset = null; yield break; } // 确认该资源是否已经加载完成或者正在被加载 while (rAssetLoadInfo.isLoading && !rAssetLoadInfo.isLoadCompleted) { yield return(0); } //引用计数加1 rAssetLoadInfo.refCount++; // 如果该资源加载完成了 if (!rAssetLoadInfo.isLoading && rAssetLoadInfo.isLoadCompleted) { if (!string.IsNullOrEmpty(rRequest.assetName)) { AssetBundleRequest rABRequest = rAssetLoadInfo.cacheAsset.LoadAssetAsync(rRequest.assetName); yield return(rABRequest); rRequest.asset = rABRequest.asset; } yield break; } // 开始加载资源依赖项 if (rAssetLoadInfo.abDependNames != null) { for (int i = rAssetLoadInfo.abDependNames.Length - 1; i >= 0; i--) { string rDependABPath = rAssetLoadInfo.abDependNames[i]; string rDependABName = rDependABPath; LoaderRequest rDependAssetRequest = new LoaderRequest(rDependABName, "", false); rDependAssetRequest.Start(LoadAsset_Async(rDependAssetRequest)); yield return(rDependAssetRequest.Coroutine); } } //开始加载当前的资源包 rAssetLoadInfo.isLoading = true; rAssetLoadInfo.isLoadCompleted = false; string rAssetLoadUrl = AssetPlatformManager.Instance.GetStreamingUrl_CurPlatform(rAssetLoadInfo.abPath); WWW www = new WWW(rAssetLoadUrl); yield return(www); // 如果是一个直接的资源,将资源的对象取出来 rAssetLoadInfo.cacheAsset = www.assetBundle; WWWAssist.Destroy(ref www); // 加载Object if (!string.IsNullOrEmpty(rRequest.assetName)) { if (!rRequest.isScene) { AssetBundleRequest rABRequest = rAssetLoadInfo.cacheAsset.LoadAssetAsync(rRequest.assetName); yield return(rABRequest); rRequest.asset = rABRequest.asset; } else { rAssetLoadInfo.cacheAsset.GetAllScenePaths(); } } rAssetLoadInfo.isLoading = false; rAssetLoadInfo.isLoadCompleted = true; }