Unity3d中使用assetbundle

本文介绍了如何在Unity3D中导出和读取AssetBundle。包括单个资源和多个资源打包成AssetBundle的方法,以及如何通过加载到内存并解压为具体资源的方式读取AssetBundle。

1、导出assetbundle:

  ①单个资源导出成assetbundle;

  ②多个资源导出成一个assetbundle;

2、读取assetbundle:

  ①加载到内存;

  ②解压为具体资源。

 

1、导出assetbundle:

  

①单个资源导出成assetbundle;

BuildPipeline.BuildAssetBundle(Object obj, null, string path, BuildAssetBundleOptions.CollectDependencies, BuildTarget.Android);

//obj 单个资源,转成Object类型
//path 路径(例如:"Assets/streamingassets/obj.unity3d"),资源包的后缀名可以任意写或不写
//BuildAssetBundleOptions.CollectDependencies 包含资源的依赖关系,比如要导出的资源是一个prefab,就会把其引用的mesh、贴图、材质球、动画统统导出到资源包里面去
//BuildTarget.Android 导出的资源包是给什么平台用的,Android、PC、或者别的什么


 

  ②多个资源导出成一个assetbundle;

BuildPipeline.BuildAssetBundle(null, Object[] objs, string path, BuildAssetBundleOptions.CollectDependencies, BuildTarget.Android);

//objs 包含多个资源的数组,Object[]类型
//path 路径(例如:"Assets/streamingassets/obj.unity3d"),资源包的后缀名可以任意写或不写
//BuildAssetBundleOptions.CollectDependencies 包含资源的依赖关系,比如要导出的资源是一个prefab,就会把其引用的mesh、贴图、材质球、动画统统导出到资源包里面去
//BuildTarget.Android 导出的资源包是给什么平台用的,Android、PC、或者别的什么

 

 

2、读取assetbundle:

  ①加载到内存;

  ②解压为具体资源。

IEnumerator Load(string path, string name)
{
    /*下面是加载到内存的过程*/
    WWW bundle = new WWW(path);  //例如:"Assets/streamingassets/obj.unity3d"
    yield return bundle;

    /*下面是解压为具体资源的过程*/
    Object obj = bundle.assetBundle.Load(name);  //name是具体资源的名字      
}

 

 

注:IEnumerator是协程,可以想象成Unity3d中的多线程,就是同时可以干很多事情的意思,虽然原理上与多线程不同,但使用起来的效果几乎没有区别。如果要处理的是很多资源的assetbundle的话,不管是导成一个了,还是多个的,要结合策划的具体需求合理规划协程的组织。

 

随手补充:

 

[MenuItem("Assets/Export Android Assetbundle")]
static void Export()
{
    string fileName = Selection.activeObject.name;
    string assetbundlePath = EditorUtility.SaveFilePanel("Save", Application.dataPath, fileName, "assetbundle");
    BuildPipeline.BuildAssetBundle(Selection.activeObject, null, assetbundlePath, BuildAssetBundleOptions.CollectDependencies | BuildAssetBundleOptions.CompleteAssets, BuildTarget.Android);        
}

 

转载于:https://www.cnblogs.com/fengrenyuan/p/4242555.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值