Unity資源熱更之AssetBundle(3)———新版本AssetBundle

————尊重原創,轉載請註明出處,謝謝!!!!

http://blog.csdn.net/y1196645376/article/details/52602002


http://www.xuanyusong.com/archives/2405  雨凇MOMO講AssetBuddle的博客


之前我們介紹了老版本的AssetBundle,在文章最後我們提到了在生成AB包的時候同時生成依賴關係信息文件。這樣加載的時候就能夠自動加載所有依賴項。而新版的AssetBundle正是採用的這樣的方式。下面我來介紹一下:



1.Unity5.x的AssetBundle的新功能。

  • 爲每個資源添加了AssetBundleName屬性,我們可以通過Editor中的資源可視化視圖下方爲資源設置AssetBundleName。 
    這裏寫圖片描述

          上圖第一個箭頭所指既是AssetBundleName,名字固定爲小寫,另外,每個AssetBundle都可以設置一個Variant,其實就是一個後綴,實際上AssetBundle會添加這個後綴。如果有不同分辨率的同名資源,可以使用這個來區分。
    值得注意的是:
          AssetBundleName是可以帶’/’符號的,這是一個很好的設計,因爲我們打包的資源會很多,如果打包生成的所有AssetBundle都生成在同一個文件目錄裏,這肯定是很難管理的。不過名稱引入’/’便可以很好解決這個問題。我們通過名字中設置類似”npc/demon/jushiguai.unity3d”這樣的名字。那麼在生成AssetBundle的時候會自動根據名字生成文件目錄。這樣生成的AssetBundle有了分類就很好管理了。如下圖:

        這裏寫圖片描述

  • 統一了打包AssetBundle接口:BuildPipeline.BuildAssetBundles(outputPath),outputPath爲AB包生成文件夾路徑。

  • BuildAssetBundles打包默認開啓了:CompleteAssets ,CollectDependencies,DeterministicAssetBundle。

  • 打包模式新增:

    ForceRebuildAssetBundle             : 強制重新打包所有AssetBundle文件,一般情況只做增量打包。
    IgnoreTypeTreeChanges              : 用於判斷AssetBundle更新時,是否忽略TypeTree的變化。
    AppendHashToAssetBundleName : 將Hash值添加在AB包文件名之後,開啓後可通過文件名來判斷哪些AB進行了更新。


  • 打包生成manifest文件,包含CRC,Hash,ID,AssetPath以及Dependencies等AssetBundle信息。新版的AssetBundle打包會自動幫你處理依賴關係。

  • 加載AssetBundle的API被替換,如下:

    4.x版本中的AssetBundle.CreateFromFile方法,在5.x版本中變成了AssetBundle.LoadFromFile方法。
    4.x版本中的AssetBundle.CreateFromMemory方法,在5.x版本中變成了LoadFromMemoryAsync方法。
    4.x版本中的AssetBundle.CreateFromMemoryImmediate方法,在5.x版本中變成了LoadFromMemory方法。


2.實戰演練

  • 在這裏我準備了一個實例工程,在Prefab文件夾放了一些素材。 
    這裏寫圖片描述

    簡單介紹下,Cube1,Cube2是上篇文章使用的兩個預製體。JuShiGuai是一個怪物模型的Prefab,使用了Mat1,Mat2兩個材質,而Mat1,Mat2兩個材質分別使用了JuShiGuai_01,JuShiGuai_02兩個貼圖。 

  • 接下來我分別給這幾個Prefab和Material,Texture設置AssetBundle。

    Mat1             :  materials/mat1.unity3d 
    Mat2             :  materials/mat2.unity3d 
    JuShiGuai_01 :  texture/jushiguai_01.unity3d 
    JuShiGuai_02 :  texture/jushiguai_02.unity3d 
    Cube1           :  cube1.unity3d 
    Cube2           :  cube2.unity3d 
    JuShiGuai      :  jushiguai.unity3d 

  • 首先編寫路徑常量類:

using UnityEngine;
using System.Collections;

public class AssetBundleConfig : MonoBehaviour {

    //AssetBundle打包路徑
    public static string ASSETBUNDLE_PATH = Application.dataPath + "/StreamingAssets/AssetBundle/";

    //資源地址
    public static string APPLICATION_PATH = Application.dataPath + "/";

    //工程地址
    public static string PROJECT_PATH = APPLICATION_PATH.Substring(0, APPLICATION_PATH.Length - 7);

    //AssetBundle存放的文件夾名
    public static string ASSETBUNDLE_FILENAM = "AssetBundle";

