转自:http://www.cnblogs.com/sifenkesi/p/3557290.html

本篇接着上一篇。上篇中说到的4步的代码分别如下所示:

(1)将资源打包成assetbundle,并放到自定目录下

using UnityEditor;
using UnityEngine;
using System.IO;
using System.Collections;
using System.Collections.Generic;public class CreateAssetBundle
{public static void Execute(UnityEditor.BuildTarget target){string SavePath = AssetBundleController.GetPlatformPath(target);// 当前选中的资源列表foreach (Object o in Selection.GetFiltered(typeof(Object), SelectionMode.DeepAssets)){string path = AssetDatabase.GetAssetPath(o);// 过滤掉meta文件和文件夹if(path.Contains(".meta") || path.Contains(".") == false)continue;// 过滤掉UIAtlas目录下的贴图和材质(UI/Common目录下的所有资源都是UIAtlas)if (path.Contains("UI/Common")){if ((o is Texture) || (o is Material))continue;}path = SavePath + ConvertToAssetBundleName(path);path = path.Substring(0, path.LastIndexOf('.'));path += ".assetbundle";BuildPipeline.BuildAssetBundle(o, null, path, BuildAssetBundleOptions.CollectDependencies | BuildAssetBundleOptions.CompleteAssets | BuildAssetBundleOptions.DeterministicAssetBundle, target);}// scene目录下的资源
AssetDatabase.Refresh();}static string ConvertToAssetBundleName(string ResName){return ResName.Replace('/', '.');}}

(2)为每个assetbund生成MD5码,用于检查资源是否有修改

using UnityEngine;
using UnityEditor;
using System.IO;
using System.Xml;
using System.Collections;
using System.Collections.Generic;
using System.Security.Cryptography;public class CreateMD5List
{public static void Execute(UnityEditor.BuildTarget target){string platform = AssetBundleController.GetPlatformName(target);Execute(platform);AssetDatabase.Refresh();}public static void Execute(string platform){Dictionary<string, string> DicFileMD5 = new Dictionary<string, string>();MD5CryptoServiceProvider md5Generator = new MD5CryptoServiceProvider();string dir = System.IO.Path.Combine(Application.dataPath, "AssetBundle/" + platform);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);string strMD5 = System.BitConverter.ToString(hash);file.Close();string key = filePath.Substring(dir.Length + 1, filePath.Length - dir.Length - 1);if (DicFileMD5.ContainsKey(key) == false)DicFileMD5.Add(key, strMD5);elseDebug.LogWarning("<Two File has the same name> name = " + filePath);}string savePath = System.IO.Path.Combine(Application.dataPath, "AssetBundle/") + platform + "/VersionNum";if (Directory.Exists(savePath) == false)Directory.CreateDirectory(savePath);// 删除前一版的old数据if (File.Exists(savePath + "/VersionMD5-old.xml")){System.IO.File.Delete(savePath + "/VersionMD5-old.xml");}// 如果之前的版本存在,则将其名字改为VersionMD5-old.xmlif (File.Exists(savePath + "/VersionMD5.xml")){System.IO.File.Move(savePath + "/VersionMD5.xml", savePath + "/VersionMD5-old.xml");}XmlDocument XmlDoc = new XmlDocument();XmlElement XmlRoot = XmlDoc.CreateElement("Files");XmlDoc.AppendChild(XmlRoot);foreach (KeyValuePair<string, string> pair in DicFileMD5){XmlElement xmlElem = XmlDoc.CreateElement("File");XmlRoot.AppendChild(xmlElem);xmlElem.SetAttribute("FileName", pair.Key);xmlElem.SetAttribute("MD5", pair.Value);}// 读取旧版本的MD5Dictionary<string, string> dicOldMD5 = ReadMD5File(savePath + "/VersionMD5-old.xml");// VersionMD5-old中有,而VersionMD5中没有的信息,手动添加到VersionMD5foreach (KeyValuePair<string, string> pair in dicOldMD5){if (DicFileMD5.ContainsKey(pair.Key) == false)DicFileMD5.Add(pair.Key, pair.Value);}XmlDoc.Save(savePath + "/VersionMD5.xml");XmlDoc = null;}static Dictionary<string, string> ReadMD5File(string fileName){Dictionary<string, string> DicMD5 = new Dictionary<string, 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;string file = (node as XmlElement).GetAttribute("FileName");string md5 = (node as XmlElement).GetAttribute("MD5");if (DicMD5.ContainsKey(file) == false){DicMD5.Add(file, md5);}}XmlRoot = null;XmlDoc = null;return DicMD5;}}

