protected virtual IEnumerator LoadFromCachedFile() { if (state != LoadState.State_Error) { string bundleFilePath = FilePathTools.GetBundleLoadPath(assetBundleInfo.AssetBundleName); AssetBundleCreateRequest req = AssetBundle.LoadFromFileAsync(bundleFilePath); yield return(req); _bundle = req.assetBundle; this.Complete(); } }
//path 一定要是bundle的path private AssetBundle LoadBundle(string path, ref List <string> bundles, int stackSizeForCheck = 0) { if (stackSizeForCheck > MAX_DEPENDENCY_DEEP) { DebugUtil.LogError($"{GetType()}.LoadBundle, path = {path}, dependency stack reach max count({MAX_DEPENDENCY_DEEP}, check if its a bad bundle strategy or a dead code cycle!)"); return(null); } string abKey = path; AssetBundle targetAb; targetAb = AssetBundleCache.GetAssetBundle(abKey); AssetBundleInfo abInfo = VersionManager.Instance.GetAssetBundleInfo(abKey); string[] dependencies = abInfo.DependenciesBundleNames; // 加载自己 if (targetAb == null) { string bundleFilePath = FilePathTools.GetBundleLoadPath(abKey); if (File.Exists(bundleFilePath)) { targetAb = AssetBundle.LoadFromFile(bundleFilePath); } if (targetAb != null) { bundles.Add(abKey); // if (HomeRoomConfigController.Instance != null) // { // var info = HomeRoomConfigController.Instance.GetRoomResInfoByABPath(abKey); // if (info != null) // { // //homeroom 公共库的ab包名字是带versioncode的,需要去掉 // targetAb.name = abKey; // } // } // if (CookingGameConfigController.Instance != null) // { // var info = CookingGameConfigController.Instance.GetRoomResInfoByABPath(abKey); // if (info != null) // { // //cookinggame 公共库的ab包名字是带versioncode的,需要去掉 // targetAb.name = abKey; // } // } // if (ColorResConfigController.Instance != null) // { // var info = ColorResConfigController.Instance.GetRoomResInfoByABPath(abKey); // if (info != null) // { // //colorRes公共库的ab包名字是带versioncode的,需要去掉 // targetAb.name = abKey; // } // } AssetBundleCache.AddAssetBundle(targetAb, abInfo); } else { return(null); } } // 加载依赖包 if (dependencies != null && dependencies.Length > 0) { ++stackSizeForCheck; foreach (string fileName in dependencies) { if (!bundles.Contains(fileName)) { LoadBundle(fileName, ref bundles, stackSizeForCheck); } } } return(targetAb); }
/// <summary> /// 通过组名,ab包名,获得需要下载的ab包 /// </summary> /// <returns>The update files dict.[name,md5]</returns> /// <param name="groupName">Group name.</param> /// <param name="assetbundleName">Assetbundle name.</param> public Dictionary <string, string> GetUpdateFilesDict(string groupName, string assetbundleName = null) { Dictionary <string, string> updateFileNames = new Dictionary <string, string>(); if (localVersionFile == null || remoteVersionFile == null) { DebugUtil.LogError("Version文件出错"); return(updateFileNames); } VersionItemInfo item = null; if (localVersionFile.ResGroups.ContainsKey(groupName)) { item = localVersionFile.ResGroups[groupName]; if (item.UpdateWholeGroup)//强制更新整个组里的ab包 { List <string> remoteAB = remoteVersionFile.GetAssetBundlesByKey(groupName); List <string> localAB = localVersionFile.GetAssetBundlesByKey(groupName); Dictionary <string, string> localFilesHash = new Dictionary <string, string>(); foreach (string assetname in localAB) { localFilesHash.Add(assetname, localVersionFile.GetAssetBundleHash(groupName, assetname)); } foreach (string assetname in remoteAB) { string remoteHash = remoteVersionFile.GetAssetBundleHash(groupName, assetname); string remoteMd5 = remoteVersionFile.GetAssetBundleMd5(groupName, assetname); string localPath = FilePathTools.GetBundleLoadPath(assetname); if (localFilesHash.ContainsKey(assetname)) { //添加“remoteState == AssetState.ExistInDownLoad”在于兼容ProtectMode if (!File.Exists(localPath) || remoteHash.Trim() != localFilesHash[assetname].Trim()) { updateFileNames.Add(assetname, remoteMd5); } } else { updateFileNames.Add(assetname, remoteMd5); } } } else { if (!string.IsNullOrEmpty(assetbundleName)) { string remoteName = remoteVersionFile.GetAssetBundleByKeyAndName(groupName, assetbundleName); string localName = localVersionFile.GetAssetBundleByKeyAndName(groupName, assetbundleName); string remoateHash = remoteVersionFile.GetAssetBundleHash(groupName, remoteName); string remoateMd5 = remoteVersionFile.GetAssetBundleMd5(groupName, remoteName); if (string.IsNullOrEmpty(localName)) //本地没有 { if (!string.IsNullOrEmpty(remoteName)) //远端有 { updateFileNames.Add(remoteName, remoateMd5); //远端有,本地没有 } } else { if (!string.IsNullOrEmpty(remoteName))//本地有,远端有 { // 比较两者的hash string localHash = localVersionFile.GetAssetBundleHash(groupName, localName); string localPath = FilePathTools.GetBundleLoadPath(localName); if (!File.Exists(localPath) || localHash.Trim() != remoateHash.Trim())//hash不一样,要更新 { updateFileNames.Add(remoteName, remoateMd5); } } } } } } else {// 本地version文件不包含这个组,目前只发生在editor模式下,修复下。将来不一定。 if (remoteVersionFile.ResGroups.ContainsKey(groupName)) { item = remoteVersionFile.ResGroups[groupName]; if (item.UpdateWholeGroup) { List <string> remoteAB = remoteVersionFile.GetAssetBundlesByKey(groupName); foreach (string assetname in remoteAB) { string remoteMd5 = remoteVersionFile.GetAssetBundleMd5(groupName, assetname); updateFileNames.Add(assetname, remoteMd5); } } else { string remoteName = remoteVersionFile.GetAssetBundleByKeyAndName(groupName, assetbundleName); string remoteMd5 = remoteVersionFile.GetAssetBundleMd5(groupName, remoteName); if (!string.IsNullOrEmpty(remoteName))//远端有 { updateFileNames.Add(remoteName, remoteMd5); } } } else { DebugUtil.LogError("远端和本地的version文件都不包含:" + groupName); } } return(updateFileNames); }
IEnumerator CheckAsset(List <string> bundleNames) { Log(string.Format("load bundle progress:{00:F1}%", 0.0f), false); List <AssetBundle> allBundles = new List <AssetBundle>(); for (int m = 0; m < bundleNames.Count; ++m) { AssetBundle bundle = AssetBundle.LoadFromFile(FilePathTools.GetBundleLoadPath(bundleNames[m])); if (null == bundle) { Fail(string.Format("can not load bundle file : {0}", bundleNames[m])); } allBundles.Add(bundle); Log(string.Format("load bundle progress:{00:F1}%", (m + 1) / (bundleNames.Count * 1.0f) * 100.0f), true); yield return(null); } Log(string.Format("load bundle progress:{00:F1}%", 100.0f), true); Log(string.Format("Object progress:{00:F1}% ", 0.0f), false); for (int m = 0; m < allBundles.Count; ++m) { UnityEngine.Object[] objs = allBundles[m].LoadAllAssets(); foreach (var obj in objs) { if (obj == null) { Fail(string.Format("null in bundle load : {0}", bundleNames[m])); } if (obj.GetType() == typeof(GameObject)) { GameObject prefab = obj as GameObject; if (null == prefab) { Fail(string.Format("{0} convert Prefab fail in bundle :{1}", obj.name, bundleNames[m])); } Component[] comps = prefab.GetComponentsInChildren <Component>(true); foreach (var com in comps) { if (null == com) { Fail(string.Format("{0} prefab exist null component in bundle :{1}", obj.name, bundleNames[m])); } } GameObject instance = Instantiate(prefab); if (null == instance) { Fail(string.Format("{0} instance is null inbundle :{1}", obj.name, bundleNames[m])); } comps = instance.GetComponentsInChildren <Component>(true); foreach (var com in comps) { if (null == com) { Fail(string.Format("{0} instance exist null component in bundle :{1}", obj.name, bundleNames[m])); } } Destroy(instance); } } Log(string.Format("Object progress:{00:F1}% ", (m + 1) / (allBundles.Count * 1.0f) * 100.0f), true); yield return(null); } Log(string.Format("Object progress:{00:F1}% ", 100.0f), true); Log(string.Format("unload bundle progress:{00:F1}%", 0.0f), false); for (int m = 0; m < allBundles.Count; ++m) { allBundles[m].Unload(false); Log(string.Format("unload bundle progress:{00:F1}%", (m + 1) / (allBundles.Count * 1.0f) * 100.0f), true); yield return(null); } Log(string.Format("unload bundle progress:{00:F1}%", 100.0f), true); yield return(null); }