Unity5.X AssetBundle创建MD5文件
来源:互联网 发布:seo中h1是什么意思 编辑:程序博客网 时间:2024/06/06 03:56
using UnityEngine;
using UnityEditor;
using System.IO;
using System.Xml;
using System.Collections;
using System.Collections.Generic;
using System.Security.Cryptography;
public class CreatMD5 : MonoBehaviour {
public static void CreatMD5XML()
{
Dictionary<string, List<string>> DicFileMD5 = new Dictionary<string, List<string>>();
MD5CryptoServiceProvider md5Generator = new MD5CryptoServiceProvider();
//需要创建MD5文件的文件夹路径
string dir = System.IO.Path.Combine(Application.dataPath, "StreamingAssets");
//MD5保存路径
string savePath = System.IO.Path.Combine(Application.dataPath, "StreamingAssets");
foreach (string filePath in Directory.GetFiles(dir))
{
if (filePath.Contains(".meta") || filePath.Contains("VersionMD5") || filePath.Contains(".xml"))
continue;
FileStream file = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.Read);
byte[] hash = md5Generator.ComputeHash(file);
List<string> tempList = new List<string>();
string strMD5 = System.BitConverter.ToString(hash);
string size = file.Length.ToString();
tempList.Add(strMD5);
tempList.Add(size);
file.Close();
string key = filePath.Substring(dir.Length + 1, filePath.Length - dir.Length - 1);
if (DicFileMD5.ContainsKey(key) == false)
DicFileMD5.Add(key, tempList);
else
Debug.LogWarning("<Two File has the same name> name = " + filePath);
}
if (Directory.Exists(savePath) == false)
Directory.CreateDirectory(savePath);
XmlDocument XmlDoc = new XmlDocument();
XmlElement XmlRoot = XmlDoc.CreateElement("Files");
XmlDoc.AppendChild(XmlRoot);
foreach (KeyValuePair<string, List<string>> pair in DicFileMD5)
{
XmlElement xmlElem = XmlDoc.CreateElement("File");
XmlRoot.AppendChild(xmlElem);
xmlElem.SetAttribute("FileName", pair.Key);
xmlElem.SetAttribute("MD5", pair.Value[0]);
xmlElem.SetAttribute("Size", pair.Value[1]);
}
XmlDoc.Save(savePath + "/VersionMD5.xml");
XmlDoc = null;
AssetDatabase.Refresh();
}
static Dictionary<string, List<string>> ReadMD5File(string fileName)
{
Dictionary<string, List<string>> DicMD5 = new Dictionary<string, List<string>>();
// 如果文件不存在,则直接返回
if (System.IO.File.Exists(fileName) == false)
return DicMD5;
XmlDocument XmlDoc = new XmlDocument();
XmlDoc.Load(fileName);
XmlElement XmlRoot = XmlDoc.DocumentElement;
foreach (XmlNode node in XmlRoot.ChildNodes)
{
if ((node is XmlElement) == false)
continue;
List<string> tempList = new List<string>();
string file = (node as XmlElement).GetAttribute("FileName");
string md5 = (node as XmlElement).GetAttribute("MD5");
string size = (node as XmlElement).GetAttribute("Size");
tempList.Add(md5);
tempList.Add(size);
if (DicMD5.ContainsKey(file) == false)
{
DicMD5.Add(file, tempList);
}
}
XmlRoot = null;
XmlDoc = null;
return DicMD5;
}
}
using UnityEditor;
using System.IO;
using System.Xml;
using System.Collections;
using System.Collections.Generic;
using System.Security.Cryptography;
public class CreatMD5 : MonoBehaviour {
public static void CreatMD5XML()
{
Dictionary<string, List<string>> DicFileMD5 = new Dictionary<string, List<string>>();
MD5CryptoServiceProvider md5Generator = new MD5CryptoServiceProvider();
//需要创建MD5文件的文件夹路径
string dir = System.IO.Path.Combine(Application.dataPath, "StreamingAssets");
//MD5保存路径
string savePath = System.IO.Path.Combine(Application.dataPath, "StreamingAssets");
foreach (string filePath in Directory.GetFiles(dir))
{
if (filePath.Contains(".meta") || filePath.Contains("VersionMD5") || filePath.Contains(".xml"))
continue;
FileStream file = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.Read);
byte[] hash = md5Generator.ComputeHash(file);
List<string> tempList = new List<string>();
string strMD5 = System.BitConverter.ToString(hash);
string size = file.Length.ToString();
tempList.Add(strMD5);
tempList.Add(size);
file.Close();
string key = filePath.Substring(dir.Length + 1, filePath.Length - dir.Length - 1);
if (DicFileMD5.ContainsKey(key) == false)
DicFileMD5.Add(key, tempList);
else
Debug.LogWarning("<Two File has the same name> name = " + filePath);
}
if (Directory.Exists(savePath) == false)
Directory.CreateDirectory(savePath);
XmlDocument XmlDoc = new XmlDocument();
XmlElement XmlRoot = XmlDoc.CreateElement("Files");
XmlDoc.AppendChild(XmlRoot);
foreach (KeyValuePair<string, List<string>> pair in DicFileMD5)
{
XmlElement xmlElem = XmlDoc.CreateElement("File");
XmlRoot.AppendChild(xmlElem);
xmlElem.SetAttribute("FileName", pair.Key);
xmlElem.SetAttribute("MD5", pair.Value[0]);
xmlElem.SetAttribute("Size", pair.Value[1]);
}
XmlDoc.Save(savePath + "/VersionMD5.xml");
XmlDoc = null;
AssetDatabase.Refresh();
}
static Dictionary<string, List<string>> ReadMD5File(string fileName)
{
Dictionary<string, List<string>> DicMD5 = new Dictionary<string, List<string>>();
// 如果文件不存在,则直接返回
if (System.IO.File.Exists(fileName) == false)
return DicMD5;
XmlDocument XmlDoc = new XmlDocument();
XmlDoc.Load(fileName);
XmlElement XmlRoot = XmlDoc.DocumentElement;
foreach (XmlNode node in XmlRoot.ChildNodes)
{
if ((node is XmlElement) == false)
continue;
List<string> tempList = new List<string>();
string file = (node as XmlElement).GetAttribute("FileName");
string md5 = (node as XmlElement).GetAttribute("MD5");
string size = (node as XmlElement).GetAttribute("Size");
tempList.Add(md5);
tempList.Add(size);
if (DicMD5.ContainsKey(file) == false)
{
DicMD5.Add(file, tempList);
}
}
XmlRoot = null;
XmlDoc = null;
return DicMD5;
}
}
阅读全文
0 0
- Unity5.X AssetBundle创建MD5文件
- unity5.X AssetBundle
- Unity5.X AssetBundle打包
- Unity5.X AssetBundle加载
- [Unity5.X]AssetBundle总结
- Unity5.x 编辑器中 获取所有标记的 AssetBundle 文件
- unity5.X AssetBundle使用(一 )
- unity5.X AssetBundle使用(二 )
- Unity5.x AssetBundle 的变化
- Unity5.x AssetBundle 的变化
- Unity5.X批清理AssetBundle的名称
- Unity5.X打包与加载AssetBundle
- Unity5.x shader打包AssetBundle总结
- Unity5.x shader打包AssetBundle总结
- unity5.x assetbundle打包和加载
- unity5.X AssetBundle资源打包(一 )
- Unity5.x AssetBundle依赖项打包详解
- Unity5.X AssetBundle检测资源更新
- sublime工具的安装方法简介
- fixed自适应居中
- application/x-www-form-urlencoded
- html块级元素和行内元素
- class 注解
- Unity5.X AssetBundle创建MD5文件
- 详解Struts2拦截器机制
- Node.js项目学习笔记
- windows程序设计——视窗和讯息
- select count (1)
- Python实现常用排序算法总结
- 小程序----应用(3)
- 渲染类
- dispatch_once造成的死锁----分析、解决与自动检测