实力封装:Unity打包AssetBundle(二)

→前情提要:Unity最基本的AssetBundle打包方式。

第二种打包方式

Unity提供的BuildAssetBundles API还有一个重载形式,看下面↓↓

public static AssetBundleManifest BuildAssetBundles(string outputPath, AssetBundleBuild[] builds, BuildAssetBundleOptions assetBundleOptions, BuildTarget targetPlatform);
 

这个重载函数多了一个参数,这个参数是一个AssetBundleBuild数组,下面我们来说说AssetBundleBuild是何方妖孽↓↓

AssetBundleBuild是一个结构体,有四个属性,说明如下↓↓

assetBundleName string类型,打包后AssetBundle包的名字
assetNames string数组类型,AssetBundle包中每一个文件相对于工程目录的路径(一个AssetBundle包中可以有多个文件)
addressableNames string数组类型,相当于是assetNames的别名,必须和assetNames长度一致
assetBundleVariant string类型,设置AssetBundle变体,其实就是后缀名
 
 

一般情况下,只要设置AssetBundleBuild结构体中的assetBundleName属性和assetNames属性就可以了,如果你愿意给打在AssetBundle包中的文件重命名的话,就设置一下addressableNames属性,不想重命名的文件就给一个空字符串,这样默认就会使用assetNames了。要是还想设置自定义的后缀名,可以试试AssetBundleVariant。

有了AssetBundleBuild这样一个结构体数组做参数,这就意味着我们可以让打包的过程更简单了。不用在Inspector面板中给每一个需要打包的文件设置AssetBundle名称,我们可以直接在编辑器中鼠标点选需要打包的文件,然后一键打包。当然,这也就意味着我们不能随意的控制每个AssetBundle包的名字了,有利有弊吧。

在这里讲一讲打包的方式吧,我们可以把许多文件打进一个AssetBundle包,也可以将每个文件单独打一个AssetBundle包,具体怎么打就要看需求了。

下面是打包的代码↓↓

  1.  
    [MenuItem( "AssetBundles/Buil (All)")]
  2.  
    private static void _packageBuddlesInOne() {
  3.  
    string _packagePath = UnityEditor.EditorUtility.OpenFolderPanel ("Select Package Path", "F:/", "");
  4.  
    if (_packagePath.Length <= 0 || !Directory.Exists (_packagePath))
  5.  
    return;
  6.  
    buildMap[ 0].assetBundleName = "myBnndle";
  7.  
    //将选中对象一起打包
  8.  
    AssertBundleAssetBundleBuild[] buildMap = new AssetBundleBuild[1];
  9.  
    GameObject[] objs = Selection.gameObjects; //获取当前选中的所有对象
  10.  
    string[] itemAssets = new string[objs.Length];
  11.  
    for (int i = 0; i < objs.Length; i++) {
  12.  
    itemAssets [i] = AssetDatabase.GetAssetPath (objs [i]); //获取对象在工程目录下的相对路径
  13.  
    }
  14.  
    buildMap [ 0].assetNames = itemAssets;
  15.  
    AssetBundleManifest manifest = BuildPipeline.BuildAssetBundles (_packagePath, buildMap, BuildAssetBundleOptions.None, BuildTarget.StandaloneWindows64);
  16.  
    AssetDatabase.Refresh (); //刷新
  17.  
    if (manifest == null)
  18.  
    Debug.LogError( "Package AssetBundles Faild.");
  19.  
    else
  20.  
    Debug.Log( "Package AssetBundles Success.");
  21.  
    }
 
 
这样所有你选中的对象就都打进一个AssetBundle包了,建议关联或相似的文件可以这样打包。

我们也可以将每个对象都打进不同的AssetBundle包,看代码↓↓

  1.  
    [MenuItem( "AssetBundle/Build (Selected)")]
  2.  
    private static void _packageBuddleSelected() {
  3.  
    string _packagePath = UnityEditor.EditorUtility.OpenFolderPanel ("Select Package Path", "F:/", "");
  4.  
    if (_packagePath.Length <= 0 || !Directory.Exists (_packagePath))
  5.  
    return;
  6.  
    AssetBundleBuild[] buildMap = new AssetBundleBuild[objs.Length];
  7.  
    GameObject[] objs = Selection.gameObjects;
  8.  
    for (int i = 0; i < objs.Length; i++) {
  9.  
    string[] itemAsset = new string[] { AssetDatabase.GetAssetPath (objs[i]) };
  10.  
    buildMap[i].assetBundleName = objs[i].name;
  11.  
    buildMap [i].assetNames = itemAsset;
  12.  
    }
  13.  
    AssetBundleManifest manifest = BuildPipeline.BuildAssetBundles (_packagePath, buildMap, BuildAssetBundleOptions.None, BuildTarget.StandaloneWindows64);
  14.  
    AssetDatabase.Refresh ();
  15.  
    if(menifest == null)
  16.  
    Debug.LogError( "Error:Package Failed");
  17.  
    else
  18.  
    Debug.Log( "Package Success.");
  19.  
    }
[cpp]  view plain  copy
 
  1.   
 

按↑图操作,就可以打包选中的文件了。

更多花样,现在你可以自己创造了。今天就到这儿啦,咱们下期见~

猜你喜欢

转载自www.cnblogs.com/huwenya/p/9246229.html