    //AssetBundle打包的後綴名
    public static string SUFFIX = ".unity3d";

}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 然後編寫打包代碼:
public class NewAssetBundleEditor : Editor {



    [MenuItem ("New AB Editor/Build AssetBundles")]
    static void BuildAllAssetBundles ()
    {
        //第一個參數獲取的是AssetBundle存放的相對地址。
        BuildPipeline.BuildAssetBundles(
         AssetBundleConfig.ASSETBUNDLE_PATH.Substring(AssetBundleConfig.PROJECT_PATH.Length), 
         BuildAssetBundleOptions.UncompressedAssetBundle |
         BuildAssetBundleOptions.CollectDependencies |
         BuildAssetBundleOptions.DeterministicAssetBundle,
         BuildTarget.StandaloneWindows64);
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 在Unity菜單欄出現了New AB Editor,點擊 Build AssetBundles。那麼在StreamingAssets/AssetBundle路徑下就會生成所有的AB包。

    這裏寫圖片描述

    每個AssetBundle包都配有一個manifest文件記錄該包的相關信息,並且在AssetBundle生成根目錄下會有一個AssetBundle文件(文件名爲你存放AB包的文件名,我這裏是AssetBundle)。和AssetBundle.manifest記錄的是整個工程的AB包的相關信息。 

  • 生成包完了之後就是解析加載AB包了。不過值得注意的是雖然Unity說會幫我們處理好依賴關係,但是隻是說能夠方便獲取一個asset的依賴項,並不會幫你自動加載所有依賴項。所以我們加載的代碼還是三個部分:加載依賴項和加載自身,卸載依賴項。

  • 對於上面的方案,這裏我提出一個相對更實用的一個方案。我們知道整個資源中,可能有的資源會被很多其他資源依賴,比如Shader,材質等。如果每次加載好資源項後,又去卸載。對於某種資源依賴次數很多的情況,這種方案就會比較耗時。所以我們可以把加載好的資源用字典存着,下次如果還需要加載這個依賴項就可以直接從字典裏面讀取。代碼如下:

using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class NewAssetBundleLoad : MonoBehaviour {

    private static AssetBundleManifest manifest = null;

    private static Dictionary<string, AssetBundle> abDic = new Dictionary<string, AssetBundle>();

    public static AssetBundle LoadAB(string abPath)
    {
        if (abDic.ContainsKey(abPath) == true)
            return abDic[abPath];
        if (manifest == null)
        {
            AssetBundle manifestBundle = AssetBundle.CreateFromFile(AssetBundleConfig.ASSETBUNDLE_PATH +
            AssetBundleConfig.ASSETBUNDLE_FILENAM);
            manifest = (AssetBundleManifest)manifestBundle.LoadAsset("AssetBundleManifest");
        }
        if (manifest != null)
        {
            // 2.獲取依賴文件列表  
            string[] cubedepends = manifest.GetAllDependencies(abPath);

            for (int index = 0; index < cubedepends.Length; index++)
            {
                //Debug.Log(cubedepends[index]);
                // 3.加載所有的依賴資源
                LoadAB(cubedepends[index]);
            }

            // 4.加載資源
             abDic[abPath] = AssetBundle.CreateFromFile(AssetBundleConfig.ASSETBUNDLE_PATH + abPath);

            return abDic[abPath];
        }
        return null;
    }

    public static Object LoadGameObject(string abName)
    {
        string abPath = abName + AssetBundleConfig.SUFFIX;
        int index = abName.LastIndexOf('/');
        if (index == -1) index = abName.Length;
        string realName = abName.Substring(index + 1, abName.Length - index - 1);

        LoadAB(abPath);

        if (abDic.ContainsKey(abPath) && abDic[abPath] != null)
        {
            return abDic[abPath].LoadAsset(realName);
        }
        return null;
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 不過值得注意的是以上加載方法是同步的,也就是說如果資源太大可能會導致阻塞。可以考慮用協程或者多線程解決。

3.批量命名

  • 新版的AssetBundle雖然提出了AssetBundleName這樣一個新的方法。但是在實際工程中,如果對於每個資源都手動添加設置Name。一來會十分麻煩,二來容易出錯,三來不方便管理。所以在實際項目中,我們需要一個方法對於一些資源進行批量命名。

  • 這裏我給出了一個方式。我們把資源的Path作爲資源的AssetBundleName,這樣在AssetBundle中的文件分類也是和資源文件夾的分類一樣的,方便管理。當然,你也可以不按照這個規定來,只要適合項目實際情況就好了。

using UnityEngine;
using UnityEditor;
using System.IO;
public class NewAssetBundleEditor : Editor {


    [MenuItem("New AB Editor/SetAssetBundleName")]
    static void SetResourcesAssetBundleName()
    {
        UnityEngine.Object[] SelectedAsset = Selection.GetFiltered(typeof(Object), 
                            SelectionMode.Assets | SelectionMode.ExcludePrefab );
        //此處添加需要命名的資源後綴名,注意大小寫。
        string[] Filtersuffix = new string[] { ".prefab",".mat",".dds" };   

        if (!(SelectedAsset.Length == 1)) return;

        string fullPath = AssetBundleConfig.PROJECT_PATH + 
        AssetDatabase.GetAssetPath(SelectedAsset[0]);

        if (Directory.Exists(fullPath))
        {
            DirectoryInfo dir = new DirectoryInfo(fullPath);

            var files = dir.GetFiles("*", SearchOption.AllDirectories);
            ;
            for (var i = 0; i < files.Length; ++i)
            {
                var fileInfo = files[i];
                    "設置AssetName名稱", "正在設置AssetName名稱中...", 
                    1f * i / files.Length);
                foreach (string suffix in Filtersuffix)
                {
                    if (fileInfo.Name.EndsWith(suffix))
                    {
                        string path = fileInfo.FullName.Replace('\\', 
                        '/').Substring(AssetBundleConfig.PROJECT_PATH.Length);
                        var importer = AssetImporter.GetAtPath(path);
                        if (importer)
                        {
                            string name = path.Substring(fullPath.Substring(
                            AssetBundleConfig.PROJECT_PATH.Length).Length + 1);
                            importer.assetBundleName = name.Substring(0,
                            name.LastIndexOf('.')) + AssetBundleConfig.SUFFIX;
                        }
                    }
                }
            }
            AssetDatabase.RemoveUnusedAssetBundleNames();
        }
        EditorUtility.ClearProgressBar();
    }

    [MenuItem("New AB Editor/GetAllAssetBundleName")]
    static void GetAllAssetBundleName()
    {

        string[] names = AssetDatabase.GetAllAssetBundleNames();

        foreach (var name in names)
        {
            Debug.Log(name);
        }

    }

    [MenuItem("New AB Editor/ClearAssetBundleName")]
    static void ClearResourcesAssetBundleName()
    {
        UnityEngine.Object[] SelectedAsset = Selection.GetFiltered(typeof(Object), 
                                SelectionMode.Assets | SelectionMode.ExcludePrefab);
        //此處添加需要命名的資源後綴名,注意大小寫。
        string[] Filtersuffix = new string[] { ".prefab", ".mat", ".dds" };   

        if (!(SelectedAsset.Length == 1)) return;

        string fullPath = AssetBundleConfig.PROJECT_PATH + AssetDatabase.GetAssetPath(SelectedAsset[0]);

        if (Directory.Exists(fullPath))
        {
            DirectoryInfo dir = new DirectoryInfo(fullPath);

            var files = dir.GetFiles("*", SearchOption.AllDirectories);
            ;
            for (var i = 0; i < files.Length; ++i)
            {
                var fileInfo = files[i];
                EditorUtility.DisplayProgressBar("清除AssetName名稱", 
                "正在清除AssetName名稱中...", 1f * i / files.Length);
                foreach (string suffix in Filtersuffix)
                {
                    if (fileInfo.Name.EndsWith(suffix))
                    {
                        string path = fileInfo.FullName.Replace('\\', 
                        '/').Substring(AssetBundleConfig.PROJECT_PATH.Length);
                        var importer = AssetImporter.GetAtPath(path);
                        if (importer)
                        {
                            importer.assetBundleName = null;
                        }
                    }
                }
            }
        }
        EditorUtility.ClearProgressBar();
        AssetDatabase.RemoveUnusedAssetBundleNames();
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 具體使用方法:

    選中資源視圖中的某個文件夾,然後點擊New AB Editor,選中對應的選項:

    SetAssetBundleName    : 自動設置該文件夾所有資源的AssetBunldName。 
    GetAssetBundleName   : 控制檯輸出所有設置過AssetBundleName的資源的Name。 
    ClearAssetBundleName : 清除該文件夾所有資源的AssetBunldName。 

基本上新版的AssetBundle使用方法就介紹到這裏了。如果你有疑惑或者以上文章有什麼錯誤還望你在下面評論區指出。本人致以萬分感謝!

最後附上以上Demo源碼地址:點這裏

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章