如果要做游戏的热更新(其实不光是Unity,其他游戏也需要),就需要此项功能。
服务器资源列表更新后,客户端联网后需要检验服务器端的资源情况。如果文件名相同但MD5值发生了改变,表明该文件被更新,将其加入待更新的列表。如果有不同的文件名,表明是新的资源,像新的地图什么的,也需要加入列表的。检查完毕后从服务器下载资源,然后替换本地文件或者加入新的文件。当然,我这里也许说的很不全面,希望大侠们帮我纠正,小弟感激不尽!
本章简单写了一下文件的MD5值生成方法。欢迎交流!
using System; using System.Collections.Generic; using System.Text; using System.Security.Cryptography; using System.IO; namespace GetMD5 { class Program { public static string getFileHash(string filePath) { try { FileStream fs = new FileStream(filePath, FileMode.Open); int len = (int)fs.Length; byte[] data = new byte[len]; fs.Read(data, 0, len); fs.Close(); MD5 md5 = new MD5CryptoServiceProvider(); byte[] result = md5.ComputeHash(data); string fileMD5 = ""; foreach (byte b in result) { fileMD5 += Convert.ToString(b, 16); } return fileMD5; } catch (FileNotFoundException e) { Console.WriteLine(e.Message); return ""; } } static void Main(string[] args) { string md5 = getFileHash("E:\\Myweb\\cubetest.unity3d"); string b = getFileHash("E:\\Myweb\\testscene.unity3d"); Console.WriteLine(md5); Console.WriteLine(b); Console.ReadKey(); } } }
Unity游戏开发技术杂谈(一):为文件生成MD5值,布布扣,bubuko.com
原文:http://blog.csdn.net/zjc_game_coder/article/details/20309505