/// <summary> /// 得到清单文件 /// </summary> /// <returns></returns> private static ManifestConfig GetManifest(string output) { output += "/" + output.Split('/')[output.Split('/').Length - 1]; ManifestConfig manifestConfig = null; if (File.Exists(output)) { manifestConfig = new ManifestConfig(); var bundle = AssetBundle.LoadFromFile(output); AssetBundleManifest abManifest = bundle.LoadAsset("assetbundlemanifest") as AssetBundleManifest; string[] bundleNames = abManifest.GetAllAssetBundles(); for (int i = 0; i < bundleNames.Length; ++i) { if (bundleNames[i].EndsWith("updatefile.json") || bundleNames[i].EndsWith("manifestfile.json")) { continue; } Manifest manifest = new Manifest(); manifest.name = bundleNames[i]; ABFI ab = GetABFI(outputPath + "/" + bundleNames[i]); manifest.MD5 = ab.md5; manifest.size = ab.size; foreach (var dependenciesName in abManifest.GetDirectDependencies(bundleNames[i])) { manifest.dependencies.Add(dependenciesName); } manifestConfig.Add(manifest); } bundle.Unload(true); } return(manifestConfig); }
/// <summary> /// 拷贝更新资源包 /// </summary> public static void CopyUpdateAssetBundles(string output, string dest, string version, string cdn = null) { ManifestConfig remote = new ManifestConfig(); if (!string.IsNullOrEmpty(cdn)) { string url = cdn + "/data/conf/updatefile.json"; WWW www = new WWW(url); while (!www.isDone) { ; } if (string.IsNullOrEmpty(www.error) && www.progress == 1f) { TextAsset text = www.assetBundle.LoadAsset(Path.GetFileNameWithoutExtension(url)) as TextAsset; remote = JsonReader.Deserialize <ManifestConfig>(text.text); www.assetBundle.Unload(true); } www.Dispose(); } ManifestConfig local = JsonReader.Deserialize <ManifestConfig>(File.ReadAllText(assetPath + "/data/conf/updatefile.json")); if (local != null) { ManifestConfig manifestConfig = new ManifestConfig(); foreach (var data in local.data.Values) { if (remote.Contains(data.name) && remote.Get(data.name).MD5 == data.MD5) { continue; } manifestConfig.Add(data); } if (!Directory.Exists(dest)) { Directory.CreateDirectory(dest); } string updateFilePath = dest + "/updatefile.json"; string updateFileValue = JsonWriter.Serialize(manifestConfig); File.WriteAllText(updateFilePath, updateFileValue); AssetDatabase.Refresh(); manifestConfig.Add(new Manifest() { name = "data/conf/manifestfile.json" }); manifestConfig.Add(new Manifest() { name = "data/conf/updatefile.json" }); using (MemoryStream stream = new MemoryStream()) { using (ZipOutputStream zip = new ZipOutputStream(stream)) { zip.SetComment(version); foreach (var data in manifestConfig.data.Values) { ZipEntry entry = new ZipEntry(data.name); entry.DateTime = new DateTime(); entry.DosTime = 0; zip.PutNextEntry(entry); string filepPth = output + "/" + data.name; var bytes = File.ReadAllBytes(filepPth); zip.Write(bytes, 0, bytes.Length); } zip.Finish(); zip.Flush(); var fileBytes = new byte[stream.Length]; Array.Copy(stream.GetBuffer(), fileBytes, fileBytes.Length); string platform = "PC"; #if UNITY_ANDROID platform = "Android"; #elif UNITY_IOS platform = "iOS"; #endif DateTime dt = DateTime.Now; string date = string.Format("{0}.{1}.{2}_{3}.{4}.{5}", dt.Year, dt.Month, dt.Day, dt.Hour, dt.Minute, dt.Second); string md5 = Util.GetMD5(fileBytes); File.WriteAllBytes(string.Format("{0}/{1}_{2}_{3}_{4}.zip", dest, platform, version, date, md5), fileBytes); } } File.Delete(updateFilePath); AssetDatabase.Refresh(); } }
/// <summary> /// 更新文件 /// </summary> /// <param name="output"></param> public static void BuildUpdateFile(string output, string cdn = null) { ManifestConfig newManifestConfig = GetManifest(output); ManifestConfig oldManifestConfig = newManifestConfig; if (!string.IsNullOrEmpty(cdn)) { string url = cdn + "/data/conf/manifestfile.json"; WWW www = new WWW(url); while (!www.isDone) { ; } if (string.IsNullOrEmpty(www.error) && www.progress == 1f) { TextAsset text = www.assetBundle.LoadAsset(Path.GetFileNameWithoutExtension(url)) as TextAsset; oldManifestConfig = JsonReader.Deserialize <ManifestConfig>(text.text); www.assetBundle.Unload(false); } www.Dispose(); } ManifestConfig manifestConfig = new ManifestConfig(); if (!string.IsNullOrEmpty(cdn)) { string url = cdn + "/data/conf/updatefile.json"; WWW www = new WWW(url); while (!www.isDone) { ; } if (string.IsNullOrEmpty(www.error) && www.progress == 1f) { TextAsset text = www.assetBundle.LoadAsset(Path.GetFileNameWithoutExtension(url)) as TextAsset; manifestConfig = JsonReader.Deserialize <ManifestConfig>(text.text); www.assetBundle.Unload(false); } www.Dispose(); } // 写入Manifest if (newManifestConfig != null && oldManifestConfig != null) { foreach (var data in newManifestConfig.data.Values) { if (oldManifestConfig.Contains(data.name) && oldManifestConfig.Get(data.name).MD5 == data.MD5) { continue; } manifestConfig.Add(data); } // 写入到文件 File.WriteAllText(assetPath + "/data/conf/updatefile.json", JsonWriter.Serialize(manifestConfig)); // 刷新 AssetDatabase.Refresh(); // Build清单文件 AssetBundleBuild[] builds = new AssetBundleBuild[1]; builds[0].assetBundleName = "data/conf/updatefile"; builds[0].assetBundleVariant = null; builds[0].assetNames = new string[1] { assetPath + "/data/conf/updatefile.json" }; BuildPipeline.BuildAssetBundles(output, builds, BuildAssetBundleOptions.ChunkBasedCompression, buildTarget); } }