將本地資源打包,然后放到資源服務器上供游戲客戶端下載或更新。服務器上包含以下資源列表:
(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:每個操作的具體實現,見下一篇講解...