public async Task StartAsync() { using (UnityWebRequestAsync request = ComponentFactory.Create <UnityWebRequestAsync>()) { string versionUrl = GlobalConfigComponent.Instance.GlobalProto.GetUrl() + "StreamingAssets/" + "Version.txt"; Log.Debug(versionUrl); await request.DownloadAsync(versionUrl); this.VersionConfig = JsonHelper.FromJson <VersionConfig>(request.Request.downloadHandler.text); } Log.Debug("WebVersion:\n" + JsonHelper.ToJson(this.VersionConfig)); // 对比本地的Version.txt string versionPath = Path.Combine(PathHelper.AppHotfixResPath, "Version.txt"); if (!File.Exists(versionPath)) { foreach (FileVersionInfo versionInfo in this.VersionConfig.FileInfoDict.Values) { if (versionInfo.File == "Version.txt") { continue; } this.bundles.Enqueue(versionInfo.File); this.TotalSize += versionInfo.Size; } } else { VersionConfig localVersionConfig = JsonHelper.FromJson <VersionConfig>(File.ReadAllText(versionPath)); Log.Debug("LocalVersion:\n" + JsonHelper.ToJson(localVersionConfig)); // 先删除服务器端没有的ab foreach (FileVersionInfo fileVersionInfo in localVersionConfig.FileInfoDict.Values) { if (this.VersionConfig.FileInfoDict.ContainsKey(fileVersionInfo.File)) { continue; } string abPath = Path.Combine(PathHelper.AppHotfixResPath, fileVersionInfo.File); File.Delete(abPath); } // 再下载 foreach (FileVersionInfo fileVersionInfo in this.VersionConfig.FileInfoDict.Values) { FileVersionInfo localVersionInfo; if (localVersionConfig.FileInfoDict.TryGetValue(fileVersionInfo.File, out localVersionInfo)) { if (fileVersionInfo.MD5 == localVersionInfo.MD5) { continue; } } if (fileVersionInfo.File == "Version.txt") { continue; } this.bundles.Enqueue(fileVersionInfo.File); this.TotalSize += fileVersionInfo.Size; } } if (this.bundles.Count == 0) { return; } Log.Debug($"need download bundles: {this.bundles.ToList().ListToString()}"); await this.WaitAsync(); }
public async Task LoadOneBundleAsync(string assetBundleName) { ABInfo abInfo; if (this.bundles.TryGetValue(assetBundleName, out abInfo)) { ++abInfo.RefCount; return; } if (this.cacheDictionary.ContainsKey(assetBundleName)) { abInfo = this.cacheDictionary[assetBundleName]; ++abInfo.RefCount; this.bundles[assetBundleName] = abInfo; this.cacheDictionary.Remove(assetBundleName); return; } if (!Define.IsAsync) { #if UNITY_EDITOR string[] realPath = AssetDatabase.GetAssetPathsFromAssetBundle(assetBundleName); foreach (string s in realPath) { string assetName = Path.GetFileNameWithoutExtension(s); string path = $"{assetBundleName}/{assetName}".ToLower(); UnityEngine.Object resource = AssetDatabase.LoadAssetAtPath <UnityEngine.Object>(s); this.resourceCache[path] = resource; } this.bundles[assetBundleName] = new ABInfo(assetBundleName, null); return; #endif } AssetBundle assetBundle; using (AssetsBundleLoaderAsync assetsBundleLoaderAsync = ComponentFactory.Create <AssetsBundleLoaderAsync>()) { assetBundle = await assetsBundleLoaderAsync.LoadAsync(assetBundleName); } if (!assetBundle.isStreamedSceneAssetBundle) { // 异步load资源到内存cache住 UnityEngine.Object[] assets; using (AssetsLoaderAsync assetsLoaderAsync = ComponentFactory.Create <AssetsLoaderAsync, AssetBundle>(assetBundle)) { assets = await assetsLoaderAsync.LoadAllAssetsAsync(); } foreach (UnityEngine.Object asset in assets) { string path = $"{assetBundleName}/{asset.name}".ToLower(); this.resourceCache[path] = asset; } } this.bundles[assetBundleName] = new ABInfo(assetBundleName, assetBundle); }