MD5列表如下所示:

<Files><File FileName="Assets.Resources.BigLevelTexture.TestLevel.assetbundle" MD5="54-00-42-38-D5-86-43-A6-57-9D-7C-09-3A-F8-6E-10" /><File FileName="Assets.Resources.EquipmentTexture.Test001.assetbundle" MD5="A1-19-D4-04-17-94-18-61-60-99-35-25-3F-7C-39-93" /><File FileName="Assets.Resources.EquipmentTexture.Test002.assetbundle" MD5="CF-36-DA-C8-D2-DB-CE-FD-4A-BF-31-81-A1-D1-D2-21" /><File FileName="Assets.Resources.EquipmentTexture.Test003.assetbundle" MD5="EF-30-78-AE-F8-F4-A0-EC-5B-4E-45-3F-1E-EF-42-44" /><File FileName="Assets.Resources.EquipmentTexture.Test004.assetbundle" MD5="3D-5D-A7-01-D2-B1-20-5F-B9-89-C5-CB-40-96-EC-89" /><File FileName="Assets.Resources.PetTexture.Empty.assetbundle" MD5="D9-AC-54-F8-EB-AA-1C-36-8C-2B-6C-12-37-AB-3B-48" />
</Files>

(3)比较新旧MD5码,生成资源变更列表

