static void CreateXML(AssetBundleConfig config, PackageABType packageABType) { //写入xml string xmlPath = ""; if (packageABType == PackageABType.PhoneAB) { xmlPath = EditorAssetPath.ABInfoPath + "/AssetbundleConfig.xml"; } else { xmlPath = EditorAssetPath.ABInfoPath + buildTarget.ToString() + "/AssetbundleConfig.xml"; } if (File.Exists(xmlPath)) { File.Delete(xmlPath); } FileStream fileStream = new FileStream(xmlPath, FileMode.Create, FileAccess.ReadWrite, FileShare.ReadWrite); StreamWriter sw = new StreamWriter(fileStream, System.Text.Encoding.UTF8); XmlSerializer xs = new XmlSerializer(config.GetType()); xs.Serialize(sw, config); sw.Close(); fileStream.Close(); }
//写入数据 //resPathDic : 路径对应的AB包 static void WriteData(Dictionary <string, string> resPathDic, ConfigWritingMode configWritingMode, PackageABType packageABType) { AssetBundleConfig config = new AssetBundleConfig(); config.ABList = new List <ABBase>(); foreach (string path in resPathDic.Keys) { //进行赋值 ABBase abBase = new ABBase(); abBase.Path = path; abBase.Crc = Crc32.GetCrc32(path); abBase.ABName = resPathDic[path]; abBase.AssetName = path.Remove(0, path.LastIndexOf("/", System.StringComparison.Ordinal) + 1); abBase.ABDependce = new List <string>(); string[] resDependce = AssetDatabase.GetDependencies(path); for (int i = 0; i < resDependce.Length; i++) { string tempPath = resDependce[i]; //排除自身与脚本文件 if (tempPath == path || path.EndsWith(".cs", System.StringComparison.Ordinal)) { continue; } string abName = ""; //所依赖的资源是不是在其他AB包里面 if (resPathDic.TryGetValue(tempPath, out abName)) { if (abName == resPathDic[path]) { continue; } //判断是否已经添加到aBBase包里面 if (!abBase.ABDependce.Contains(abName)) { abBase.ABDependce.Add(abName); } } } config.ABList.Add(abBase); } switch (configWritingMode) { case ConfigWritingMode.TXT: string txtPath = EditorAssetPath.ABInfoPath + buildTarget.ToString() + "/AssetbundleConfig.txt"; if (File.Exists(txtPath)) { File.Delete(txtPath); } FileStream fileStream1 = new FileStream(txtPath, FileMode.Create, FileAccess.ReadWrite, FileShare.ReadWrite); StreamWriter sw1 = new StreamWriter(fileStream1, System.Text.Encoding.UTF8); string configJson = SerializeHelper.ToJson(config); sw1.Write(configJson); sw1.Close(); fileStream1.Close(); break; case ConfigWritingMode.XML: CreateXML(config, packageABType); break; case ConfigWritingMode.Binary: foreach (ABBase abBase in config.ABList) { abBase.Path = ""; } string BinaryPath = "Assets/AssetbundleConfig.bytes"; FileStream fs = new FileStream(PathTool.ProjectPath + BinaryPath, FileMode.Create, FileAccess.ReadWrite, FileShare.ReadWrite); fs.Seek(0, SeekOrigin.Begin); fs.SetLength(0); BinaryFormatter bf = new BinaryFormatter(); bf.Serialize(fs, config); fs.Close(); CreateXML(config, packageABType); AssetDatabase.Refresh(); SetABName("assetbundleconfig", BinaryPath); break; } }