AssetBundle系列——遊戲資源打包(一)

將本地資源打包,而後放到資源服務器上供遊戲客戶端下載或更新。服務器上包含如下資源列表:
(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個按鈕每一個對應上述的一步操做:ui

 

 

最終生成的資源目錄結構以下所示:spa

 編輯器代碼以下所示(包括菜單項和窗口):code

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:每一個操做的具體實現,見下一篇講解...orm

相關文章
相關標籤/搜索