工具1-查找项目所有的图片

本文介绍了一个Unity项目中的图片本地化工具,通过该工具可以自动查找项目中的所有图片,并将其复制到指定的目标文件夹,同时生成包含图片路径、MD5值、重复状态等信息的XML文件。
using UnityEngine;
using UnityEditor;
using System.IO;
using System.Collections.Generic;
using System.Xml;

public class LocalTools : MonoBehaviour
{
    static string _sourcePath = @"E:\New Unity Project"; //需要查找的源文件路径
    static string _aimPath = @"E:/aim/aimpng";           //存放需要翻译的图片
    static string _FilePath = @"E:/aim/picturefiles.xml";//存储所有源图片信息的xml文件
    static string[] ImageType = { "*.JPG", "*.PNG", "*.psd" };
    static Dictionary<string, pictureInfo> pictureDic = new Dictionary<string, pictureInfo>(); //xml

    [MenuItem("本地化工具/查找所有图片")]
    static void FindAllPngPicture()
    {
        //判断路径是否存在,_aimPath表示路径参数
        if (Directory.Exists(_aimPath))
        {
            Directory.Delete(_aimPath, true);
        }
        //创建目录
        Directory.CreateDirectory(_aimPath);

        //判断xml文件是否存在,_FilePath表示路径参数
        if (File.Exists(_FilePath))
            File.Delete(_FilePath);
        
        FileStream fs = new FileStream(_FilePath, FileMode.CreateNew);
        StreamWriter sw = new StreamWriter(fs);

        string content = "<root>\n";
        content += findImagePaths(_sourcePath, _aimPath);
        content += "</root>";
        sw.Close();
        fs.Close();
        File.WriteAllText(_FilePath, content);
        AssetDatabase.Refresh();
        EditorUtility.DisplayDialog("提示", "搜索完毕", "OK");
    }

    /// <summary>
    /// 获取所有的图片信息
    /// </summary>
    /// <param name="sourcePath"> 源文件夹路径</param>
    /// <param name="aimPath">目标文件夹路径</param>
    /// <returns></returns>
    static string findImagePaths (string sourcePath,string aimPath)
    {
        string pathStr = string.Empty;
        for (int i = 0; i < ImageType.Length; i++)
        {
            string[] dirs = Directory.GetFiles(sourcePath, ImageType[i]);
            for (int j = 0; j < dirs.Length; j++)
            {
                string md5 = Encrypt.getFileMd5(dirs[j]);
                pathStr += "    <info path=\"" + dirs[j] + "\" md5=\"" + md5 + "\"";
                string destFileName = string.Empty;
                destFileName = aimPath + "/" + System.IO.Path.GetFileName(dirs[j]);
                if (File.Exists(@destFileName))
                {
                    destFileName = aimPath + "/"+ md5 + System.IO.Path.GetExtension(dirs[j]);
                    pathStr += " repeat=\"true\"";
                }
                else
                {
                    pathStr += " repeat=\"false\"";
                }
                File.Copy(dirs[j], destFileName);
                pathStr += " hasFlag=\"false\" />\n";
            }
        }
        string[] childFilePaths = Directory.GetDirectories(sourcePath);
        for(int i = 0;i < childFilePaths.Length;i++)
        {
            pathStr += findImagePaths(childFilePaths[i],aimPath);
        }
        return pathStr;
    }
    class pictureInfo
    {
        public string path;  //原图片路径
        public string md5;  //原图片的md5
        public bool repeat; //图片是否重复标志
        public bool hasFlag; //是否有繁体字
    }
    
