编辑器扩展--打包(一)

using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
using System.IO;
using System.Security.Cryptography;
using System.Xml;
using System;
using System.Linq;
using System.Collections;
using LuaInterface;


/// <summary>
/// 打包
/// </summary>
public  class WKABBuilder : EditorWindow
{
  public static   float FileNumbre = 0;
   static  string OutPutPosition = "d:/AssetBundles";
    // static float version = 0;  //当前版本号


    string SourcesPosition = "d:/SVNProject/Assets/Resources/WKLuaTest";




    string packgeDes = "";
    string packageBigDes = "";//升级大包的位置


    string path = "d:/AssetBundles";//打包之后放置的位置


    Texture2D tex = null;


   // static GainAllFileFullName allName;
    /// <summary>
    /// 窗口1的错误提示
    /// </summary>
    string tip = "";


   // List<string> fileDict = new List<string>();


    //添加菜单栏用于打开窗口
    [MenuItem("WKABPackage/Build AssetBundles")]
    static void showWindow()
    {
        EditorWindow.GetWindow(typeof(WKABBuilder));
    }
    bool Init = false;
    void OnGUI()
    {
        if (Init == false)
        {
            DrawWindowOne();
        }
        else
        {
            DrawWindowTwo();
        }
    }
    /// <summary>
    /// 第一个界面
    /// </summary>
    void DrawWindowOne()
    {
        float y = 10;


        GUI.skin.label.fontSize = 14;
        GUI.Label(new Rect(10, y, 120, 20), "打包存储位置:");
        OutPutPosition = "d:/AssetBundles";
        OutPutPosition = GUI.TextField(new Rect(130, y, 500, 20), OutPutPosition);




        y += 30;


        if (GUI.Button(new Rect(10, y, 120, 20), "创建"))
        {
            string Path001 = "d:/AssetBundles/NameNumber";
            if (Directory.Exists(Path001) == false)
            {
                Directory.CreateDirectory(Path001);
                GainAllFileFullName.CreateNameNumberXML();
            }
           // GainAllFileFullName.ReadNameNumberXML();
            if (Directory.Exists(OutPutPosition) == false)
            {
                Directory.CreateDirectory(OutPutPosition);
            }
            CreateFileMXL();
            tip = "创建成功,请读取目录信息";
        }


        if (GUI.Button(new Rect(150, y, 120, 20), "读取"))
        {
             string Path001 = "d:/AssetBundles/NameNumber";
            if (Directory.Exists(Path001) == false)
            {
                Directory.CreateDirectory(Path001);
                GainAllFileFullName.CreateNameNumberXML();
            }
           // GainAllFileFullName.ReadNameNumberXML();
            string path = "d:/AssetBundles/AssetBundles" + FileNumbre;
            // Debug.Log(path);
            if (Directory.Exists(path) == false)
            {
                Directory.CreateDirectory(path);
                //tip = "请先创建打包存储位置";
                CreateFileMXL();
            }
            ReadFileXml();
        }
        GUI.Label(new Rect(50, 80, 10000, 20), tip);




        y += 20;
        if (tex != null)
            GUI.DrawTexture(new Rect(500, 100, 500, 120), tex);
    }
    /// <summary>
    /// 第二个界面
    /// </summary>
    void DrawWindowTwo()
    {
       // ReadFileXml();
        float y = 10;


        GUI.skin.label.fontSize = 14;
        GUI.Label(new Rect(10, y, 120, 20), "导出位置:");
        OutPutPosition = GUI.TextField(new Rect(130, y, 500, 20), OutPutPosition);


        y += 20;


        GUI.Label(new Rect(10, y, 120, 20), "资源目录:");
        SourcesPosition = GUI.TextField(new Rect(130, y, 500, 20), SourcesPosition);


        y += 30;




        GUI.Label(new Rect(10, y, 200, 20), "当前最新版本为:" + FileNumbre);


        OutPutPosition = "d:/AssetBundles/AssetBundles" + FileNumbre;


        y += 30;
        if (GUI.Button(new Rect(10, y, 120, 20), "打包Update版本"))
        {
            //如果文件夹不存在 创建项目文件夹
            if (!Directory.Exists(OutPutPosition))
            {
                Directory.CreateDirectory(OutPutPosition);
            }
            //建立,或者重置一个默认的列表
           // CreateDefaultWorkInIFile();


           //打包
           //获得所有文件的信息,文件名,包名,MD5
           //保存文件列表
           //根据包包含的那些文件,生成BuildMap
           //打包
           //生成包文件列表,包名,MD5,并进行保存
            PackageUpdate();


        }
        GUI.Label(new Rect(130, y, 200, 20), packgeDes);


        y += 30;
        if (GUI.Button(new Rect(10, y, 120, 20), "打包合并版本"))
        {
            //步骤
            //1:先获得最新的文件列表
            //2:读旧的文件列表并进行保存
            //3:合并:新旧列表
            //a:新旧文件的MD5相同,新的数据指向旧的位置
            //b:新增的
            //c: 新旧MD5不同,当做新的用
            //4:打包BuileMap
            //5:记录每个文件的最新文件后缀
            //如果文件夹不存在 创建项目文件夹


            if (!Directory.Exists(OutPutPosition))
            {
                Directory.CreateDirectory(OutPutPosition);
            }
            BuildBigVersion();




            //string url = @"file:///d:/ab/abcdef";
            //Debug.Log("123");
            //AssetBundle.UnloadAllAssetBundles(true);
            //Debug.Log("456");
            //WWW www = WWW.LoadFromCacheOrDownload(url, 1);
            //Debug.Log("789");
            //if (string.IsNullOrEmpty(www.error))
            //{
            //    Debug.Log("100");
            //    AssetBundle myAB = www.assetBundle;
            //    Debug.Log("200");
            //    string namename = "assets/abres/icon_ppp.png";
            //    UnityEngine.Object obj = myAB.LoadAsset(namename);
            //    Debug.Log("300");


            //    tex = (Texture2D)obj;
            //}
            //else
            //    Debug.Log(www.error);


        }
        GUI.Label(new Rect(130, y, 200, 20), packageBigDes);


        y += 30;
        if (tex != null)
            GUI.DrawTexture(new Rect(10, y, 120, 120), tex);


    }