using UnityEngine;
using UnityEditor;
using System.IO;
using System.Xml;
using System.Collections;
using System.Collections.Generic;public class CampareMD5ToGenerateVersionNum
{public static void Execute(UnityEditor.BuildTarget target){string platform = AssetBundleController.GetPlatformName(target);Execute(platform);AssetDatabase.Refresh();}// 对比对应版本目录下的VersionMD5和VersionMD5-old,得到最新的版本号文件VersionNum.xmlpublic static void Execute(string platform){// 读取新旧MD5列表string newVersionMD5 = System.IO.Path.Combine(Application.dataPath, "AssetBundle/" + platform + "/VersionNum/VersionMD5.xml");string oldVersionMD5 = System.IO.Path.Combine(Application.dataPath, "AssetBundle/" + platform + "/VersionNum/VersionMD5-old.xml");Dictionary<string, string> dicNewMD5Info = ReadMD5File(newVersionMD5);Dictionary<string, string> dicOldMD5Info = ReadMD5File(oldVersionMD5);// 读取版本号记录文件VersinNum.xmlstring oldVersionNum = System.IO.Path.Combine(Application.dataPath, "AssetBundle/" + platform + "/VersionNum/VersionNum.xml");Dictionary<string, int> dicVersionNumInfo = ReadVersionNumFile(oldVersionNum);// 对比新旧MD5信息,并更新版本号,即对比dicNewMD5Info&&dicOldMD5Info来更新dicVersionNumInfoforeach (KeyValuePair<string, string> newPair in dicNewMD5Info){// 旧版本中有if (dicOldMD5Info.ContainsKey(newPair.Key)){// MD5一样,则不变// MD5不一样,则+1// 容错:如果新旧MD5都有,但是还没有版本号记录的,则直接添加新纪录,并且将版本号设为1if (dicVersionNumInfo.ContainsKey(newPair.Key) == false){dicVersionNumInfo.Add(newPair.Key, 1);}else if (newPair.Value != dicOldMD5Info[newPair.Key]){int num = dicVersionNumInfo[newPair.Key];dicVersionNumInfo[newPair.Key] = num + 1;}}else // 旧版本中没有,则添加新纪录,并=1
            {dicVersionNumInfo.Add(newPair.Key, 1);}}// 不可能出现旧版本中有,而新版本中没有的情况,原因见生成MD5List的处理逻辑// 存储最新的VersionNum.xml
        SaveVersionNumFile(dicVersionNumInfo, oldVersionNum);}static Dictionary<string, string> ReadMD5File(string fileName){Dictionary<string, string> DicMD5 = new Dictionary<string, 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;string file = (node as XmlElement).GetAttribute("FileName");string md5 = (node as XmlElement).GetAttribute("MD5");if (DicMD5.ContainsKey(file) == false){DicMD5.Add(file, md5);}}XmlRoot = null;XmlDoc = null;return DicMD5;}static Dictionary<string, int> ReadVersionNumFile(string fileName){Dictionary<string, int> DicVersionNum = new Dictionary<string, int>();// 如果文件不存在,则直接返回if (System.IO.File.Exists(fileName) == false)return DicVersionNum;XmlDocument XmlDoc = new XmlDocument();XmlDoc.Load(fileName);XmlElement XmlRoot = XmlDoc.DocumentElement;foreach (XmlNode node in XmlRoot.ChildNodes){if ((node is XmlElement) == false)continue;string file = (node as XmlElement).GetAttribute("FileName");int num = XmlConvert.ToInt32((node as XmlElement).GetAttribute("Num"));if (DicVersionNum.ContainsKey(file) == false){DicVersionNum.Add(file, num);}}XmlRoot = null;XmlDoc = null;return DicVersionNum;}static void SaveVersionNumFile(Dictionary<string, int> data, string savePath){XmlDocument XmlDoc = new XmlDocument();XmlElement XmlRoot = XmlDoc.CreateElement("VersionNum");XmlDoc.AppendChild(XmlRoot);foreach (KeyValuePair<string, int> pair in data){XmlElement xmlElem = XmlDoc.CreateElement("File");XmlRoot.AppendChild(xmlElem);xmlElem.SetAttribute("FileName", pair.Key);xmlElem.SetAttribute("Num", XmlConvert.ToString(pair.Value));}XmlDoc.Save(savePath);XmlRoot = null;XmlDoc = null;}}

如下图所示,根据VersionMD5.xml和VersionMD5-old.xml对比产生VersionNum.xml:

(4)将变更列表文件也打包成assetbundle

也就是讲VersionNum.xml打包后供下载:

using UnityEditor;
using UnityEngine;
using System.IO;
using System.Collections;
using System.Collections.Generic;public class CreateAssetBundleForXmlVersion
{public static void Execute(UnityEditor.BuildTarget target){string SavePath = AssetBundleController.GetPlatformPath(target);Object obj = AssetDatabase.LoadAssetAtPath(SavePath + "VersionNum/VersionNum.xml", typeof(Object));BuildPipeline.BuildAssetBundle(obj, null, SavePath + "VersionNum/VersionNum.assetbundle", BuildAssetBundleOptions.CollectDependencies | BuildAssetBundleOptions.CompleteAssets | BuildAssetBundleOptions.DeterministicAssetBundle, target);AssetDatabase.Refresh();}static string ConvertToAssetBundleName(string ResName){return ResName.Replace('/', '.');}}

(转)AssetBundle系列——游戏资源打包(二)相关推荐

  1. AssetBundle系列——共享资源打包/依赖资源打包

    有人在之前的博客中问我有关共享资源打包的代码,其实这一块很简单,就两个函数: BuildPipeline.PushAssetDependencies():依赖资源压栈: BuildPipeline.P ...

  2. (转)AssetBundle系列——共享资源打包/依赖资源打包

    有人在之前的博客中问我有关共享资源打包的代码,其实这一块很简单,就两个函数: BuildPipeline.PushAssetDependencies():依赖资源压栈: BuildPipeline.P ...

  3. 6.pixi.js编写的塔防游戏(类似保卫萝卜)-游戏资源打包逻辑

    游戏说明 一个用pixi.js编写的h5塔防游戏,可以用electron打包为exe,支持移动端,也可以用webview控件打包为app在移动端使用 环境说明 cnpm@6.2.0 npm@6.14. ...

  4. gta 6 android,【图片】GTA安卓新版CLEO+全系列游戏资源+FLA6.0【gta安卓吧】_百度贴吧...

    该楼层疑似违规已被系统折叠 隐藏此楼查看此楼 [八楼安卓CLEO开发人员说明] 这是开发人员应该看的,什么是开发人员? CLEO脚本编写者.C++插件编写者. 新版安卓CLEO为新的CLEO脚本提供了 ...

  5. pkg文件--一种简单的游戏资源打包格式

    .pkg文件的格式 [四字节] 固定的内容, 值不重要 [四字节] 文件数目(unsigned int) [四字节] 文件名表 的偏移(unsigned int) [四字节] 文件名表 的长度(字节数 ...

  6. CoD系列游戏资源提取工具

    http://www.diegologic.net/diegologic/ 不解释.

  7. C3游戏引擎资源打包格式支持(APK不释放资源的问题)

    http://www.9miao.com/thread-16828-1-1.html C3游戏引擎支持wdf和tpd两种资源打包格式. Wdf是一种C3游戏资源打包格式,不做数据压缩和加密,适合png ...

  8. 通用型游戏资源提取工具介绍

    先感慨一下,这是篇2007年的帖子啊!!13年了! 游戏资源包括了游戏的图片.文字.音乐.动画和其他数据资源.虽然很多游戏的资源都是开放的或者采用通用格式压缩的,但也不少游戏是经特殊格式打包过了,要想 ...

  9. 通用型游戏资源提取工具介绍收藏

    游戏资源包括了游戏的图片.文字.音乐.动画和其他数据资源.虽然很多游戏的资源都是开放的或者采用通用格式压缩的,但也不少游戏是经特殊格式打包过了,要想得到这些资源可以寻找专用的资源提取工具.但并非所有游 ...

  10. 【转贴】通用型游戏资源提取工具介绍 (推荐)

    游戏资源包括了游戏的图片.文字.音乐.动画和其他数据资源.虽然很多游戏的资源都是开放的或者采用通用格式压缩的,但也不少游戏是经特殊格式打包过了,要想得到这些资源可以寻找专用的资源提取工具.但并非所有游 ...

最新文章

  1. 建立注册DLL和反注册DLL文件的快捷方式
  2. HDU 2189 悼念512汶川大地震遇难同胞——来生一起走
  3. java虚拟机改装_java虚拟机线上配置
  4. 《深入实践Spring Boot》一第一部分 Part 1基础应用开发
  5. html实现全选 反选,jquery实现全选、不选、反选的两种方法
  6. 新浪微博开发-添加子视图控制器设置颜色
  7. switch off c语言,逆向工程 | C 语言之 switch-case 分支
  8. Exam 70-462 Administering Microsoft SQL Server 2012 Databases 复习帖
  9. SpringCloud 进阶之Eureka(服务注册和发现)
  10. QTTabBar 简单配置
  11. 【黑马Python】(3)
  12. 学会二次创作后,网易云批量生产“好”音乐
  13. csps2019格雷码
  14. 未能联接game center服务器,game center连接不成功怎么办 有哪些修复步骤 - 驱动管家...
  15. Layer 2:公链本就不该追求性能 |链捕手
  16. 计算机开机的四个画面,教你修改电脑开机时“欢迎使用”四个字!
  17. 格物致知 c语言字节数、对齐、补齐的小探索
  18. 中缀向后缀转换表达式
  19. android手机内存越来越小,安卓手机因软件安装失败 导致手机内存越来越小解决方法...
  20. summernote图片上传

热门文章

  1. 初识window phone 7程序
  2. jmeter性能工具 之 cookie 管理器
  3. Excel 4.0宏躲避杀软检测(转)
  4. python学习第六天运算符总结大全
  5. Linux:写一个简单的服务器
  6. SVN提交文件冲突怎么办?
  7. 逆向project第003篇:跨越CM4验证机制的鸿沟(上)
  8. PHP SPL 迭代器
  9. MQTT协议(1)-简介
  10. Mysql 基础知识