工具1-查找项目所有的图片
2017-12-08 17:27
253 查看
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!"); } } */ }
相关文章推荐
- js查找某元素中的所有图片地址的方法
- 写了个下载百度贴吧某个贴子,所有图片的工具
- iOS------自动查找项目中不用的图片资源
- Android:认识R类、findViewById方法查找组件、@string查找字符、@color查找颜色、@drawable查找图片、@dimen某个组件尺寸定义、项目引入资源国际化
- python3.6深度学习图片集处理项目源代码(为cnn等准备图片集合,批量生成、删除、改名、旋转、调整图片大小,包括删除指定路径下所有文件)
- 利用OPENCV写的对文件夹下所有图片大小归一化的小工具
- 适配所有机型的保存图片工具
- javaWeb项目springMVC框架下利用ITextpdf 工具打印PDF文件的方法(打印表单、插入图片)
- vs2012工具>选项中VC++目录中的编辑功能被否决,区别VS2008一次配置所有项目有效
- js查找某元素中的所有图片地址的方法
- android studio中文件内容查找相当于Eclipse中Ctrl+H & 查找项目中所有中文
- 编辑器工具--NGUI--图集相关--指定图集,对选中预设中所有使用本图集的图片绑定本图集
- js查找某元素中的所有图片地址
- 利用OPENCV写的对文件夹下所有图片大小归一化的小工具
- Picasa2图片查找浏览工具
- js查找某元素中的所有图片地址
- Android 开源项目android-open-project工具库解析之(一) 依赖注入,图片缓存,网络相关,数据库orm工具包,Android公共库
- Android 开源项目android-open-project工具库解析之(一) 依赖注入,图片缓存,网络相关,数据库orm工具包,Android公共库
- 如何在项目中查找内存泄漏以及有哪些帮助工具?
- 工具--打印出项目所有字体的用处