Unity3d 5.x新版的AssetBoundle打包

最近项目升级,需要用到unity的新的api来打包AssetBoundle,结果发现网上有很多教程,我发现有很多系列在介绍新Api的时候没有说明白,我在次记录一下这半天的收获吧,API接口链接如下
https://docs.unity3d.com/ScriptReference/BuildPipeline.BuildAssetBundles.html
里边有两个小case

代码如下:

示例一

// Create an AssetBundle for Windows.
using UnityEngine;
using UnityEditor;

public class BuildAssetBundlesExample : MonoBehaviour
{
    [MenuItem("Example/Build Asset Bundles")]
    static void BuildABs()
    {
        // Put the bundles in a folder called "ABs" within the Assets folder.
        BuildPipeline.BuildAssetBundles("Assets/ABs", BuildAssetBundleOptions.None, BuildTarget.StandaloneWindows);
    }
}

示例二

using UnityEngine;
using UnityEditor;

public class BuildAssetBundlesBuildMapExample : MonoBehaviour
{
    [MenuItem("Example/Build Asset Bundles Using BuildMap")]
    static void BuildMapABs()
    {
        // Create the array of bundle build details.
        AssetBundleBuild[] buildMap = new AssetBundleBuild[2];

        buildMap[0].assetBundleName = "enemybundle";

        string[] enemyAssets = new string[2];
        enemyAssets[0] = "Assets/Textures/char_enemy_alienShip.jpg";
        enemyAssets[1] = "Assets/Textures/char_enemy_alienShip-damaged.jpg";

        buildMap[0].assetNames = enemyAssets;
        buildMap[1].assetBundleName = "herobundle";

        string[] heroAssets = new string[1];
        heroAssets[0] = "char_hero_beanMan";
        buildMap[1].assetNames = heroAssets;

        BuildPipeline.BuildAssetBundles("Assets/ABs", buildMap, BuildAssetBundleOptions.None, BuildTarget.StandaloneWindows);
    }
}

第一种是可视化操作

这里写图片描述
给这张毛爷爷加一个名字 masks,后边的那个是变体名,也就是做了一个后缀,第一次打包assetboundle的时候不要设置变体名,导出的时候会报错,这个变体名就是给boundle多了一个后缀,作用有可能是区分boundle类型或者是boundle的版本的
然后打包的代码就是示例一。
代码如下:

 [MenuItem("Assets/打包")]
    public static void PackageTextureAssetBundle()
    {
    BuildPipeline.BuildAssetBundles(Application.streamingAssetsPath + "/PC/", BuildAssetBundleOptions.None, BuildTarget.StandaloneWindows);
    }

结果如下:
这里写图片描述

这种方式很便捷,但是在资源量比较大的时候,更改名字特别麻烦
unity提供了一种代码批量大包
就是第二种方式不在ui里设置boundle名,直接通过代码设置
代码如下:

private static void CreateBoundle()
    {
        AssetBundleBuild[] buildMap = new AssetBundleBuild[2];

        buildMap[0].assetBundleName = "masks";

        string[] enemyAssets = new string[3];
        enemyAssets[0] = "Assets/Pics/rmb.jpg";
        enemyAssets[1] = "Assets/Pics/timg(4).jpg";
        enemyAssets[2] = "Assets/Pics/timg(2).jpg";

        buildMap[0].assetNames = enemyAssets;

        buildMap[1].assetBundleName = "actormasks";

        string[] enemyAssets1 = new string[2];
        enemyAssets1[0] = "masks";  //依赖项  这个依赖项可以省略的,系统自动打包依赖项
        enemyAssets1[1] = "Assets/Pics/test2.mat";这个材质用到了timg(2).jpg图片,所以加了一个依赖项

        buildMap[1].assetNames = enemyAssets1;

        BuildPipeline.BuildAssetBundles(Application.streamingAssetsPath + "/PC/", buildMap, BuildAssetBundleOptions.None, BuildTarget.StandaloneWindows);
        AssetDatabase.Refresh();
    }

