UH3L3——生成AB包资源对比文件
分析需求
分析存放AB包的文件夹下哪些文件需要生成资源对比文件
- 需要生成资源对比文件的只有没有后缀的AB包本身,
 
- manifest文件是存储AB包依赖的,一般很少更新改变
 
- meta文件是Unity对存储到Assets文件夹下的文件默认生成的设置配置信息,不需要关注
 
前置知识点
- 文件读写操作:DirectoryInfo、Directory、FileInfo
 
- 编辑器菜单栏添加按钮:[MenuItem()]
 
生成流程
- 在菜单栏添加一个按钮,用于触发该功能
 
- 遍历AB包文件夹,获取所有AB包文件消息,例如文件名,文件路径,文件长度,文件后缀名
 
- 将AB包的文件名、大小、MD码存入到资源对比文件中,MD码的获取复用上节课实现的代码
(文件名、大小、MD码之间用' '分割,不同AB包资源消息之间用'|'分割) 
代码实现
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
   | using System.IO; using System.Security.Cryptography; using System.Text; using UnityEditor; using UnityEngine;
  public class CreateABCompare : MonoBehaviour {     [MenuItem("AB包工具/创建对比文件")]     public static void CreateABCompareFile()     {         DirectoryInfo directory = new DirectoryInfo(Application.dataPath + "/ArtRes/AB/PC/");         FileInfo[] fileInfos = directory.GetFiles();         string abCompareInfo = "";         foreach (FileInfo info in fileInfos)         {                          if (info.Extension == "")             {                                  abCompareInfo += info.Name + " " + info.Length + " " + GetMD5(info.FullName);                                  abCompareInfo += "|";             }         }                  abCompareInfo = abCompareInfo.Substring(0, abCompareInfo.Length - 1);         File.WriteAllText(Application.dataPath + "/ArtRes/AB/PC/ABCompareInfo.txt", abCompareInfo);         Debug.Log("AB包对比文件生成完毕!");         AssetDatabase.Refresh();                 }
      private static string GetMD5(string filePath)     {         using (FileStream file = new FileStream(filePath, FileMode.Open))         {                          MD5 md5 = new MD5CryptoServiceProvider();                          byte[] md5Info = md5.ComputeHash(file);                          StringBuilder sb = new StringBuilder();             file.Close();             for (int i = 0; i < md5Info.Length; i++)                                  sb.Append(md5Info[i].ToString("x2"));             return sb.ToString();         }     } }
   |