    /// <summary>
    /// 生成新的大版本
    /// </summary>
    void BuildBigVersion()
    {
        //1首先生成文件列表
        Main();
        //2读取旧的文件列表
        ReadOldFileXML();
        RemoveOldSameABName();
        GainAllFileFullName.ReadNameNumberXML();
        //包的名字,不带数字
        GainAllFileFullName.RemoveTheSameABName();


        //CUBE0;Prefab0,Image0...
        GainAllFileFullName.ABFilesNumber();
        foreach (KeyValuePair<string, int> ff in GainAllFileFullName.FilesABNumberDic)
        {
            Debug.Log(ff.Value + "个" + ff.Key);
        }
        //生成文件列表
        CreateFileXML();


        //3:合并新旧文件列表
        CombineNewAndOldFileXML();


        //4:打包
        //3根据包包含的哪些文件,生成BuildMap
        AssetBundleBuild[] buildMap = new AssetBundleBuild[GainAllFileFullName.ABNameNoSame.Count];
        for (int i = 0; i < GainAllFileFullName.ABNameNoSame.Count; i++)
        {
            buildMap[i].assetBundleName = GainAllFileFullName.ABNameNoSame[i] + "_" + GainAllFileFullName.FilesABNumberDic[GainAllFileFullName.ABNameNoSame[i]];
            buildMap[i].assetNames = TheSameFile(buildMap[i].assetBundleName);
        }
        BuildAssetBundleOptions buildOp = BuildAssetBundleOptions.CollectDependencies | BuildAssetBundleOptions.CompleteAssets
           | BuildAssetBundleOptions.DeterministicAssetBundle;
        if (!Directory.Exists(OutPutPosition))
        {
            Directory.CreateDirectory(OutPutPosition);
        }
        // 然后用这个打包地图文件来打包
        BuildPipeline.BuildAssetBundles(OutPutPosition, buildMap, buildOp, BuildTarget.Android);


        //生成包列表
        //包列表 //创建ABList
        CreatMD5(OutPutPosition);
        //下次打包的最新文件的数字
        //FileNumbre += 1;
        ModifyAttribute(@"d:/AssetBundles" + "\\NewFileNumber.xml");
        GainAllFileFullName.ModifyAttributeS(@"d:/AssetBundles" + "\\NameNumber.xml");
    }