结果如下:
这里写图片描述

PC.manifest文件内容如下:

ManifestFileVersion: 0
CRC: 296088231
AssetBundleManifest:
AssetBundleInfos:
Info_0:
Name: masks
Dependencies: {}
Info_1:
Name: actormasks
Dependencies:
Dependency_0: masks

masks.manifest文件内容如下:

ManifestFileVersion: 0
CRC: 3035632970
Hashes:
AssetFileHash:
serializedVersion: 2
Hash: 89bc1c87876ab0c3fac15da102217585
TypeTreeHash:
serializedVersion: 2
Hash: 36c33a819640bead83b84b1645a12e6d
HashAppended: 0
ClassTypes:
- Class: 28
Script: {instanceID: 0}
- Class: 213
Script: {instanceID: 0}
Assets:
- Assets/Pics/timg(4).jpg
- Assets/Pics/rmb.jpg
- Assets/Pics/timg(2).jpg
Dependencies: []

actormasks.manfest文件内容如下:
ManifestFileVersion: 0
CRC: 3963451652
Hashes:
AssetFileHash:
serializedVersion: 2
Hash: 54e3cb225502efafec0efba951c65d8c
TypeTreeHash:
serializedVersion: 2
Hash: d1ae6e26fb0b4d11fccd18da598542fa
HashAppended: 0
ClassTypes:
- Class: 21
Script: {instanceID: 0}
- Class: 28
Script: {instanceID: 0}
- Class: 48
Script: {instanceID: 0}
Assets:
- Assets/Pics/test2.mat
Dependencies:
- D:/MyUnity/New Unity Project 19/Assets/StreamingAssets/PC/masks 刚才代码里提到的依赖项

这篇文章大概介绍了一下,unity新版本的boundle打包的用法,到具体项目,按实际需求来用就可以了。

demo完整代码如下:

using UnityEditor;
using UnityEngine;

public class AssetBoundleExport : EditorWindow
{

    [MenuItem("Window/PackAsset")]
    static void Init()
    {
        EditorWindow.GetWindow(typeof(AssetBoundleExport));
    }

    void OnGUI()
    {
        if (GUILayout.Button("方式一", GUILayout.Width(100)))
        {
            CreateBoundle1();
        }
        if (GUILayout.Button("方式二", GUILayout.Width(100)))
        {
            CreateBoundle2();
        }
    }

    private static void CreateBoundle2()
    {
        AssetBundleBuild[] buildMap = new AssetBundleBuild[2];

        buildMap[0].assetBundleName = "masks";

        string[] enemyAssets = new string[3];
        enemyAssets[0] = "Assets/Pics/rmb.jpg";
        enemyAssets[1] = "Assets/Pics/timg(4).jpg";
        enemyAssets[2] = "Assets/Pics/timg(2).jpg";

        buildMap[0].assetNames = enemyAssets;

        buildMap[1].assetBundleName = "actormasks";

        string[] enemyAssets1 = new string[2];
        enemyAssets1[0] = "masks";
        enemyAssets1[1] = "Assets/Pics/test2.mat";

        buildMap[1].assetNames = enemyAssets1;

        BuildPipeline.BuildAssetBundles(Application.streamingAssetsPath + "/PC/", buildMap, BuildAssetBundleOptions.None, BuildTarget.StandaloneWindows);
        AssetDatabase.Refresh();
    }

    private static void CreateBoundle1()
    {
        BuildPipeline.BuildAssetBundles(Application.streamingAssetsPath + "/PC/", BuildAssetBundleOptions.None, BuildTarget.StandaloneWindows);
    }
}

参考文章:https://blog.csdn.net/onafioo/article/details/78872493
https://blog.csdn.net/ring0hx/article/details/46376709

猜你喜欢

转载自blog.csdn.net/u011017980/article/details/79883405