博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Odin Inspector 系列教程 --- Odin工具箱【一键查找重复文件】
阅读量:4147 次
发布时间:2019-05-25

本文共 4682 字,大约阅读时间需要 15 分钟。

前言:随着项目进度的不断开展,在工程文件里面或多或少总有那么几个重复文件,或者名称相同,或者MD5值相同(一样的文件复制多份但是名称不同)

笔者制作了这个基于Odin的一键查找工具,方便大家查找项目中被遗忘的文(la)(ji)

7643202-cdf6ff8bbbaf2f71.gif

示例工程中已经写好备注,方便各位同学魔改

欢迎感兴趣的小伙伴,推送各种基于Odin制作的轻量工具

完整示例代码
using System;using System.Collections;using System.Collections.Generic;using System.IO;using System.Linq;using UnityEngine;using UnityEngine.UI;using UnityEngine.Timeline;using Sirenix.OdinInspector;using Sirenix.OdinInspector.Editor;using UnityEditor;using System.Threading;public class OneKeySearchDuplicateFiles : SerializedScriptableObject{    private bool IsToggled;    private int maxCount;    private IEnumerator
fileInfoIEnumerator; [PropertySpace(10)] [Title("需要搜索的文件夹", "默认为Asset全目录", titleAlignment: TitleAlignments.Split)] [FolderPath(ParentFolder = "Assets", RequireExistingPath = true, AbsolutePath = true)] [LabelText("选择你要搜索的文件夹")] public string targetSearchFolder; [ShowInInspector] [DictionaryDrawerSettings(KeyLabel = "MD5值", ValueLabel = "文件名称列表")] private Dictionary
> sameMD5Group = new Dictionary
>(); [ShowInInspector] [DictionaryDrawerSettings(KeyLabel = "文件名称", ValueLabel = "绝对路径列表")] private Dictionary
> sameNameGroup = new Dictionary
>(); [ShowInInspector] [TitleGroup("重复文件列表")] [HorizontalGroup("重复文件列表/重复文件")] [BoxGroup("重复文件列表/重复文件/MD5值相同", CenterLabel = true)] [PropertyOrder(1000)] [InfoBox("发现相同MD5值文件.", InfoMessageType.Error, "CheckSameMD5ResultGroup")] [ShowIf("$CheckSameMD5ResultGroup")] [DictionaryDrawerSettings(KeyLabel = "MD5值", ValueLabel = "相同MD5值文件名称")] private Dictionary
> sameMD5Result5Group = new Dictionary
>(); [BoxGroup("重复文件列表/重复文件/名称值相同", CenterLabel = true)] [ShowInInspector] [PropertyOrder(1000)] [InfoBox("发现相同名称文件.", InfoMessageType.Error, "CheckSameNameResultGroup")] [ShowIf("$CheckSameNameResultGroup")] [DictionaryDrawerSettings(KeyLabel = "相同文件名称", ValueLabel = "对应绝对路径列表")] private Dictionary
> sameNameResultGroup = new Dictionary
>(); public bool CheckSameMD5ResultGroup() { return sameMD5Result5Group.Count > 0; } private bool CheckSameNameResultGroup() { return sameNameResultGroup.Count > 0; } [PropertySpace(10,20)] [ShowIf("@ IsToggled== false")] [Button("开始搜索", ButtonSizes.Large)] public void StartSearch() { if (string.IsNullOrEmpty(targetSearchFolder)) { targetSearchFolder = Application.dataPath; } ResetData(); DirectoryInfo directoryInfo = new DirectoryInfo(targetSearchFolder); var filesGroup = directoryInfo.EnumerateFiles("*", SearchOption.AllDirectories).Where(x => x.Extension != ".meta"); maxCount = filesGroup.Count(); fileInfoIEnumerator = filesGroup.GetEnumerator(); IsToggled = true; EditorApplication.update += Updte; } private void ResetData() { maxCount = 0; MaxCount = 0; sameMD5Group.Clear(); sameNameGroup.Clear(); sameMD5Result5Group.Clear(); sameNameResultGroup.Clear(); fileInfoIEnumerator = null; } ///
/// 过滤掉没有重复文件的数据 /// private void FilterDictionary() { sameMD5Result5Group = sameMD5Group.Where(x => x.Value.Count > 1).ToDictionary(p=>p.Key, p => p.Value); sameNameResultGroup = sameNameGroup.Where(x => x.Value.Count > 1).ToDictionary(p => p.Key, p => p.Value); } [ReadOnly] [ProgressBar(0, "maxCount", DrawValueLabel = true, ValueLabelAlignment = TextAlignment.Left, ColorMember = "GetHealthBarColor", Height = 30)] [ShowInInspector] [HideLabel] [ShowIf("@ IsToggled== true")] public int MaxCount { get; set; }//绘制进度条 private Color GetHealthBarColor(int value) { maxCount = maxCount == 0 ? 1 : maxCount; return Color.Lerp(Color.red, Color.green, Mathf.Pow((float)value / maxCount, 2)); } public void Updte() { if (IsToggled) { if (fileInfoIEnumerator.MoveNext()) { //获取对应Hash值 string hashValue = GetMD5HashFromFile(fileInfoIEnumerator.Current.FullName); if (!sameMD5Group.ContainsKey(hashValue)) { sameMD5Group[hashValue] = new List
(); } sameMD5Group[hashValue].Add("名称为:" + fileInfoIEnumerator.Current.Name); //获取名称 string fileName = fileInfoIEnumerator.Current.Name; if (!sameNameGroup.ContainsKey(fileName)) { sameNameGroup[fileName] = new List
(); } sameNameGroup[fileName].Add("路径为:" + fileInfoIEnumerator.Current.FullName); ++MaxCount; } else { EditorApplication.update -= Updte; IsToggled = false; FilterDictionary(); Debug.Log("
注销"); } } } ///
/// 计算文件MD5值 /// ///
///
public string GetMD5HashFromFile(string fileFullName) { try { FileStream file = new FileStream(fileFullName, FileMode.Open); System.Security.Cryptography.MD5 md5 = new System.Security.Cryptography.MD5CryptoServiceProvider(); byte[] retVal = md5.ComputeHash(file); file.Close(); return BitConverter.ToString(retVal).ToLower().Replace("-", ""); } catch { throw; } }}

更多教程内容详见:

转载地址:http://zsjti.baihongyu.com/

你可能感兴趣的文章
【JavaScript 教程】标准库—Date 对象
查看>>
前阿里手淘前端负责人@winter:前端人如何保持竞争力?
查看>>
【JavaScript 教程】面向对象编程——实例对象与 new 命令
查看>>
我在网易做了6年前端,想给求职者4条建议
查看>>
SQL1015N The database is in an inconsistent state. SQLSTATE=55025
查看>>
RQP-DEF-0177
查看>>
MySQL字段类型的选择与MySQL的查询效率
查看>>
Java的Properties配置文件用法【续】
查看>>
JAVA操作properties文件的代码实例
查看>>
IPS开发手记【一】
查看>>
Java通用字符处理类
查看>>
文件上传时生成“日期+随机数”式文件名前缀的Java代码
查看>>
Java代码检查工具Checkstyle常见输出结果
查看>>
北京十大情人分手圣地
查看>>
Android自动关机代码
查看>>
Android中启动其他Activity并返回结果
查看>>
2009年33所高校被暂停或被限制招生
查看>>
GlassFish 部署及应用入门
查看>>
X-code7 beta error: warning: Is a directory
查看>>
Error: An App ID with identifier "*****" is not avaliable. Please enter a different string.
查看>>