AssetBundle是Unity引擎提供的—种用于存储资源的文件格式 ,它 可以存储任意一种Unity引擎能够识别的资源 ,例如模型 、纹理 、音频 、动画片段甚至整个场景等 。AssetBundle也可以包含开发者自定义的二进制文件,只需将二进制文件的扩展名改成.bytes,unity引擎即可将其识别为TextAsset,进 而可以被打包到AssetBundle文件中。

AseetBundle 资源打包

如何离线创建AB

<code class="hljs vala has-numbering">BuildPipeline.BuildAssetBundle(UnityEngine.<span class="hljs-built_in">Object</span> mainAsset, UnityEngine.<span class="hljs-built_in">Object</span>[] assets, <span class="hljs-keyword">string</span> pathName, BuildAssetBundleOptions assetBundleOptions, BuildTarget targetPlatform) </code><ul style="" class="pre-numbering"><li>1</li></ul><ul style="" class="pre-numbering"><li>1</li></ul>


•  将Project文件夹中的任意个Assets打包成一个AB
•  一般适用于对单个大规模场景的细分
参数:mainAsset 主资源,assets 打包对象,pathName 文件保存路径, assetBundleOptions 打包类型,targetPlatform 目标平台

<code class="hljs cs has-numbering">  BuildPipeline.BuildStreamedSceneAssetBundle(<span class="hljs-keyword">string</span>[] levels, <span class="hljs-keyword">string</span> locationPath, BuildTarget target) </code><ul style="" class="pre-numbering"><li>1</li></ul><ul style="" class="pre-numbering"><li>1</li></ul>

• 该方法与BuildPipeline.BuildAssetBundle的作用相似,但是多了一个额外的参数可以为每个物体指定一个自定义的标识符
•  将一个或多个场景中的资源及其所有依赖打包成一个AB
•  一般适用于对单个或多个场景进行集中打包
参数:打包场景名称数组 levels,locationPath 保存路径,target 目标平台

打包选项 BuildAssetBundleOptions

CompleteAssets
•  使每个Asset本身完备化

CollectDependencies
•  包入每个Asset依赖的所有资源

DeterministicAssetBundle
•  使每个Object具有唯一的、不变的Hash ID,便于后续查找
•  可用于增量式发布Asset Bundle

UncompressedAssetBundle
•  不进行数据压缩

依赖关系处理

假设模型A需要使用材质materia1和材质materia2,模型B需使用材质materia1和materia3,materia1需要使用贴图texture1,materia2需要使用贴图texture2,materia3需要使用贴图texture3,模型C需要使用模型A。
我们在进行打包的时候需要将被依赖的对象优先进行打包,因此打包顺序为先为贴图texture1、texture2、texture3进行打包,然后对材质materia1、materia2、materia3进行打包,在对模型A、模型B进行打包,最后对模型C进行打包。
打包建立依赖需要使用的方法:
•  BuildPipeline.PushAssetDependencies()
•  BuildPipeline.PopAssetDependencies()
以上两个方法必须成对使用,一个push对应一个pop
打包过程简化为
PushAssetDependencies。。
打包texture1、texture2、texture3
PushAssetDependencies。。
打包materia1、materia2、materia3
PushAssetDependencies。。
打包模型A、模型B
PushAssetDependencies。。
打包模型C
PopAssetDependencies。。
PopAssetDependencies。。
PopAssetDependencies。。
PopAssetDependencies。。

参考示例
对选择的一个文件夹内的对象进行打包