    /// <summary>
    /// 第三个步骤:合并新旧列表
    /// </summary>
    void CombineNewAndOldFileXML()
    {
       
        //a:新旧文件的MD5相同
        //b:新增的
        //c:新的和旧的MD5不同,当做新的用
        for (int i = 0; i < GainAllFileFullName._MD5Arr.Count; i++)
        {
            int TheSameIndex = -1;
            string Path = GainAllFileFullName.FileName[i] +"\\"+ GainAllFileFullName.ObjName[i];
            for (int j = 0; j < OldFullName.Count; j++)
            {
                string FileNmaes = OldFullName[j];
                if (FileNmaes.Contains("_"))
                {
                    int index = FileNmaes.IndexOf("_");
                    FileNmaes = FileNmaes.Substring(0, index);
                }
                string oldPath =  @"d:\SVNProject\Assets\Resources\WKLuaTest\" + FileNmaes + @"\" + OldObjName[j];
                //如果新路径和旧的路径相同
                if (Path == oldPath)
                {
                    TheSameIndex = j;
                }
            }
            //路径存在相同的话,就判断这两个文件的MD5码是否相同
            if (TheSameIndex >= 0)
            {
                //MD5码相同的话,就指向旧的包
                //新包                                    旧包
                if (GainAllFileFullName._MD5Arr[i] == OldMD5[TheSameIndex])
                {
                    //旧包
                    //创建一个文档
                    XmlDocument xmlDocumentOld = new XmlDocument();
                    //加载要读取的xml文件
                    xmlDocumentOld.Load(@"d:/AssetBundles/AssetBundles"+(FileNumbre-1)+@"\FileXml.xml");
                    Debug.Log("旧包文件的路径" + @"d:/AssetBundles/AssetBundles" + (FileNumbre - 1) + @"\FileXml.xml");
                    //拿到下边的节点
                    XmlNodeList nodeListOld = xmlDocumentOld.SelectNodes("data/unity3d");
                    //循环遍历
                   string oldFileName = nodeListOld[TheSameIndex].Attributes["FullName"].Value;








                    //最新的文件列表的信息
                    //创建一个文档
                    XmlDocument xmlDocument = new XmlDocument();
                    //加载要读取的xml文件
                    xmlDocument.Load(OutPutPosition + "\\FileXml.xml");
                    Debug.Log("文件的路径" + OutPutPosition + "\\FileXml.xml");
                    //拿到下边的节点
                    XmlNodeList nodeList = xmlDocument.SelectNodes("data/unity3d");
                    //循环遍历
                    nodeList[i].Attributes["FullName"].Value = oldFileName;
                    xmlDocument.Save(OutPutPosition + "\\FileXml.xml");
                }
            }
            else
            {
                //当做新的文件
                Debug.Log("当做新的文件");
            }






            // for(int j = 0; j < OldMD5.Count; j++)
            //{
            //    if (GainAllFileFullName._MD5Arr[i] == OldMD5[j] && GainAllFileFullName.ObjName[i] == OldObjName[j] && GainAllFileFullName.AllFileName[i] + "_" + (GainAllFileFullName.FilesABNumberDic[GainAllFileFullName.AllFileName[i]] - 1) == OldFullName[j])
            //    {
            //        //新旧文件的MD5相同,新的数据指向旧的位置
            //        GainAllFileFullName._MD5Arr[i] = OldMD5[j];
            //        GainAllFileFullName.ObjName[i] = OldObjName[j];
            //        //包的名字需要修改一下
            //        GainAllFileFullName.FilesABNumberDic[GainAllFileFullName.AllFileName[i]]--;


            //        GainAllFileFullName.AllFileName[i] = OldFullName[j];


            //        Debug.Log("相同的");
            //    }
            //    else if (!OldMD5.Contains(GainAllFileFullName._MD5Arr[i]))
            //    {
            //        //原来的md5不包含现在的MD5
            //        //新增的
            //        Debug.Log("新增的");




            //    }
            //    else if (GainAllFileFullName._MD5Arr[i] != OldMD5[j] && GainAllFileFullName.ObjName[i] == OldObjName[j] && GainAllFileFullName.AllFileName[i] + "_" + (GainAllFileFullName.FilesABNumberDic[GainAllFileFullName.AllFileName[i]] - 1) == OldFullName[j])
            //    {
            //        //MD5不同,其他的相同,用当前最新的
            //        Debug.Log("需要更新的");


            //    }
            //}
        }
       


    }






