public static FileListManifest BuildFileLists(PackageBuildInfo buildInfo, BundleBuilderData.BundleInfo[] builds) { var build = new FileListManifest(); foreach (var bundle in builds) { var entry = new FileListManifestEntry() { name = bundle.name, }; build.fileLists.Add(entry); var filename = Path.Combine(buildInfo.packagePath, bundle.name); var manifest = new UnityFS.FileListManifest(); foreach (var bundleSplit in bundle.splits) { foreach (var bundleSlice in bundleSplit.slices) { if (bundleSlice.IsBuild(buildInfo.buildPlatform)) { var assetCount = bundleSlice.GetAssetCount(); for (var assetIndex = 0; assetIndex < assetCount; assetIndex++) { var assetGuid = bundleSlice.GetAssetGuid(assetIndex); var assetPath = AssetDatabase.GUIDToAssetPath(assetGuid); var fileEntry = GenFileEntry(assetPath, assetPath); manifest.files.Add(fileEntry); if (CopyRawFile(buildInfo.packagePath, assetPath)) { var fileListManifestFileInfo = new FileListManifestFileInfo() { assetPath = assetPath, streamingAssets = bundleSlice.streamingAssets }; build.fileEntrys.Add(fileListManifestFileInfo); } // FileUtil.CopyFileOrDirectory(assetPath, outPath); // Debug.LogFormat("gen {0} from {1}", outFilePath, assetPath); } } } } var jsonString = JsonUtility.ToJson(manifest); File.WriteAllText(filename, jsonString); } return(build); }
private static void Cleanup(PackageBuildInfo buildInfo, AssetBundleManifest assetBundleManifest, ZipArchiveManifest zipArchiveManifest, FileListManifest fileListManifest, RawFileManifest rawFileManifest, EmbeddedManifest embeddedManifest) { foreach (var dir in Directory.GetDirectories(buildInfo.packagePath)) { CleanupRecursively(dir, "Assets", fileListManifest, false); } foreach (var file in Directory.GetFiles(buildInfo.packagePath)) { var match = false; var fi = new FileInfo(file); var filename = fi.Name; if (builtinFiles.Contains(filename)) { match = true; } if (!match && buildInfo.filelist.Contains(filename)) { match = true; } if (!match && filename == Manifest.EmbeddedManifestFileName && embeddedManifest.bundles.Count > 0) { match = true; } if (!match) { Debug.LogWarning("delete unused file: " + filename); try { fi.Delete(); } catch (Exception exception) { Debug.LogError(exception); } } } }
// 生成打包 private static void _BuildPackages(PackageBuildInfo packageBuildInfo) { Debug.Log($"building bundles..."); Scan(packageBuildInfo.data); var assetBundleBuilds = GenerateAssetBundleBuilds(packageBuildInfo); var zipArchiveBuilds = GenerateZipArchiveBuilds(packageBuildInfo); var fileListBuilds = GenerateFileListBuilds(packageBuildInfo); var rawFileBuilds = GenerateRawFileBuilds(packageBuildInfo); AssetBundleManifest assetBundleManifest = null; ZipArchiveManifest zipArchiveManifest = null; FileListManifest fileListManifest = null; RawFileManifest rawFileManifest = null; if (assetBundleBuilds.Length != 0) { assetBundleManifest = BuildAssetBundles(packageBuildInfo, assetBundleBuilds); } if (zipArchiveBuilds.Count != 0) { zipArchiveManifest = BuildZipArchives(packageBuildInfo, zipArchiveBuilds); } if (fileListBuilds.Length != 0) { fileListManifest = BuildFileLists(packageBuildInfo, fileListBuilds); } if (rawFileBuilds.Length != 0) { rawFileManifest = BuildRawFiles(packageBuildInfo, rawFileBuilds); } var embeddedManifest = BuildFinalPackages(packageBuildInfo, assetBundleManifest, zipArchiveManifest, fileListManifest, rawFileManifest); Cleanup(packageBuildInfo, assetBundleManifest, zipArchiveManifest, fileListManifest, rawFileManifest, embeddedManifest); packageBuildInfo.DoAnalyze(); packageBuildInfo.data.build++; packageBuildInfo.data.MarkAsDirty(); Debug.Log( $"{packageBuildInfo.packagePath}: build bundles finished. {assetBundleBuilds.Length} assetbundles. {zipArchiveBuilds.Count} zip archives. {fileListBuilds.Length} file lists. {embeddedManifest.bundles.Count} bundles to streamingassets."); }
private static void CleanupRecursively(string innerPath, string relativeDir, FileListManifest fileListManifest, bool bStreamingAssets) { foreach (var dir in Directory.GetDirectories(innerPath)) { var info = new DirectoryInfo(dir); CleanupRecursively(dir, relativeDir + '/' + info.Name, fileListManifest, bStreamingAssets); } foreach (var file in Directory.GetFiles(innerPath)) { var fi = new FileInfo(file); var filename = NormalizeFileName(relativeDir + '/' + fi.Name); var match = false; if (fileListManifest != null) { foreach (var entry in fileListManifest.fileEntrys) { // Debug.LogFormat("!! {0} {1}", file, filename); if (filename == entry.assetPath) { if (!bStreamingAssets || entry.streamingAssets) { match = true; break; } } } } if (!match) { fi.Delete(); } } }
// 生成最终包文件, 生成最终清单 public static EmbeddedManifest BuildFinalPackages(PackageBuildInfo buildInfo, AssetBundleManifest assetBundleManifest, ZipArchiveManifest zipArchiveManifest, FileListManifest fileListManifest, RawFileManifest rawFileManifest) { var data = buildInfo.data; var manifest = new Manifest(); manifest.chunkSize = data.chunkSize; manifest.build = buildInfo.data.build; manifest.timestamp = (int)(DateTime.Now - new DateTime(1970, 1, 1)).TotalSeconds; manifest.tag = buildInfo.sharedBuildInfo.tag; var embeddedManifest = new EmbeddedManifest(); if (assetBundleManifest != null) { var assetBundles = assetBundleManifest.GetAllAssetBundles(); foreach (var assetBundle in assetBundles) { BundleBuilderData.BundleInfo bundleInfo; BundleBuilderData.BundleSplit bundleSplit; BundleBuilderData.BundleSlice bundleSlice; if (TryGetBundleSlice(data, assetBundle, out bundleInfo, out bundleSplit, out bundleSlice)) { // Debug.Log(bundleInfo.name); var fileEntry = EncryptFileEntry(data, buildInfo, bundleSplit.encrypted, bundleSlice.name, buildInfo.assetBundlePath); var bundle = new Manifest.BundleInfo(); bundle.comment = bundleInfo.note; bundle.tag = bundleInfo.tag; bundle.encrypted = bundleSplit.encrypted; bundle.rsize = fileEntry.rsize; bundle.type = Manifest.BundleType.AssetBundle; bundle.name = fileEntry.name; bundle.checksum = fileEntry.checksum; bundle.size = fileEntry.size; bundle.load = bundleInfo.load; bundle.priority = GetPriority(buildInfo, bundleInfo, bundleSlice); if (bundleSlice.lastBuildSize != fileEntry.size) { bundleSlice.lastBuildSize = fileEntry.size; data.MarkAsDirty(); } for (int assetIndex = 0, assetCount = bundleSlice.GetAssetCount(); assetIndex < assetCount; assetIndex++) { var assetPath = bundleSlice.GetAssetPath(assetIndex); bundle.assets.Add(assetPath); } bundle.dependencies = assetBundleManifest.GetAllDependencies(assetBundle); buildInfo.CreateEntry(bundle); manifest.bundles.Add(bundle); if (bundleSlice.streamingAssets || data.streamingAssetsAnyway) { embeddedManifest.bundles.Add(fileEntry); } } } } if (zipArchiveManifest != null) { foreach (var zipArchive in zipArchiveManifest.archives) { BundleBuilderData.BundleInfo bundleInfo; BundleBuilderData.BundleSplit bundleSplit; BundleBuilderData.BundleSlice bundleSlice; if (TryGetBundleSlice(data, zipArchive.name, out bundleInfo, out bundleSplit, out bundleSlice)) { var fileEntry = EncryptFileEntry(data, buildInfo, bundleSplit.encrypted, zipArchive.name, buildInfo.zipArchivePath); var bundle = new Manifest.BundleInfo(); bundle.comment = bundleInfo.note; bundle.tag = bundleInfo.tag; bundle.encrypted = bundleSplit.encrypted; bundle.rsize = fileEntry.rsize; bundle.type = Manifest.BundleType.ZipArchive; bundle.name = fileEntry.name; bundle.checksum = fileEntry.checksum; bundle.size = fileEntry.size; bundle.load = bundleInfo.load; bundle.priority = GetPriority(buildInfo, bundleInfo, bundleSlice); foreach (var assetPath in zipArchive.assets) { bundle.assets.Add(assetPath); } manifest.bundles.Add(bundle); if (bundleSlice.streamingAssets || data.streamingAssetsAnyway) { embeddedManifest.bundles.Add(fileEntry); } } } } if (fileListManifest != null) { foreach (var fileList in fileListManifest.fileLists) { var bundleInfo = GetBundleInfo(data, fileList.name); var fileListPath = Path.Combine(buildInfo.packagePath, fileList.name); var fileEntry = GenFileEntry(fileList.name, fileListPath); var bundle = new Manifest.BundleInfo(); bundle.comment = bundleInfo.note; bundle.tag = bundleInfo.tag; bundle.type = Manifest.BundleType.FileList; bundle.name = fileList.name; bundle.checksum = fileEntry.checksum; bundle.size = fileEntry.size; bundle.load = bundleInfo.load; bundle.priority = bundleInfo.priority; buildInfo.filelist.Add(fileList.name); foreach (var bundleTargets in bundleInfo.targets) { var targetPath = bundleTargets.targetPath; bundle.assets.Add(targetPath); } manifest.bundles.Add(bundle); if (bundleInfo.streamingAssets || data.streamingAssetsAnyway) { embeddedManifest.bundles.Add(fileEntry); } } } WriteManifest(buildInfo, manifest); WriteEmbeddedManifest(buildInfo, embeddedManifest); if (buildInfo.buildTarget == EditorUserBuildSettings.activeBuildTarget) { BuildStreamingAssets(buildInfo, fileListManifest); } return(embeddedManifest); }
// 将首包资源复制到 StreamingAssets 目录 (在 BuildPlayer 之前调用) public static void BuildStreamingAssets(PackageBuildInfo buildInfo, FileListManifest fileListManifest) { var packagePath = buildInfo.packagePath; var embeddedManifest = ReadEmbeddedManifest(packagePath); if (embeddedManifest != null && embeddedManifest.bundles.Count > 0) { File.Copy(Path.Combine(packagePath, Manifest.EmbeddedManifestFileName), Path.Combine(buildInfo.streamingAssetsPath, Manifest.EmbeddedManifestFileName), true); if (buildInfo.data.streamingAssetsManifest) { File.Copy(Path.Combine(packagePath, Manifest.ManifestFileName), Path.Combine(buildInfo.streamingAssetsPath, Manifest.ManifestFileName), true); File.Copy(Path.Combine(packagePath, Manifest.ChecksumFileName), Path.Combine(buildInfo.streamingAssetsPath, Manifest.ChecksumFileName), true); } foreach (var bundleInfo in embeddedManifest.bundles) { // Debug.LogFormat("copy {0}", bundleInfo.name); File.Copy(Path.Combine(packagePath, bundleInfo.name), Path.Combine(buildInfo.streamingAssetsPath, bundleInfo.name), true); } if (fileListManifest != null) { foreach (var entry in fileListManifest.fileEntrys) { if (entry.streamingAssets && CopyRawFile(buildInfo.streamingAssetsPath, entry.assetPath)) { Debug.LogWarningFormat("copy xxx {0}", entry.assetPath); } } } AssetDatabase.Refresh(); // cleanup foreach (var dir in Directory.GetDirectories(buildInfo.streamingAssetsPath)) { CleanupRecursively(dir, "Assets", fileListManifest, true); } foreach (var file in Directory.GetFiles(buildInfo.streamingAssetsPath)) { var fi = new FileInfo(file); var match = false; if (fi.Name == Manifest.EmbeddedManifestFileName || fi.Name == Manifest.EmbeddedManifestFileName + ".meta") { continue; } foreach (var bundleInfo in embeddedManifest.bundles) { if (fi.Name == bundleInfo.name || fi.Name == bundleInfo.name + ".meta") { match = true; break; } } if (!match) { if (buildInfo.data.streamingAssetsManifest) { if (!builtinFiles.Contains(fi.Name)) { fi.Delete(); } } else { fi.Delete(); } } } AssetDatabase.Refresh(); } else { PathUtils.CleanupDirectoryRecursively(buildInfo.streamingAssetsPath); } }