public static void SelectPlatform(AssetBundleBuildRule.Platform platform, bool selected) { if (selected) { AssetBundleRule.Platforms |= platform; } else { AssetBundleRule.Platforms &= ~platform; } }
private static BuildTarget GetBuildTarget(AssetBundleBuildRule.Platform platform) { switch (platform) { case AssetBundleBuildRule.Platform.Windows32: return(BuildTarget.StandaloneWindows); case AssetBundleBuildRule.Platform.Windows64: return(BuildTarget.StandaloneWindows64); case AssetBundleBuildRule.Platform.MacOs: return(BuildTarget.StandaloneOSX); case AssetBundleBuildRule.Platform.Ios: return(BuildTarget.iOS); case AssetBundleBuildRule.Platform.Android: return(BuildTarget.Android); default: throw new Exception("打包平台不存在.... 请检查"); } }
private static void ProcessPackage(string workingPath, string tempworkingPath, string packagePath, AssetBundleBuildRule.Platform platform) { AssetBundleFileDatas.Clear(); string packageVersionPath = $"{packagePath}{AppConst.AssetBundleConfig.VersionFile}"; string assetbundleZipPath = $"{packagePath}{AppConst.AssetBundleConfig.AssetBundlePackageFile}"; string fileInfoPath = $"{packagePath}{AppConst.AssetBundleConfig.FileListFile}"; string[] allFiles = Directory.GetFiles(workingPath, "*", SearchOption.AllDirectories); List <string> allAssetBundleFiles = new List <string>(); foreach (string tempFile in allFiles) { string path = tempFile.Replace("\\", "/"); if (Path.GetExtension(path) == ".manifest") { continue; } allAssetBundleFiles.Add(path); } EditorUtility.ClearProgressBar(); int index = 0; int count = allAssetBundleFiles.Count; long sizeCount = 0; foreach (string assetBundleFile in allAssetBundleFiles) { index++; EditorUtility.DisplayProgressBar("复制AssetBundle", "复制AssetBundle文件", (float)index / (float)count); string target = assetBundleFile.Replace(workingPath, tempworkingPath); string targetPath = Path.GetDirectoryName(target); if (!Directory.Exists(targetPath)) { if (targetPath != null) { Directory.CreateDirectory(targetPath); } } File.Copy(assetBundleFile, target); AssetBundleFileData assetBundleFileData = new AssetBundleFileData(); byte[] srcBytes = File.ReadAllBytes(assetBundleFile); int srcLength = srcBytes.Length; sizeCount += srcBytes.Length; byte[] srcMd5Bytes = MD5Utility.GetMd5Bytes(srcBytes); int srcMd5Code = BitConverter.ToInt32(srcMd5Bytes, 0); string assetBundleName = target.Replace(tempworkingPath, ""); assetBundleFileData.AssetBundleName = assetBundleName; assetBundleFileData.Length = srcLength; assetBundleFileData.Md5Code = srcMd5Code; assetBundleFileData.ZipLength = 0; assetBundleFileData.ZipMd5Code = 0; AssetBundleFileDatas.Add(assetBundleFileData); } EditorUtility.ClearProgressBar(); ZipUtility.CompressFolder(assetbundleZipPath, tempworkingPath, "*", AssetBundleRule.ZipPassWord); using (ByteBuffer buffer = new ByteBuffer()) { ValueParse.WriteValue(buffer, platform.ToString(), ValueParse.StringParse); ValueParse.WriteValue(buffer, CurrentVersion.MasterVersion, ValueParse.IntParse); ValueParse.WriteValue(buffer, CurrentVersion.MinorVersion, ValueParse.IntParse); ValueParse.WriteValue(buffer, CurrentVersion.RevisedVersion, ValueParse.IntParse); ValueParse.WriteValue(buffer, allAssetBundleFiles.Count, ValueParse.IntParse); ValueParse.WriteValue(buffer, sizeCount, ValueParse.LongParse); ValueParse.WriteValue(buffer, string.IsNullOrEmpty(AssetBundleRule.AssetBundleVariant) ? "" : AssetBundleRule.AssetBundleVariant, ValueParse.StringParse); ValueParse.WriteValue(buffer, AssetBundleRule.ZipSelected, ValueParse.BoolParse); ValueParse.WriteValue(buffer, string.IsNullOrEmpty(AssetBundleRule.ZipPassWord) ? "" : AssetBundleRule.ZipPassWord, ValueParse.StringParse); File.WriteAllBytes(packageVersionPath, buffer.ToBytes()); } using (ByteBuffer buffer = new ByteBuffer()) { ValueParse.WriteValue(buffer, AssetBundleFileDatas.Count, ValueParse.IntParse); foreach (AssetBundleFileData assetBundleFileData in AssetBundleFileDatas) { ValueParse.WriteValue(buffer, assetBundleFileData.AssetBundleName, ValueParse.StringParse); ValueParse.WriteValue(buffer, assetBundleFileData.Length, ValueParse.IntParse); ValueParse.WriteValue(buffer, assetBundleFileData.Md5Code, ValueParse.IntParse); ValueParse.WriteValue(buffer, assetBundleFileData.ZipLength, ValueParse.IntParse); ValueParse.WriteValue(buffer, assetBundleFileData.ZipMd5Code, ValueParse.IntParse); } File.WriteAllBytes(fileInfoPath, buffer.ToBytes()); } }
public static bool BuildAssetBundles(AssetBundleBuildRule.Platform platform, BuildAssetBundleOptions buildAssetBundleOptions) { if (!IsPlatformSelected(platform)) { return(true); } string platformName = platform.ToString(); string workingPath = $"{WorkingDirectory}{platformName}/"; string tempworkingPath = $"{TempWorkingDirectory}{platformName}/"; string packagePath = $"{PackageDirectory}{platformName}/"; string updatePath = $"{UpdateFullDirectory}{platformName}/"; if (!Directory.Exists(workingPath)) { Directory.CreateDirectory(workingPath); } if (Directory.Exists(tempworkingPath)) { Directory.Delete(tempworkingPath, true); } Directory.CreateDirectory(tempworkingPath); if (Directory.Exists(packagePath)) { Directory.Delete(packagePath, true); } Directory.CreateDirectory(packagePath); if (Directory.Exists(updatePath)) { Directory.Delete(updatePath, true); } Directory.CreateDirectory(updatePath); AssetBundleManifest assetBundleManifest = BuildPipeline.BuildAssetBundles(workingPath, buildAssetBundleOptions, GetBuildTarget(platform)); if (assetBundleManifest == null) { return(false); } string[] assetBundles = assetBundleManifest.GetAllAssetBundles(); string[] fileNames = Directory.GetFiles(workingPath, "*", SearchOption.AllDirectories); foreach (string fileName in fileNames) { if (fileName.EndsWith(".manifest")) { continue; } var strPath = fileName.Replace(workingPath, ""); if (strPath != platform.ToString() && !assetBundles.Contains(strPath)) { FileUtility.DeleteFile(fileName); } } string[] manifestNames = Directory.GetFiles(workingPath, "*.manifest", SearchOption.AllDirectories); foreach (string manifestName in manifestNames) { if (!File.Exists(manifestName.Substring(0, manifestName.LastIndexOf('.')))) { File.Delete(manifestName); } } PathUtility.RemoveEmptyDirectory(workingPath); ProcessUpdate(workingPath, updatePath, platform); ProcessPackage(workingPath, tempworkingPath, packagePath, platform); return(true); }
public static bool IsPlatformSelected(AssetBundleBuildRule.Platform platform) { return((AssetBundleRule.Platforms & platform) != 0); }