<code class="hljs cs has-numbering"><span class="hljs-keyword">static</span> <span class="hljs-keyword">void</span> floderPack()
    {
        <span class="hljs-keyword">string</span> fileName = <span class="hljs-string">"resource1"</span>;<span class="hljs-comment">//打包文件名</span>
        <span class="hljs-keyword">if</span> (Selection.activeObject != <span class="hljs-keyword">null</span>)
        {<span class="hljs-comment">//如果选择内容不为空,则打包文件名为该文件名</span>
            fileName = Selection.activeObject.name;
        }
        <span class="hljs-comment">//设置打包文件保存文件夹的位置</span>
        <span class="hljs-keyword">string</span> targetDir = Application.streamingAssetsPath + <span class="hljs-string">"/MyResource"</span>;
        <span class="hljs-keyword">if</span> (!Directory.Exists(targetDir))
        {
            targetDir = <span class="hljs-string">""</span>;
        }
        <span class="hljs-comment">//打开文件打包路径选择面板,选择文件打包路径</span>
        <span class="hljs-keyword">string</span> savePath = EditorUtility.SaveFilePanel(<span class="hljs-string">"打包文件"</span>, targetDir, fileName, ExtensionType.unity3D);
        <span class="hljs-comment">//设置打包方式为保存依赖的所有文件</span>
        BuildAssetBundleOptions ABBuilder = BuildAssetBundleOptions.CollectDependencies | BuildAssetBundleOptions.CompleteAssets | BuildAssetBundleOptions.DeterministicAssetBundle;


        Object[] objs = Selection.GetFiltered(<span class="hljs-keyword">typeof</span>(Object), SelectionMode.DeepAssets);
        <span class="hljs-keyword">foreach</span> (<span class="hljs-keyword">var</span> item <span class="hljs-keyword">in</span> objs)
        {
            <span class="hljs-keyword">string</span> objPath = AssetDatabase.GetAssetPath(item);
            Debug.Log(<span class="hljs-string">"objpath="</span> + objPath);
            <span class="hljs-keyword">if</span> (item <span class="hljs-keyword">is</span> Texture2D)
            {
                AssetDatabase.ImportAsset(objPath);
            }
        }
        <span class="hljs-keyword">bool</span> isBuildOK = BuildPipeline.BuildAssetBundle(Selection.activeGameObject, objs, savePath, ABBuilder, BuildTarget.StandaloneWindows);
        <span class="hljs-keyword">if</span> (isBuildOK)
        {
            Debug.Log(<span class="hljs-string">"build ok"</span>);
        }
        <span class="hljs-keyword">else</span>
        {
            Debug.Log(<span class="hljs-string">"build error"</span>);
        }


    }</code>


对一个文件夹内的被选中的单独文件进行打包

<code class="hljs avrasm has-numbering">[MenuItem(<span class="hljs-string">"ABpackage/SingleFileAB"</span>)]
    static void SinglePack()
    {
        string fileName = <span class="hljs-string">"resource1"</span><span class="hljs-comment">;//打包文件名</span>
        if (Selection<span class="hljs-preprocessor">.activeObject</span> != null)
        {//如果选择内容不为空,则打包文件名为该文件名
            fileName = Selection<span class="hljs-preprocessor">.activeObject</span><span class="hljs-preprocessor">.name</span><span class="hljs-comment">;</span>
        }
        //设置打包文件保存文件夹的位置
        string targetDir = Application<span class="hljs-preprocessor">.streamingAssetsPath</span> + <span class="hljs-string">"/MyResource"</span><span class="hljs-comment">;</span>
        if (!Directory<span class="hljs-preprocessor">.Exists</span>(targetDir))
        {
            targetDir = <span class="hljs-string">""</span><span class="hljs-comment">;</span>
        }
        //打开文件打包路径选择面板,选择文件打包路径
        string savePath = EditorUtility<span class="hljs-preprocessor">.SaveFilePanel</span>(<span class="hljs-string">"打包文件"</span>, targetDir, fileName, ExtensionType<span class="hljs-preprocessor">.unity</span>3D)<span class="hljs-comment">;</span>
        //设置打包方式为保存依赖的所有文件
        BuildAssetBundleOptions ABBuilder = BuildAssetBundleOptions<span class="hljs-preprocessor">.CollectDependencies</span> | BuildAssetBundleOptions<span class="hljs-preprocessor">.CompleteAssets</span> | BuildAssetBundleOptions<span class="hljs-preprocessor">.DeterministicAssetBundle</span><span class="hljs-comment">;</span>

        Object obj = Selection<span class="hljs-preprocessor">.activeObject</span><span class="hljs-comment">;</span>

        string objPath = AssetDatabase<span class="hljs-preprocessor">.GetAssetPath</span>(obj)<span class="hljs-comment">;</span>
        Debug<span class="hljs-preprocessor">.Log</span>(<span class="hljs-string">"objpath="</span> + objPath)<span class="hljs-comment">;</span>
        Object[] objs = { obj }<span class="hljs-comment">;</span>

        bool isBuildOK = BuildPipeline<span class="hljs-preprocessor">.BuildAssetBundle</span>(Selection<span class="hljs-preprocessor">.activeGameObject</span>, objs, savePath, ABBuilder, BuildTarget<span class="hljs-preprocessor">.StandaloneWindows</span>)<span class="hljs-comment">;</span>
        if (isBuildOK)
        {
            Debug<span class="hljs-preprocessor">.Log</span>(<span class="hljs-string">"build ok"</span>)<span class="hljs-comment">;</span>
        }
        else
        {
            Debug<span class="hljs-preprocessor">.Log</span>(<span class="hljs-string">"build error"</span>)<span class="hljs-comment">;</span>
        }


    }</code>


对一个文件夹内的场景文件进行打包

