写了一个简单的例子,传入指定文件夹,会给该文件夹下的文件生成对应的MD5,然后将信息转换成Json存储到本地;

注意,文件夹下如果有子文件夹(及多层目录),没有做处理;

using System.Collections;
using System.Collections.Generic;
using System.Security.Cryptography;
using UnityEngine;
using UnityEngine.Networking;
using System.IO;
using UnityEngine.UI;
using System.Text;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;//数据结构
[System.Serializable]
public struct Md5Info
{public string name;public string md5;public Md5Info(string n,string m){name = n;md5 = m;}
}[System.Serializable]
public class MD5InfoData
{public List<Md5Info> md5Info = new List<Md5Info>();public static void SaveData(string path, MD5InfoData data){IFormatter formatter = new BinaryFormatter();Stream stream = new FileStream(path, FileMode.Create, FileAccess.Write, FileShare.None);formatter.Serialize(stream, data);stream.Close();string cfgJson = JsonUtility.ToJson(data, true);File.WriteAllText(path, cfgJson);}public static MD5InfoData GetMD5Data(string path){string text = File.ReadAllText(path);MD5InfoData info = JsonUtility.FromJson<MD5InfoData>(text);return info;}
}public class MD5Test : MonoBehaviour
{public void CreateResFileMd5(string path){MD5InfoData fmd5 = new MD5InfoData();DirectoryInfo root = new DirectoryInfo(path);FileInfo[] files = root.GetFiles("*", SearchOption.AllDirectories);foreach (var f in files){//排除.meta文件if (f.Name.EndsWith(".meta")){continue;}//Debug.Log(f.DirectoryName);string md5 = GetMd5HashFromFile(f.DirectoryName + "/" + f.Name);//Debug.Log(f.Name + "的Md5 = " + md5);Md5Info info = new Md5Info(f.Name, md5);fmd5.md5Info.Add(info);}//存储Json到本地MD5InfoData.SaveData(Application.streamingAssetsPath + "/md5.json", fmd5);}string GetMd5HashFromFile(string filePath){FileStream fs = new FileStream(filePath, FileMode.Open);MD5 md5 = new MD5CryptoServiceProvider();byte[] hash = md5.ComputeHash(fs);fs.Close();StringBuilder sb = new StringBuilder();for (int i = 0; i < hash.Length; i++){sb.Append(hash[i].ToString("x2"));}return sb.ToString().ToUpper();}
}

最后的json文件长这样:==>

新增简单下载测试:

using System.Collections;
using System.Collections.Generic;
using System.Security.Cryptography;
using UnityEngine;
using UnityEngine.Networking;
using System.IO;
using UnityEngine.UI;
using System.Text;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;public class DownloadTest : MonoBehaviour
{public Text txt_process;string[] downloadURL = { "http://www.test_1.com:81/IISTest/res/1.txt", "http://www.test_1.com:81/IISTest/res/1.jpg" };List<string> updateFileName = new List<string>();// Start is called before the first frame updatevoid Start(){txt_process.text = "下载";}public void BtnEvent_Download(){StartCoroutine(ReqRes());}public void BtnEvent_CheckMD5(){//获取服务端MD5的json数据StartCoroutine(CheckMD5());}public void CreateResFileMd5(string path)//Application.dataPath + "/TestRes";{MD5InfoData fmd5 = new MD5InfoData();DirectoryInfo root = new DirectoryInfo(path);FileInfo[] files = root.GetFiles("*", SearchOption.AllDirectories);foreach (var f in files){//排除.meta文件if (f.Name.EndsWith(".meta")){continue;}//Debug.Log(f.DirectoryName);string md5 = GetMd5HashFromFile(f.DirectoryName + "/" + f.Name);//Debug.Log(f.Name + "的Md5 = " + md5);Md5Info info = new Md5Info(f.Name, md5);fmd5.md5Info.Add(info);}//存储Json到本地MD5InfoData.SaveData(Application.streamingAssetsPath + "/md5.json", fmd5);}IEnumerator CheckMD5(){string url = "http://www.test_1.com:81/IISTest/res/md5.json";var req = UnityWebRequest.Get(url);yield return req.SendWebRequest();var text = req.downloadHandler.text;MD5InfoData info = JsonUtility.FromJson<MD5InfoData>(text);var serverList = info.md5Info;//与本地MD5数据做对比,找到变化的文件,取文件名string localPath = Application.streamingAssetsPath + "/md5.json";MD5InfoData localMD5Data = MD5InfoData.GetMD5Data(localPath);var localList = localMD5Data.md5Info;List<string> changeNames = new List<string>();//对比,有变化的或新增的,都将name字段的value放入changeNamesforeach (var data in serverList){bool newFile = true;foreach (var item in localList){if (string.Equals(data.name, item.name)){newFile = false;if (!string.Equals(data.md5, item.md5)){changeNames.Add(data.name);}}}//新增文件if (newFile){changeNames.Add(data.name);}}updateFileName = changeNames;}IEnumerator ReqRes(){FileStream file = null;foreach (var url in downloadURL){var req = UnityWebRequest.Get(url);yield return req.SendWebRequest();if (req.isHttpError || req.isNetworkError){Debug.Log("error");}else{while (!req.isDone){txt_process.text = "下载中..." + req.downloadProgress + "%";}var data = req.downloadHandler.data;string fileName = Path.GetFileName(url);file = new FileStream(Application.streamingAssetsPath + "/" + fileName, FileMode.Create);file.Write(data, 0, data.Length);file.Close();}}txt_process.text = "下载完成";yield return new WaitForSeconds(1);//UnityEditor.AssetDatabase.Refresh();}string GetMd5HashFromFile(string filePath){FileStream fs = new FileStream(filePath, FileMode.Open);MD5 md5 = new MD5CryptoServiceProvider();byte[] hash = md5.ComputeHash(fs);fs.Close();StringBuilder sb = new StringBuilder();for (int i = 0; i < hash.Length; i++){sb.Append(hash[i].ToString("x2"));}return sb.ToString().ToUpper();}}//数据结构
[System.Serializable]
public struct Md5Info
{public string name;public string md5;public Md5Info(string n,string m){name = n;md5 = m;}
}[System.Serializable]
public class MD5InfoData
{public List<Md5Info> md5Info = new List<Md5Info>();public static void SaveData(string path, MD5InfoData data){IFormatter formatter = new BinaryFormatter();Stream stream = new FileStream(path, FileMode.Create, FileAccess.Write, FileShare.None);formatter.Serialize(stream, data);stream.Close();string cfgJson = JsonUtility.ToJson(data, true);File.WriteAllText(path, cfgJson);}public static MD5InfoData GetMD5Data(string path){string text = File.ReadAllText(path);MD5InfoData info = JsonUtility.FromJson<MD5InfoData>(text);return info;}
}

Unity资源文件创建对应的MD5相关推荐

  1. ​Unity资源Assetmport New Asset对话框

    ​Unity资源Assetmport New Asset对话框 1.2.2  资源 开发游戏一定会使用很多东西,如网格.纹理.电影.动画.声音.音乐.文本等等.这些文件都被Unity称为资源(Asse ...

  2. unity 存档插件_【Unity消息】5月1日到5月15日 Unity资源商店大促

    5月1日到5月15日,Unity资源商店5月大促,几百款资源5折,而且每天有一款资源打3折~ 而且Unity资源商店又改版啦,新版好好看呀~ Unity资源5月大促地址:Unity Asset Sto ...

  3. Unity资源处理机制(Assets/WWW/AssetBundle/...)读取和加载资源方式详解

    Unity资源机制 1.概述 本文意在阐述Unity资源机制相关的信息,以及一些关于个人的理解与试验结果.另外还会提及一些因机制问题可能会出现的异常以及处理建议.大部分机制信息来源于官方文档,另外为自 ...

  4. Unity资源导入自动化设置

    Unity资源导入自动化设置 简介 具体实现 新的问题 解决方法 简介 大家都知道,在Unity中导入的资源不同类型有不同的设置, 例如:模型文件导入之后是这样的 当导入数量少的时候我们可以手动去改, ...

  5. Unity资源加载发布到移动端iphone/ipad

    Unity资源加载发布到iOS平台的特殊路径 using UnityEngine; using System.Collections; public class TestLoad : MonoBeha ...

  6. Unity资源下载材质贴图消失

    一.问题 在网上下载的Unity资源模型导入Unity后变成白色,材质丢失. 二.解决办法 1.在Unity Assets中选择下载的模型. 2.在Inspector面板点击模型的materials, ...

  7. 【Unity资源】(粒子系统)

    请支持正版,该系列文章资源均为免费,用作分流,查找资料.请勿侵犯他人的版权. 如果该文章侵犯到您的权益,请及时主动留言联系,我们将及时删除相关内容. 如果您也想为UNITY免费资源的分流出力,请及时留 ...

  8. Unity资源加载入门

    写在前面 本文转载自:https://gameinstitute.qq.com/community/detail/123460,供自己学习用,如有疑问,请移步原创. 引言 Unity的资源加载及管理, ...

  9. Unity资源加载管理

    转载链接: https://bbs.gameres.com/thread_800362_1_1.html 我理解的资源管理 举一个不恰当的例子来描述我所理解的资源管理(因为我实在想不出更合适的例子了) ...

最新文章

  1. Web 开发学习笔记(1) --- 搭建你的第一个 Web Server
  2. java集合框架图(二)
  3. 【转载】C++操作符
  4. SAP CRM text Transfer mode
  5. “附近的小程序”可以直接找“餐饮” 非管理员也能登录小程序了
  6. 这8种保证线程安全的技术你都知道吗?
  7. js验证固定电话、手机号码(代码大全)
  8. 3 photolemur 样式_macOS下支持RAW格式的照片编辑工具
  9. gulp之gulp-uglify模块
  10. python qt刷新_Python Qt.SizeFDiagCursor方法代码示例
  11. 拓端tecdat|Python多项式Logistic逻辑回归进行多类别分类和交叉验证准确度箱线图可视化
  12. linux 下 ffmpeg 库怎么才可以调试
  13. 【音乐理论】音与音高 ( 音 | 乐音体系 | 音列 | 基本音级 | 音名和唱名 )
  14. SECS/GEM协议开发系列(四)SECS/GEM基础知识
  15. Python爬取某短视频热点
  16. ie浏览器点击打印没反应_解决在IE菜单中点击打印无反应
  17. win2003服务器安全设置技术实例(一)
  18. 《拥抱》---梦中好友s:103
  19. 记事本改字体的代码java_求java记事本代码(带字体设置功能)?
  20. 吉林大学邮箱smtp服务器,吉珠专属EDU邮箱上线,校友也可申请!除了发邮件,这个邮箱还能省钱!...

热门文章

  1. 顶刊TPAMI 2022!清华刘玉身团队提出SPD:雪花反卷积网络
  2. java微信红包开发_java写的伪微信红包功能示例代码
  3. 树中的叶子结点的个数 计算方法
  4. 根据列名提取指定列 shell awk
  5. 华强北耳机值得买吗?质量怎么样?靠谱吗?深度拆解悦虎二代1562m耳机!
  6. Azure 深入浅出[3]: 如何在MS Visio里面画专业的Azure技术架构图?
  7. v-distpicker 插件只要省市
  8. 载药脂质体并表面修饰各种分子定制
  9. 港澳四晚五日游(购物团)游记
  10. 2022双11/双十一天猫喵果总动员/京东穿行寻宝一键完成,自动任务脚本软件,分享源码学习