    //上一版本的所有信息
  public  static   List<string> OldObjName = new List<string>();//Image 1.prefab
    public static List<string> OldFullName = new List<string>();//AAAAA_0   CCCCC_0
    public static List<string> OldMD5 = new List<string>();
    public static List<string> OldFileNameWithOutNumber = new List<string>();//不带数字的包的名字,没有重复的AAAAA   CCCCC


    /// <summary>
    /// 读取旧的文件列表
    /// </summary>
    static void ReadOldFileXML()
    {
        OldObjName.Clear();
        OldFullName.Clear();
        OldMD5.Clear();
        if (FileNumbre <= 1)
        {
            return;
        }
        XmlDocument xmlDoc = new XmlDocument();
        string Path = @"d:/AssetBundles/AssetBundles" + (FileNumbre - 1) + "\\FileXML.xml";


        xmlDoc.Load(Path);
        var childeList = xmlDoc.SelectSingleNode("data").ChildNodes;


        XmlNodeList nodearr = xmlDoc.SelectSingleNode("data").ChildNodes;// SelectSingleNode("unity3d").ChildNodes;
        foreach (XmlNode n in nodearr)
        {
            XmlElement e = n as XmlElement;
            OldObjName.Add(e.GetAttribute("objName"));
            OldFullName.Add(e.GetAttribute("FullName"));
            string fileName = e.GetAttribute("FullName");
            int index = fileName.IndexOf("_");
            string Name = fileName.Substring(0, index);
            if (!OldFileNameWithOutNumber.Contains(Name))
                OldFileNameWithOutNumber.Add(Name);
            Debug.Log(Name + "文件夹的名字");
            OldMD5.Add(e.GetAttribute("md5"));
        }
    }




    /// <summary>
    ///删除旧的包名里边的重复的部分
    /// </summary>
    public static void RemoveOldSameABName()
    {
        for (int i = 0; i < OldFullName.Count; i++)
        {
            if (!GainAllFileFullName.OldABName.Contains(OldFullName[i]))
            {
                GainAllFileFullName.OldABName.Add(OldFullName[i]);
            }
        }
    }












































































    /// <summary>
    /// 创建版本号
    /// </summary>
    void CreateDefaultWorkInIFile()
    {
        XmlDocument doc = new XmlDocument();
        XmlElement e = doc.CreateElement("Root");
        doc.AppendChild(e);
        e.SetAttribute("version", FileNumbre.ToString());


        string pathfile = OutPutPosition + "\\ver.bytes";


        doc.Save(pathfile);


        pathfile = OutPutPosition + "\\ver.xml";


        doc.Save(pathfile);
    }


