public static void BuildAssetInfo(BuildTarget target) { string streamPath = SysDefine.GetStreamingAssetsTargetPathByPlatform(BuildTargetToPlatform(target)); string path = SysDefine.GetAssetsTargetPathByPlatform(BuildTargetToPlatform(target)); if (Directory.Exists(streamPath)) { Directory.Delete(streamPath, true); } Directory.CreateDirectory(streamPath); AllBundleInfo AllInfos = new AllBundleInfo(); string[] files = Directory.GetFiles(path); foreach (var file in files) { //if (Path.GetFileName(file)!= "AssetBundles.manifest" && Path.GetExtension(file) == ".manifest") continue; var info = GetInfo(file); AllInfos.Infos.Add(info.Name, info); File.Copy(file, Path.Combine(streamPath, info.Name + "@" + info.MD5)); } string json = JsonConvert.SerializeObject(AllInfos, Formatting.Indented); if (File.Exists(streamPath + "/" + AllBundleInfo.Name)) { File.Delete(streamPath + "/" + AllBundleInfo.Name); } File.WriteAllText(streamPath + "/" + AllBundleInfo.Name, json); }
private SingleBundleInfo GetMainManifestBundleInfo(AllBundleInfo allBundleInfo) { foreach (var bundleInfo in allBundleInfo.BundleInfoList) { if (CheckIsMainManifest(bundleInfo.Value)) { return(bundleInfo.Value); } } return(null); }
/// <summary> /// 清除除了allBundleInfo中其他版本号的资源 /// </summary> /// <param name="allBundleInfo"></param> private void ClearOtherCachedVersions(AllBundleInfo allBundleInfo) { foreach (var singleBundleInfo in allBundleInfo.BundleInfoList) { bool success = Caching.ClearOtherCachedVersions(singleBundleInfo.Value.bundleName, Hash128.Parse(singleBundleInfo.Value.bundleHash128)); string bundleName = singleBundleInfo.Value.bundleName; Hash128 hash128 = Hash128.Parse(singleBundleInfo.Value.bundleHash128); Debug.Log(string.Format("缓存bundleName:{0},清理版本号hash128:{1}之外的资源,success:{2}", bundleName, hash128, success)); } }
public bool LoadInfo(string path, out AllBundleInfo allBundleInfo) { try { string json = File.ReadAllText(path); allBundleInfo = JsonConvert.DeserializeObject <AllBundleInfo>(json); return(true); } catch { allBundleInfo = null; return(false); } }
static void SetBundleVersionInfo() { AllBundleInfo allBundleInfo = new AllBundleInfo(); allBundleInfo.BundleInfoList = new List <SingleBundleInfo>(); foreach (var item in Bundle_Name) { allBundleInfo.BundleInfoList.Add(ComputeHashAndCRC(item)); } allBundleInfo.BundleInfoList.Insert(0, ComputeManifestHashAndCRC(GetBundleDirectory() + "/" + GetBundleDirectory().Split('/')[GetBundleDirectory().Split('/').Length - 1])); string json = JsonUtil.Instance.ObjectToJson <AllBundleInfo>(allBundleInfo); //序列化为json WriteManifestJsonConfig(json); //写入配置文件 }
/// <summary> /// 生成 ABConfig.json 文件。通过streamingAssetsPath拼接主manifest名字获取到主manifest文件,从而得到所有assetbundle信息。 /// </summary> /// <param name="mainManifestName"></param> private static void GenerateABConfig(string mainManifestName) { if (string.IsNullOrEmpty(mainManifestName)) { Debug.LogError("mainManifestName 不能为空值"); } AllBundleInfo allBundleInfo = new AllBundleInfo(); //添加主manifest的AssetBundle信息 allBundleInfo.BundleInfoList.Add(mainManifestName, ComputeManifestHashAndCRC(GetAssetBundleDirectory() + "/" + mainManifestName)); //加载主manifest的AssetBundle AssetBundle assetBundle = AssetBundle.LoadFromFile(GetAssetBundleDirectory() + "/" + mainManifestName); if (!assetBundle) { Debug.LogError(string.Format("生成AssetBundle版本文件失败,主Manifest文件未正常加载!path:{0}", GetAssetBundleDirectory() + "/" + mainManifestName)); return; } AssetBundleManifest manifest = assetBundle.LoadAsset <AssetBundleManifest>("AssetBundleManifest"); //必须要卸载AssetBundle,否则编辑器未重启的情况下LoadFromFile就会出错,因为不允许重复加载AssetBundle! assetBundle.Unload(false); //获取打包的所以AssetBundle名字 var bundleNames = manifest.GetAllAssetBundles(); if (bundleNames.Length <= 0) { Debug.LogError("生成AssetBundle版本文件失败,没有AssetBundle文件,先打包AssetBundle"); return; } foreach (var bundleName in bundleNames) { //AssetImporter.GetAtPath 的参数必须是是以“Assets/”开头的相对路径,包括后缀名。 var bundleFile = bundleName; //变体需要截断后面的变体后缀,只传文件名字 if (bundleFile.Contains(".")) { bundleFile = bundleFile.Split('.')[0]; } string assetName = bundleName.Substring(bundleName.LastIndexOf('/') + 1); Debug.Log("assetName = " + assetName); //获取到AssetBundle相对路径下的完全路径 //如果是以文件夹为单位打包的AssetBundle,fullPath 即为 "Assets/" + bundleName string fullPath; if (AssetDatabase.IsValidFolder("Assets/" + bundleName)) { fullPath = "Assets/" + bundleName; } //如果是以单个文件为单位打包的AssetBundle,fullPath 即为 "Assets/" + bundleName + 后缀名 else { string[] fullPaths = AssetDatabase.GetAssetPathsFromAssetBundleAndAssetName(bundleName, assetName); fullPath = fullPaths.Length > 0 ? fullPaths[0] : bundleFile; } AssetImporter asset = AssetImporter.GetAtPath(fullPath); if (asset != null) { allBundleInfo.BundleInfoList.Add(bundleName, ComputeHashAndCRC(asset)); } else { Debug.LogError(string.Format("生成AssetBundle版本文件错误,没有文件:{0}", "Assets/" + bundleName)); } } string json = JsonConvert.SerializeObject(allBundleInfo, Formatting.Indented); WriteManifestJsonConfig(json); }