    [MenuItem("本地化工具/设置文字图片的标志位")]
    static void SetPictureFlag()
    {
        if (pictureDic.Count <= 0)
        {
            loadPictureXml();
        }
        if(pictureDic.Count > 0)
        {
            for (int i = 0; i < ImageType.Length; i++)
            {
                string[] dirs = Directory.GetFiles(_aimPath, ImageType[i]);
                for (int j = 0; j < dirs.Length; j++)
                {
                    string key = System.IO.Path.GetFileNameWithoutExtension(dirs[j]);
                    if (pictureDic.ContainsKey(key))
                    {
                        pictureDic[key].hasFlag = true;
                    }
                }
            }
            //判断文件是否存在,_FilePath表示路径参数
            if (File.Exists(_FilePath))
                File.Delete(_FilePath);
            FileStream fs = new FileStream(_FilePath, FileMode.CreateNew);
            StreamWriter sw = new StreamWriter(fs);

            string content = "<root>\n";
            foreach (var itemInfo in pictureDic)
            {
                content += "    <info path=\"" + itemInfo.Value.path + "\" md5=\"" + itemInfo.Value.md5 + "\""
                    + " repeat=\"" + itemInfo.Value.repeat + "\""
                    + " hasFlag=\"" + itemInfo.Value.hasFlag + "\" />\n";
            }
            content += "</root>";
            sw.Close();
            fs.Close();
            File.WriteAllText(_FilePath, content);
            AssetDatabase.Refresh();
        }
        EditorUtility.DisplayDialog("提示", "设置完毕", "OK");
    }

    static void loadPictureXml()
    {
        pictureDic.Clear();
        if (File.Exists(_FilePath))
        {
            XmlDocument xmlDoc = new XmlDocument();
            xmlDoc.Load(_FilePath);
            XmlNodeList nodeList = xmlDoc.SelectSingleNode("root").ChildNodes;
            //遍历每一个节点,拿节点的属性以及节点的内容
            foreach (XmlElement xe in nodeList)
            {
                pictureInfo info = new pictureInfo();
                info.path = xe.GetAttribute("path");
                info.md5 = xe.GetAttribute("md5");
                info.repeat = bool.Parse(xe.GetAttribute("repeat"));
                info.hasFlag = bool.Parse(xe.GetAttribute("hasFlag"));
                string key = info.repeat ? info.md5 : Path.GetFileNameWithoutExtension(info.path);
                if (pictureDic.ContainsKey(key))
                {
                    pictureDic[key] = info;
                }
                else
                {
                    pictureDic.Add(key, info);
                }
            }
        }
    }
    /*
    [MenuItem("本地化工具/test")]
    static void createXml()
    {
        //xml保存的路径,这里放在Assets路径 注意路径。
        string filepath =  @"E:/my.xml";
        //继续判断当前路径下是否有该文件
        if (!File.Exists(filepath))
        {
            //创建XML文档实例
            XmlDocument xmlDoc = new XmlDocument();
            //创建root节点,也就是最上一层节点
            XmlElement root = xmlDoc.CreateElement("transforms");
            //继续创建下一层节点
            XmlElement elmNew = xmlDoc.CreateElement("rotation");
            //设置节点的两个属性 ID 和 NAME
            elmNew.SetAttribute("id", "0");
            elmNew.SetAttribute("name", "momo");
            //继续创建下一层节点
            XmlElement rotation_X = xmlDoc.CreateElement("x");
            //设置节点中的数值
            rotation_X.InnerText = "0";
            XmlElement rotation_Y = xmlDoc.CreateElement("y");
            rotation_Y.InnerText = "1";
            XmlElement rotation_Z = xmlDoc.CreateElement("z");
            rotation_Z.InnerText = "2";
            //这里在添加一个节点属性,用来区分。。
            rotation_Z.SetAttribute("id", "1");

            //把节点一层一层的添加至XMLDoc中 ,请仔细看它们之间的先后顺序,这将是生成XML文件的顺序
            elmNew.AppendChild(rotation_X);
            elmNew.AppendChild(rotation_Y);
            elmNew.AppendChild(rotation_Z);
            root.AppendChild(elmNew);
            xmlDoc.AppendChild(root);
            //把XML文件保存至本地
            xmlDoc.Save(filepath);
            Debug.Log("createXml OK!");
        }
    }
    */
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值