    /// <summary>
    /// 存储文件的最新版本
    /// </summary>
    void CreateFileMXL()
    {
        XmlDocument doc = new XmlDocument();
        XmlElement e = doc.CreateElement("Root");
        doc.AppendChild(e);
        e.SetAttribute("NewFileNumber", FileNumbre.ToString());


        string pathfile = @"d:/AssetBundles" + "\\NewFileNumber.bytes";
        doc.Save(pathfile);
        pathfile = @"d:/AssetBundles" + "\\NewFileNumber.xml";
        doc.Save(pathfile);
    }
    /// <summary>
    /// 读取XML获得NewFileNumber================添加节点  cube_0    Image_0    包名_版本(每次使用之前先读取xml,并吧相关信息存储在DICT里边)
    /// </summary>
    void ReadFileXml()
    {
        XmlDocument xmlDoc = new XmlDocument();
        xmlDoc.Load(@"d:/AssetBundles" + "\\NewFileNumber.xml");
        XmlNode node = xmlDoc.SelectSingleNode("/Root");
        if (node != null)
        {
            string k = node.Attributes["NewFileNumber"].Value;//
            FileNumbre = float.Parse(k)+1;
            //   Debug.Log("读出来的版本数据为" + FileNumbre);
            node = null;
            Init = true;
        }
    }
    /// <summary>
    /// 修改XML里边的NewFileNumber数值
    /// </summary>
    /// <param name="xmlPath"></param>
    public static  void ModifyAttribute(string xmlPath)
    {
        XmlDocument xmlDoc = new XmlDocument();
        xmlDoc.Load(xmlPath);
        XmlElement element = (XmlElement)xmlDoc.SelectSingleNode("Root");
        element.SetAttribute("NewFileNumber", FileNumbre.ToString());
        // Debug.Log("修改了NewFileNumber的值");
        xmlDoc.Save(xmlPath);
    }


   
    /// <summary>
    /// 打包
    /// </summary>
     void PackageUpdate()
    {
        //1首先生成文件列表
        Main();


        //包的名字,不带数字
        GainAllFileFullName.RemoveTheSameABName();
        
        //CUBE0;Prefab0,Image0...
        GainAllFileFullName.ABFilesNumber();


        //2创建文件列表FileXML
        CreateFileXML();


        //3根据包包含的哪些文件,生成BuildMap
        AssetBundleBuild[] buildMap = new AssetBundleBuild[GainAllFileFullName.ABNameNoSame.Count];
        for (int i = 0; i < GainAllFileFullName.ABNameNoSame.Count; i++)
        {
            buildMap[i].assetBundleName = GainAllFileFullName.ABNameNoSame[i] + "_" + GainAllFileFullName.FilesABNumberDic[GainAllFileFullName.ABNameNoSame[i]];
            buildMap[i].assetNames = TheSameFile(buildMap[i].assetBundleName);
        }
        BuildAssetBundleOptions buildOp = BuildAssetBundleOptions.CollectDependencies | BuildAssetBundleOptions.CompleteAssets
           | BuildAssetBundleOptions.DeterministicAssetBundle;
        if (!Directory.Exists(OutPutPosition))
        {
            Directory.CreateDirectory(OutPutPosition);
        }
        // 然后用这个打包地图文件来打包
        BuildPipeline.BuildAssetBundles(OutPutPosition, buildMap, buildOp, BuildTarget.Android);


        //生成包列表
        //包列表 //创建ABList
        CreatMD5(OutPutPosition);    
        ModifyAttribute(@"d:/AssetBundles" + "\\NewFileNumber.xml");
    }


    /// <summary>
    /// 创建MD5码
    /// </summary>
    /// <param name="Path"></param>
    public   static    void CreatMD5(string Path)
    {
        List<String> _MD5Arr = new List<string>();
        for (int i = 0; i < GainAllFileFullName.ABNameNoSame.Count; i++)
        {
            // 路径+包名
            string lose = @"d:\AssetBundles\AssetBundles" + FileNumbre + @"\" + GainAllFileFullName.ABNameNoSame[i]+ "_"+GainAllFileFullName.FilesABNumberDic[GainAllFileFullName.ABNameNoSame[i]];
            //Debug.Log("lose000001" + lose);


            string md5 = GetFileHash(lose);
            _MD5Arr.Add(md5);
        }


        //生成和创建新版本
        XmlDocument doc = new XmlDocument();
        XmlElement e = doc.CreateElement("data");
        doc.AppendChild(e);


        for (int i = 0; i < GainAllFileFullName.ABNameNoSame.Count; i++)
        {
            string name0 = GainAllFileFullName.ABNameNoSame[i];
            string name1 = "";
            if (name0.Contains("_"))
            {
                int index = name0.IndexOf("_");
                name1 = name0.Substring(0, index);
            }
            else
            {
                name1 = name0;
            }


            XmlElement n = doc.CreateElement("unity3d");
            e.AppendChild(n);


            n.SetAttribute("FileName",name1+ "_"+GainAllFileFullName.FilesABNumberDic[name1]);
            n.SetAttribute("md5", _MD5Arr[i]);
        }
        string verFile = Path + "\\ABList.bytes";
        doc.Save(verFile);
        string verFile1 = Path + "\\ABList.xml";
        doc.Save(verFile1);
    }