<code class="hljs cs has-numbering">[MenuItem(<span class="hljs-string">"ABpackage/ScenesAB _Android"</span>)]
    <span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">void</span> <span class="hljs-title">ABScenes</span>()
    {
        <span class="hljs-keyword">string</span> assetPath = EditorUtility.SaveFilePanel(<span class="hljs-string">"save scene"</span>, <span class="hljs-string">""</span>, <span class="hljs-string">"myScene"</span>, <span class="hljs-string">"scene.assetbundle"</span>);
        <span class="hljs-comment">//string currentScene = EditorApplication.currentScene;</span>
        Object[] objs = Selection.GetFiltered(<span class="hljs-keyword">typeof</span>(Object), SelectionMode.DeepAssets);
        List<<span class="hljs-keyword">string</span>> list = <span class="hljs-keyword">new</span> List<<span class="hljs-keyword">string</span>>();
        <span class="hljs-keyword">foreach</span> (<span class="hljs-keyword">var</span> item <span class="hljs-keyword">in</span> objs)
        {
            <span class="hljs-keyword">string</span> path = AssetDatabase.GetAssetPath(item);
            <span class="hljs-keyword">int</span> mFindIndex = path.LastIndexOf(<span class="hljs-string">"."</span>);
            <span class="hljs-keyword">string</span> mExtName = path.Substring(mFindIndex + <span class="hljs-number">1</span>);<span class="hljs-comment">//获取文件后缀名</span>
            Debug.Log(<span class="hljs-string">"mExtName:"</span> + mExtName);
            <span class="hljs-keyword">if</span> (mExtName == <span class="hljs-string">"unity"</span>)<span class="hljs-comment">//保存场景文件</span>
            {
                list.Add(path);
            }
        }
        Debug.Log(<span class="hljs-string">"list.Count="</span> + list.Count);
        BuildPipeline.BuildStreamedSceneAssetBundle(list.ToArray(), assetPath, BuildTarget.Android);

    }</code>


通过依赖关系进行打包

<code class="hljs javascript has-numbering">[MenuItem(<span class="hljs-string">"ABpackage/depend"</span>)]
    public static <span class="hljs-keyword">void</span> test1()
    {
        <span class="hljs-comment">//依赖关系:objB和objC依赖于objA</span>
        <span class="hljs-built_in">Object</span> objA = AssetDatabase.LoadAssetAtPath(<span class="hljs-string">"Assets/Texture/village_04.png"</span>, <span class="hljs-keyword">typeof</span>(<span class="hljs-built_in">Object</span>));
        <span class="hljs-built_in">Object</span> objB = AssetDatabase.LoadAssetAtPath(<span class="hljs-string">"Assets/Model/Cube2.prefab"</span>, <span class="hljs-keyword">typeof</span>(<span class="hljs-built_in">Object</span>));
        <span class="hljs-built_in">Object</span> objC = AssetDatabase.LoadAssetAtPath(<span class="hljs-string">"Assets/Model/Sphere.prefab"</span>, <span class="hljs-keyword">typeof</span>(<span class="hljs-built_in">Object</span>));

        BuildAssetBundleOptions option = BuildAssetBundleOptions.CollectDependencies | BuildAssetBundleOptions.CompleteAssets | BuildAssetBundleOptions.DeterministicAssetBundle;
        <span class="hljs-comment">//PushAssetDependencies需要和PopAssetDependencies成对出现</span>
        <span class="hljs-comment">//被依赖对象需要先进行打包</span>
        <span class="hljs-built_in">Object</span>[] obj1 = { objA };
        <span class="hljs-built_in">Object</span>[] obj2 = { objB };
        <span class="hljs-built_in">Object</span>[] obj3 = { objC };
        BuildPipeline.PushAssetDependencies();
        BuildPipeline.BuildAssetBundle(<span class="hljs-literal">null</span>, obj1, @<span class="hljs-string">"D:\U3Dworkplace\Login_client\AB\texture.assetbundle"</span>, option, BuildTarget.Android);
        BuildPipeline.PushAssetDependencies();
        BuildPipeline.BuildAssetBundle(<span class="hljs-literal">null</span>, obj2, @<span class="hljs-string">"D:\U3Dworkplace\Login_client\AB\objB.assetbundle"</span>, option, BuildTarget.Android);
        BuildPipeline.BuildAssetBundle(<span class="hljs-literal">null</span>, obj3, @<span class="hljs-string">"D:\U3Dworkplace\Login_client\AB\objC.assetbundle"</span>, option, BuildTarget.Android);

        BuildPipeline.PopAssetDependencies();
        BuildPipeline.PopAssetDependencies();

    }</code>