Unity热更新专题(五)如何打包AssetBundle

本文详细介绍了如何使用Unity的BuildPipeline.BuildAssetBundles方法来打包AssetBundle资源,并提供了具体的代码实例。此外,还讲解了如何加载这些AssetBundle。

Unity热更新专题(五)如何打包AssetBundle

如何打包AssetBundle?

我看了许多博客和论坛,写得有点乱,看的也有一点迷糊。而且unity4.6和Unity5.1在打包制作AssetBundle时,有很大区别。所以还是看看原API文档吧。

===================================================================================

BuildPipeline.BuildAssetBundles

[csharp]  view plain  copy
 print ?
  1. public static AssetBundleManifest BuildAssetBundles(string outputPath, BuildAssetBundleOptions assetBundleOptions = BuildAssetBundleOptions.None, BuildTarget targetPlatform = BuildTarget.WebPlayer);  

该方法就是打包制作AssetBundle的方法。

(好长的方法啊······

大概看一下
参数一:输出路径
参数二:打包的选项
参数三:平台

下面具体分析一下。

参数一:路径而已。没什么可以选的。

参数二:

描述的还是挺好理解的。

参数三:
这就更好理解了,就是Unity支持的平台。


描述:

制作在Unity编辑器里指定的所有物体的AssetBundle。

补充一点:(Unity5的新化~
1、我们创建一个cube,命名为Player。
2、把Player搞成Prefab。
我们可以看到选中Prefab时,下面有这个框。


我们点击New,标记为player.AssetBundle。

好了。下面修改代码。

创建脚本ExportAssetBundles.cs,把官方API文档上的代码贴上去。

[csharp]  view plain  copy
 print ?
  1. // C# Example  
  2. // Builds an asset bundle from the selected objects in the project view.  
  3. // Once compiled go to "Menu" -> "Assets" and select one of the choices  
  4. // to build the Asset Bundle  
  5.   
  6. using UnityEngine;  
  7. using UnityEditor;  
  8.   
  9. public class ExportAssetBundles  
  10. {  
  11.     [MenuItem("Test/Build Asset Bundles")]  
  12.     static void BuildABs() {  
  13.     // Put the bundles in a folder called "ABs" within the  
  14.     // Assets folder.  
  15.     BuildPipeline.BuildAssetBundles("Assets/ABs");  
  16. }  
  17. }  

我们可以看到Unity编辑器发生了变化。


没错。工具栏上出现了我们代码中定义的东东。点击它吧~

当然,如果你的工程中没有

Assets/ABs的话,是会出错的。


创建该目录,再次点击。我们就可以看到我们想看到的了。


加载AssetBundle。很简单。
贴个示例代码吧:
[csharp]  view plain  copy
 print ?
  1. using System;  
  2. using UnityEngine;  
  3. using System.Collections;  
  4.   
  5. public class Load: MonoBehaviour  
  6. {  
  7.     private string BundleURL = "file:///C:/cube.assetbundle";  
  8.     private string SceneURL = "file:///C:/scene1.unity3d";  
  9.   
  10.     void Start()  
  11.     {  
  12.         //BundleURL = "file//"+Application.dataPath+"/cube.assetbundle";  
  13.         Debug.Log(BundleURL);  
  14.         StartCoroutine(DownloadAssetAndScene());  
  15.     }  
  16.   
  17.     IEnumerator DownloadAssetAndScene()  
  18.     {  
  19.         //下载assetbundle,加载Cube  
  20.         using (WWW asset = new WWW(BundleURL))  
  21.         {  
  22.             yield return asset;  
  23.             AssetBundle bundle = asset.assetBundle;  
  24.             Instantiate(bundle.Load("Cube"));  
  25.             bundle.Unload(false);  
  26.             yield return new WaitForSeconds(5);  
  27.         }  
  28.         //下载场景,加载场景  
  29.         using (WWW scene = new WWW(SceneURL))  
  30.         {  
  31.             yield return scene;  
  32.             AssetBundle bundle = scene.assetBundle;  
  33.             Application.LoadLevel("scene1");  
  34.         }  
  35.          
  36.     }  
  37. }  



===================================================================================

到这里吧。


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值