Unity中编辑器拓展功能——文件校验

本文介绍了一种在Unity环境中实现文件校验与比较的方法,通过递归遍历文件夹并利用SHA1算法计算文件哈希值来比较两个文件夹内容是否一致,适用于游戏资源管理和版本控制。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

文件校验本质是通过比较两个文件的哈希值。这里边由于是比较两个文件夹下的内容是否相同,可能会出现很多个子文件夹的情况。所以采用了递归的方式去进行文件校验。

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
using System;
using System.IO;
using System.Security.Cryptography;

public class MenuItemDemo 
{
    public static MenuItemDemo _instance;
    public static MenuItemDemo Instance {
        get { 
             if (_instance == null)
             {
                 _instance = new MenuItemDemo();
                 return _instance;
             }
            else {
                 return _instance;
             }
        }
    }

    private const string otherPath = @"C:\\Users\\Admin\\Desktop\\项目\\华为音乐\\UnityProject\\AIO SDK Library\\PICO\\VRSDK_pico";
    private const string path = @"C:\\Users\\Admin\\Desktop\\项目\\华为音乐\\UnityProject\\HuaweiVRMusic\\Assets\\AIO SDK\\VRSDK_pico";
    private int index = path.Length;
    [MenuItem("Tools/FileAndCompare")]
    static void CESHI()
    {
       Instance.GetFileAndCompare(path);
    }
    string GetHash(string path)
    {
        var hash = SHA1.Create();
        var stream = new FileStream(path, FileMode.Open);
        byte[] hashByte = hash.ComputeHash(stream);
        stream.Close();
        return BitConverter.ToString(hashByte).Replace("-", "");
    }
    public void GetFileAndCompare(string path)
    {
        string[] f = System.IO.Directory.GetFileSystemEntries(path);
        if (f.Length == 0)
        {
            return;
        }

        foreach (var fs in f)
        {
            if (Directory.Exists(fs))
            {
                //当前路径是文件夹,继续递归调用自身
                GetFileAndCompare(fs);
            }
            else
            {
                if (File.Exists(fs))
                {
                        //当前路径是文件,此时需要跟外部文件夹文件进行对比
                        string t_otherpath = fs.Substring(index, fs.Length - index);
                        if (File.Exists(otherPath + t_otherpath))
                        {//文件存在,比较两个文件是否相同
                            var isSame = GetHash(fs).Equals(GetHash(otherPath + t_otherpath));
                            if (!isSame)
                            {
                                Debug.LogWarning("文件" + fs + "存在差异!");
                            }
                            else
                            {
                                Debug.Log(fs + "文件相同");
                            }
                        }
                        else
                        { //文件不存在,输出当前文件不存在的信息
                            Debug.LogError("文件:" + fs + "不存在!");
                        }
                }
            }
        }
    }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值