private static void GenerateVersionConfig(string outputPath, List <string> finalZips, List <string> finalFiles) { string abManifestFile; if (projectTag != "") { abManifestFile = Path.Combine(outputPath, projectTag); } else { abManifestFile = Path.Combine(outputPath, GetPlatformName()); } AssetBundle ab = AssetBundle.LoadFromFile(abManifestFile); AssetBundleManifest abMainfest = (AssetBundleManifest)ab.LoadAsset("AssetBundleManifest"); string[] allABNames = abMainfest.GetAllAssetBundles(); XmlDocument xmlDoc = new XmlDocument(); XmlElement xmlRoot = xmlDoc.CreateElement("config"); xmlRoot.SetAttribute("res_version", QResSystemBuilder.resVersion); xmlDoc.AppendChild(xmlRoot); assetBundleInfos.Clear(); for (int i = 0; i < allABNames.Length; i++) { XmlElement xmlItem = CreateConfigItem(xmlDoc, Path.Combine(outputPath, allABNames[i]), allABNames[i], allABNames[i]); xmlRoot.AppendChild(xmlItem); AssetBundle assetBundle = AssetBundle.LoadFromFile(Path.Combine(outputPath, allABNames[i])); AssetBundleInfo abInfo = new AssetBundleInfo(allABNames[i]); abInfo.assets = assetBundle.GetAllAssetNames(); assetBundleInfos.Add(abInfo); assetBundle.Unload(true); } // 这里要加上平台相关的xml string platformBundleName = GetPlatformName(); XmlElement platformItem; if (projectTag == "") { platformItem = CreateConfigItem(xmlDoc, abManifestFile, platformBundleName, platformBundleName); } else { platformItem = CreateConfigItem(xmlDoc, abManifestFile, projectTag, projectTag); } xmlRoot.AppendChild(platformItem); foreach (var zipPath in finalZips) { XmlElement zipItem = CreateConfigItem(xmlDoc, zipPath, Path.GetFileName(zipPath), Path.GetFileName(zipPath)); xmlRoot.AppendChild(zipItem); } foreach (var filePath in finalFiles) { XmlElement fileItem = CreateConfigItem(xmlDoc, filePath, Path.GetFileName(filePath), Path.GetFileName(filePath)); xmlRoot.AppendChild(fileItem); } XmlElement filTiem = CreateConfigItem(xmlDoc, Application.streamingAssetsPath + FrameworkConfigData.RELATIVE_AB_ROOT_FOLDER + FrameworkConfigData.EXPORT_ASSETBUNDLE_CONFIG_FILENAME, FrameworkConfigData.EXPORT_ASSETBUNDLE_CONFIG_FILENAME, FrameworkConfigData.EXPORT_ASSETBUNDLE_CONFIG_FILENAME); xmlRoot.AppendChild(filTiem); ab.Unload(true); xmlDoc.Save(outputPath + "/resconfig.xml"); AssetDatabase.Refresh(); if (QResSystemBuilder.isEnableGenerateClass) { if (!Directory.Exists("QGameData")) { Directory.CreateDirectory("QGameData"); } var path = Path.GetFullPath(Application.dataPath + Path.DirectorySeparatorChar + "QGameData/QAssets.cs"); StreamWriter writer = new StreamWriter(File.Open(path, FileMode.Create)); QBundleInfoGenerator.WriteClass(writer, "QAssetBundle", assetBundleInfos, projectTag); writer.Close(); AssetDatabase.Refresh(); } }
public static void WriteClass(TextWriter writer, string ns, List <AssetBundleInfo> assetBundleInfos, string projectTag = null) { var compileUnit = new CodeCompileUnit(); var codeNamespace = new CodeNamespace(ns); compileUnit.Namespaces.Add(codeNamespace); for (int i = 0; i < assetBundleInfos.Count; i++) { AssetBundleInfo assetBundleInfo = assetBundleInfos [i]; string className = assetBundleInfo.name; string bundleName = className.Substring(0, 1).ToUpper() + className.Substring(1); int firstNumber; if (int.TryParse(bundleName[0].ToString(), out firstNumber)) { continue; } className = className.Substring(0, 1).ToUpper() + className.Substring(1).Replace("/", "_").Replace("@", "_").Replace("!", "_"); if (!string.IsNullOrEmpty(projectTag)) { className = className.Replace("_project_" + projectTag, ""); bundleName = bundleName.Replace("_project_" + projectTag, ""); } var codeType = new CodeTypeDeclaration(className); codeNamespace.Types.Add(codeType); CodeMemberField bundleNameField = new CodeMemberField(); bundleNameField.Attributes = MemberAttributes.Public | MemberAttributes.Const; bundleNameField.Name = "BundleName"; bundleNameField.Type = new CodeTypeReference(typeof(System.String)); codeType.Members.Add(bundleNameField); bundleNameField.InitExpression = new CodePrimitiveExpression(bundleName.ToUpperInvariant()); Dictionary <string, string> checkRepeatDict = new Dictionary <string, string> (); foreach (var asset in assetBundleInfo.assets) { CodeMemberField assetField = new CodeMemberField(); assetField.Attributes = MemberAttributes.Public | MemberAttributes.Const; string content = Path.GetFileNameWithoutExtension(asset).ToUpperInvariant(); assetField.Name = content.Replace("@", "_").Replace("!", "_"); assetField.Type = new CodeTypeReference(typeof(System.String)); if (!assetField.Name.StartsWith("[") && !assetField.Name.StartsWith(" [") && !checkRepeatDict.ContainsKey(assetField.Name)) { checkRepeatDict.Add(assetField.Name, asset); codeType.Members.Add(assetField); } assetField.InitExpression = new CodePrimitiveExpression(content); } checkRepeatDict.Clear(); } var provider = new CSharpCodeProvider(); var options = new CodeGeneratorOptions(); options.BlankLinesBetweenMembers = false; options.BracingStyle = "C"; provider.GenerateCodeFromCompileUnit(compileUnit, writer, options); }