    /// <summary>
    /// 得到所有文件的MD5码
    /// </summary>
    public static string GetFileHash(string filePath)
    {
        try
        {
            FileStream fs = new FileStream(filePath, FileMode.Open);
            int len = (int)fs.Length;
            byte[] data = new byte[len];
            fs.Read(data, 0, len);
            fs.Close();
            MD5 md5 = new MD5CryptoServiceProvider();
            byte[] result = md5.ComputeHash(data);
            string fileMD5 = "";
            foreach (byte b in result)
            {
                fileMD5 += Convert.ToString(b, 16);
            }
            return fileMD5;
        }
        catch (FileNotFoundException e)
        {
            return e.ToString();
        }
    }


    static void Main()
    {
        //获取当前程序所在的文件路径
        String path = "d:/SVNProject/Assets/Resources/WKLuaTest";


        String path2 = path.Substring(0, 40);     //取盘符
        try
        {
            //文件的路径
            getDirectory(path);
        }
        catch (IOException e)
        {
            Console.WriteLine(e.Message);
        }
        finally
        {
            //Debug.Log("完成");
        }


    }




    /*
     * 获得指定路径下所有文件名
     * StreamWriter sw  文件写入流
     * string path      文件路径
     * int indent       输出时的缩进量
     */
    public static void getFileName(string path)
    {
        DirectoryInfo root = new DirectoryInfo(path);
        for (int i = 0; i < root.GetFiles().Count(); i++)
        {
            if (!root.GetFiles()[i].FullName.EndsWith(".meta"))
            {
                if (!GainAllFileFullName.LongPath.Contains(root.GetFiles()[i].FullName))
                {
                    string fileName = root.GetFiles()[i].FullName;
                    //文件的完整路径
                    GainAllFileFullName.LongPath.Add(fileName);
                    fileName = fileName.Remove(0, 41);
                    int index = fileName.IndexOf(@"\");
                    fileName = fileName.Substring(0, index);
                    // 文件夹的名字
                    GainAllFileFullName.AllFileName.Add(fileName);
                    Debug.Log(fileName + "文件夹的名字");
                    if (!OldFileNameWithOutNumber.Contains(fileName))
                        OldFileNameWithOutNumber.Add(fileName);
                    //文件的名字
                    GainAllFileFullName.GetObjName(root.GetFiles()[i].FullName);
                }
            }
        }
    }


    //获得指定路径下所有子目录名
    public static void getDirectory(string path)
    {
        getFileName(path);
        DirectoryInfo root = new DirectoryInfo(path);
        foreach (DirectoryInfo d in root.GetDirectories())
        {
            getDirectory(d.FullName);
        }
    }








    /// <summary>
    /// 创建每个文件的MD5
    /// </summary>
    /// <param name="path"></param>
    public static  void BuildMD5(string path)
    {
        string md5 = GetFileHash(path);
        GainAllFileFullName._MD5Arr.Add(md5);
    }


    //最新文件的MD5,以及包的名字
    //Dictionary<string , string> NewABName = new Dictionary<string, string>();
    /// <summary>
    /// 生成文件列表
    /// </summary>
    void CreateFileXML()
    {
        // Debug.Log(allName.ObjName.Count + "/////" + allName.AllFileName.Count + "/////" + allName._MD5Arr.Count + "//////////");
        //生成和创建新版本
        XmlDocument doc = new XmlDocument();
        XmlElement e = doc.CreateElement("data");
        doc.AppendChild(e);
       // Debug.Log(allName.FilesABNumberDic.Count + "字典的长度1");
        for (int i = 0; i < GainAllFileFullName.ObjName.Count; i++)
        {
            XmlElement n = doc.CreateElement("unity3d");
            e.AppendChild(n);
            
            if (n.HasAttribute(GainAllFileFullName.ObjName[i]))
            {
                n.SetAttribute("FullName", GainAllFileFullName.AllFileName[i] + "_" + GainAllFileFullName.FilesABNumberDic[GainAllFileFullName.AllFileName[i]].ToString());
                n.SetAttribute("md5", GainAllFileFullName._MD5Arr[i]);
            }
            else
            {
                n.SetAttribute("objName", GainAllFileFullName.ObjName[i]);
                //包名加数字
                n.SetAttribute("FullName", GainAllFileFullName.AllFileName[i]+"_"+ GainAllFileFullName.FilesABNumberDic[GainAllFileFullName.AllFileName[i]].ToString());


                n.SetAttribute("md5", GainAllFileFullName._MD5Arr[i]);
            }
        }
        string pathfile = OutPutPosition + "\\FileXML.byte";
        doc.Save(pathfile);
        pathfile = OutPutPosition + "\\FileXml.xml";
        doc.Save(pathfile);
    }


    public static void ModifyAFileXML(string xmlPath)
    {
        XmlDocument xmlDoc = new XmlDocument();
        xmlDoc.Load(xmlPath);
        xmlDoc.RemoveAll();
        XmlElement e = xmlDoc.CreateElement("data");
        xmlDoc.AppendChild(e);


        // Debug.Log(allName.FilesABNumberDic.Count + "字典的长度1");
        for (int i = 0; i < GainAllFileFullName.ObjName.Count; i++)
        {
            string name0 = GainAllFileFullName.AllFileName[i];
            string name1 = "";
            if (name0.Contains("_"))
            {
                int index = name0.IndexOf("_");
                name1 = name0.Substring(0, index);
            }
            else
            {
                name1 = name0;
            }
            XmlElement n = xmlDoc.CreateElement("unity3d");
            e.AppendChild(n);
            if (n.HasAttribute(GainAllFileFullName.ObjName[i]))
            {
                n.SetAttribute("FullName", name1 +"_"+ GainAllFileFullName.FilesABNumberDic[name1]);
                n.SetAttribute("md5", GainAllFileFullName._MD5Arr[i]);
            }
            else
            {
                n.SetAttribute("objName", GainAllFileFullName.ObjName[i]);
                // n.SetAttribute("FullName", allName.AllFileName[i]);
                //包名加数字
                n.SetAttribute("FullName",name1 + "_" + GainAllFileFullName.FilesABNumberDic[name1]);
                n.SetAttribute("md5", GainAllFileFullName._MD5Arr[i]);
            }
        }
        xmlDoc.Save(xmlPath);
        xmlDoc.Save(xmlPath);
    }








    public string[] TheSameFile(string name)
    {
        string path = Application.dataPath + "/" + "Resources/WKLuaTest";
        // Debug.Log(path + "路径");
        int llength = GainAllFileFullName.LongPath.Count;
        string[] files = new string[llength];
        int bpos = Application.dataPath.Length + 1;


        for (int i = 0; i < GainAllFileFullName.LongPath.Count; i++)
        {
            files[i] = "Assets/" + GainAllFileFullName.LongPath[i].Substring(bpos);
        }
        //一个文件夹中的文件的个数
        int count = 0;
        //其中的路径
        List<string> pathName = new List<string>();
        for (int i = 0; i <OldFileNameWithOutNumber .Count; i++)
        {
            if (OldFileNameWithOutNumber[i]+"_"+GainAllFileFullName.FilesABNumberDic[OldFileNameWithOutNumber[i]] == name)
            {
                count++;
                pathName.Add(files[i]);
            }
        }
        string[] EndName = new string[count];
        for (int i = 0; i < OldFileNameWithOutNumber.Count; i++)
        {
            if (OldFileNameWithOutNumber[i] + "_" + GainAllFileFullName.FilesABNumberDic[OldFileNameWithOutNumber[i]] == name)
            {
                for (int j = 0; j < count; j++)
                {
                    EndName[j] = pathName[j];
                }
            }
        }
        return EndName;
    }










}








 

猜你喜欢

转载自blog.csdn.net/wk201403010114/article/details/81002272
今日推荐