(转)AssetBundle系列——游戏资源打包(一)
转自:http://www.cnblogs.com/sifenkesi/p/3557231.html
将本地资源打包,然后放到资源服务器上供游戏客户端下载或更新。服务器上包含以下资源列表:
(1)游戏内容资源assetbundle
(2)资源维护列表,包含每个资源的名字(完整路径名)和对应的版本号[资源名,版本号],如下表所示(VersionNum.xml):
<VersionNum> <File FileName="Assets.Resources.BigLevelTexture.TestLevel.assetbundle" Num="1" /> <File FileName="Assets.Resources.EquipmentTexture.Test001.assetbundle" Num="1" /> <File FileName="Assets.Resources.EquipmentTexture.Test002.assetbundle" Num="1" /> <File FileName="Assets.Resources.EquipmentTexture.Test003.assetbundle" Num="1" /> <File FileName="Assets.Resources.EquipmentTexture.Test004.assetbundle" Num="1" /> <File FileName="Assets.Resources.PetTexture.Empty.assetbundle" Num="1" /> </VersionNum>
那么本地客户端的资源打包编辑器就需要完成以下工作:将资源打包、生成版本号。
我们采用通过MD5码对比的方式来对版本号进行管理,如果某资源的MD5码变更了,则将其版本号+1,否则不变。那么,可以将编辑器的具体任务划分如下:
(1)将资源打包成assetbundle,并放到指定目录下
(2)为每个assetbund生成最新MD5码,用于检查资源是否有修改
(3)比较新旧MD5码列表,产生资源变更列表,对于每个变更的资源,将其版本号+1
(4)将变更列表文件也打包成assetbundle
各个平台使用的资源包时各自独立的,打包资源代码时的选项不一样,编辑器界面如下,图2中的4个按钮每个对应上述的一步操作:
最终生成的资源目录结构如下所示:
编辑器代码如下所示(包括菜单项和窗口):
using UnityEditor; using UnityEngine; using System.IO; using System.Collections; using System.Collections.Generic; public class AssetBundleController : EditorWindow { public static AssetBundleController window; public static UnityEditor.BuildTarget buildTarget = BuildTarget.StandaloneWindows; [MenuItem("XiYouEditor/AssetBundle/AssetBundle For Windows32", false, 1)] public static void ExecuteWindows32() { if (window == null) { window = (AssetBundleController)GetWindow(typeof(AssetBundleController)); } buildTarget = UnityEditor.BuildTarget.StandaloneWindows; window.Show(); } [MenuItem("XiYouEditor/AssetBundle/AssetBundle For IPhone", false, 2)] public static void ExecuteIPhone() { if (window == null) { window = (AssetBundleController)GetWindow(typeof(AssetBundleController)); } buildTarget = UnityEditor.BuildTarget.iPhone; window.Show(); } [MenuItem("XiYouEditor/AssetBundle/AssetBundle For Mac", false, 3)] public static void ExecuteMac() { if (window == null) { window = (AssetBundleController)GetWindow(typeof(AssetBundleController)); } buildTarget = UnityEditor.BuildTarget.StandaloneOSXUniversal; window.Show(); } [MenuItem("XiYouEditor/AssetBundle/AssetBundle For Android", false, 4)] public static void ExecuteAndroid() { if (window == null) { window = (AssetBundleController)GetWindow(typeof(AssetBundleController)); } buildTarget = UnityEditor.BuildTarget.Android; window.Show(); } [MenuItem("XiYouEditor/AssetBundle/AssetBundle For WebPlayer", false, 5)] public static void ExecuteWebPlayer() { if (window == null) { window = (AssetBundleController)GetWindow(typeof(AssetBundleController)); } buildTarget = UnityEditor.BuildTarget.WebPlayer; window.Show(); } void OnGUI() { if (GUI.Button(new Rect(10f, 10f, 200f, 50f), "(1)CreateAssetBundle")) { CreateAssetBundle.Execute(buildTarget); EditorUtility.DisplayDialog("", "Step (1) Completed", "OK"); } if (GUI.Button(new Rect(10f, 80f, 200f, 50f), "(2)Generate MD5")) { CreateMD5List.Execute(buildTarget); EditorUtility.DisplayDialog("", "Step (2) Completed", "OK"); } if (GUI.Button(new Rect(10f, 150f, 200f, 50f), "(3)Compare MD5")) { CampareMD5ToGenerateVersionNum.Execute(buildTarget); EditorUtility.DisplayDialog("", "Step (3) Completed", "OK"); } if (GUI.Button(new Rect(10f, 220f, 200f, 50f), "(4)Build VersionNum.xml")) { CreateAssetBundleForXmlVersion.Execute(buildTarget); EditorUtility.DisplayDialog("", "Step (4) Completed", "OK"); } } public static string GetPlatformPath(UnityEditor.BuildTarget target) { string SavePath = ""; switch (target) { case BuildTarget.StandaloneWindows: SavePath = "Assets/AssetBundle/Windows32/"; break; case BuildTarget.StandaloneWindows64: SavePath = "Assets/AssetBundle/Windows64/"; break; case BuildTarget.iPhone: SavePath = "Assets/AssetBundle/IOS/"; break; case BuildTarget.StandaloneOSXUniversal: SavePath = "Assets/AssetBundle/Mac/"; break; case BuildTarget.Android: SavePath = "Assets/AssetBundle/Android/"; break; case BuildTarget.WebPlayer: SavePath = "Assets/AssetBundle/WebPlayer/"; break; default: SavePath = "Assets/AssetBundle/"; break; } if (Directory.Exists(SavePath) == false) Directory.CreateDirectory(SavePath); return SavePath; } public static string GetPlatformName(UnityEditor.BuildTarget target) { string platform = "Windows32"; switch (target) { case BuildTarget.StandaloneWindows: platform = "Windows32"; break; case BuildTarget.StandaloneWindows64: platform = "Windows64"; break; case BuildTarget.iPhone: platform = "IOS"; break; case BuildTarget.StandaloneOSXUniversal: platform = "Mac"; break; case BuildTarget.Android: platform = "Android"; break; case BuildTarget.WebPlayer: platform = "WebPlayer"; break; default: break; } return platform; } }
PS:每个操作的具体实现,见下一篇讲解...