您的位置:首页 > 移动开发 > Unity3D

unity AssetBundle 使用方法1

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

AseetBundle 资源打包

如何离线创建AB

BuildPipeline.BuildAssetBundle(UnityEngine.Object mainAsset, UnityEngine.Object[] assets, string pathName, BuildAssetBundleOptions assetBundleOptions, BuildTarget targetPlatform)


•  将Project文件夹中的任意个Assets打包成一个AB

•  一般适用于对单个大规模场景的细分

参数:mainAsset 主资源,assets 打包对象,pathName 文件保存路径, assetBundleOptions 打包类型,targetPlatform 目标平台

  BuildPipeline.BuildStreamedSceneAssetBundle(string[] levels, string locationPath, BuildTarget target)


• 该方法与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。。

参考示例

对选择的一个文件夹内的对象进行打包

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

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

}


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

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

Object obj = Selection.activeObject;

string objPath = AssetDatabase.GetAssetPath(obj);
Debug.Log("objpath=" + objPath);
Object[] objs = { obj };

bool isBuildOK = BuildPipeline.BuildAssetBundle(Selection.activeGameObject, objs, savePath, ABBuilder, BuildTarget.StandaloneWindows);
if (isBuildOK)
{
Debug.Log("build ok");
}
else
{
Debug.Log("build error");
}

}


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

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

}


通过依赖关系进行打包

[MenuItem("ABpackage/depend")]
public static void test1()
{
//依赖关系:objB和objC依赖于objA
Object objA = AssetDatabase.LoadAssetAtPath("Assets/Texture/village_04.png", typeof(Object));
Object objB = AssetDatabase.LoadAssetAtPath("Assets/Model/Cube2.prefab", typeof(Object));
Object objC = AssetDatabase.LoadAssetAtPath("Assets/Model/Sphere.prefab", typeof(Object));

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

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

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: