现在的位置: 首页 > 综合 > 正文

详解assetbundle打包

2017年05月24日 ⁄ 综合 ⁄ 共 3208字 ⁄ 字号 评论关闭
· 创建AssetBundle
1.创建一个空的Prefab,命名Cube,然后创建一个Cube,将其拉到刚创建好的Prefab

2.新建一个脚本ExportAssetBundles.cs(代码来自官方文档),保存在Asset/Editor目录下
1.
2. [MenuItem("Assets/Build AssetBundle From Selection")]  
3. static void ExportResourceRGB2()  
4. {  
5.     // 打开保存面板,获得用户选择的路径  
6.     string path = EditorUtility.SaveFilePanel("Save Resource", "", "New Resource", "assetbundle");  7.   
8.     if (path.Length != 0)  
9.     {  
10.         // 选择的要保存的对象  
11.         Object[] selection = Selection.GetFiltered(typeof(Object), SelectionMode.DeepAssets);  
12.         //打包  
13.         BuildPipeline.BuildAssetBundle(Selection.activeObject, selection, path, BuildAssetBundleOptions.CollectDependencies | BuildAssetBundleOptions.CompleteAssets, BuildTarget.StandaloneWindows);  
14.     }  
15. }  
这时我们将看到Asset下面出现Build AssetBundle From
Selection
Build Scene

3.选中预设Cube,运行Build AssetBundle
From Selection
。这时会弹出一个保存框,将其命名为cube.unity3d(这里为了测试方便,放在c盘。实际项目中,我们是需要将他们放在web服务器,供所有客户端下载更新)

4.新建一个场景scene1.unity,上面放置几个模型,然后保存
5.选中该场景,在之前的ExportAssetBundles.cs脚本中添加打包场景的函数,运行Assets->Build
Scene
,保存为scene1.unity3d(这里为了测试方便,也放在c)
1. [MenuItem("Assets/Save Scene")]  
2. static void ExportScene()  
3. {  
4.       // 打开保存面板,获得用户选择的路径  
5.     string path = EditorUtility.SaveFilePanel("Save Resource", "", "New Resource", "unity3d");  
6.   
7.     if (path.Length != 0)  
8.     {  
9.         // 选择的要保存的对象  
10.         Object[] selection = Selection.GetFiltered(typeof(Object), SelectionMode.DeepAssets);  
11.         string[] scenes = {"Assets/scene1.unity"};  
12.         //打包  
13.         BuildPipeline.BuildPlayer(scenes,path,BuildTarget.StandaloneWindows,BuildOptions.BuildAdditionalStreamedScenes);  
14.     }  
15. }  
注意事项

a.AssetBundle的保存后缀名可以是assetbundle或者unity3d

b.BuildAssetBundle要根据不同的平台单独打包,BuildTarget参数指定平台,如果不指定,默认的webplayer
· 加载AssetBundle
我们通过一个简单的代码来演示如何加载assetbundle,包括加载普通asset和场景。
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.     }   
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.         }            
36.     }  
37. }  
注意事项

a.LoadFromCacheOrDownload 可以指定版本,如果本地版本是新的,将不会从服务器读取

b.如果是多个资源打包在一起,我们要通过bundle.Load(),加载特定的资源

c.挂载在模型上的脚本也可以一起打包,但是保证脚本在原目录也要存在,否则加载出来无法运行。
· AssetBundle依赖关系
如果一个公共对象被多个对象依赖,我们打包的时候,可以有两种选取。一种是比较省事的,就是将这个公共对象打包到每个对象中。这样会有很多弊端:内存被浪费了;加入公共对象改变了,每个依赖对象都得重新打包。AssetBundle提供了依赖关系打包。
我们在程序加载的时候必须保证先加载公共对象。否则,只能是在各个对象加载成功后,再通过程序手动添加进来,比较繁琐。在实际项目中,由于是团队开发,对象间的依赖关系通常会比较凌乱,最好在开发周期就定好相关的规范约束,方便管理。
· 总结
这一节的内容偏实际操作,官方文档和雨松的blog中已经有更加详细介绍。如果大家还有不明白的地方,可以结合文档再实际操作一下。
【上篇】
【下篇】

抱歉!评论已关闭.