Unity手游之路<十一>资源打包Assetbundle

http://blog.csdn.net/janeky/article/details/17652021

在手游的运营过程中,更新资源是比不可少的。资源管理第一步是资源打包。传统的打包可以将所有物件制成预设Prefab,打包成场景。今天我们来一起学习官方推荐的Assetbundle,它是Unity(Pro)提供的资源打包策略。利用AssetBundle,可以将几乎所有的资源都打包封装,便于客户端更新下载新的资源。

(转载请注明原文出处http://blog.csdn.net/janeky/article/details/17652021)

  • 创建AssetBundle

1.创建一个空的Prefab,命名Cube,然后创建一个Cube,将其拉到刚创建好的Prefab
2.新建一个脚本ExportAssetBundles.cs(代码来自官方文档),保存在Asset/Editor目录下

  1. //在Unity编辑器中添加菜单
  2. [MenuItem("Assets/Build AssetBundle From Selection")]
  3. static void ExportResourceRGB2()
  4. {
  5. // 打开保存面板,获得用户选择的路径
  6. string path = EditorUtility.SaveFilePanel("Save Resource", "", "New Resource", "assetbundle");
  7. if (path.Length != 0)
  8. {
  9. // 选择的要保存的对象
  10. Object[] selection = Selection.GetFiltered(typeof(Object), SelectionMode.DeepAssets);
  11. //打包
  12. BuildPipeline.BuildAssetBundle(Selection.activeObject, selection, path, BuildAssetBundleOptions.CollectDependencies | BuildAssetBundleOptions.CompleteAssets, BuildTarget.StandaloneWindows);
  13. }
  14. }

这时我们将看到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. if (path.Length != 0)
  7. {
  8. // 选择的要保存的对象
  9. Object[] selection = Selection.GetFiltered(typeof(Object), SelectionMode.DeepAssets);
  10. string[] scenes = {"Assets/scene1.unity"};
  11. //打包
  12. BuildPipeline.BuildPlayer(scenes,path,BuildTarget.StandaloneWindows,BuildOptions.BuildAdditionalStreamedScenes);
  13. }
  14. }

注意事项
a.AssetBundle的保存后缀名可以是assetbundle或者unity3d
b.BuildAssetBundle要根据不同的平台单独打包,BuildTarget参数指定平台,如果不指定,默认的webplayer

  • 加载AssetBundle

我们通过一个简单的代码来演示如何加载assetbundle,包括加载普通asset和场景。

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

注意事项
a.LoadFromCacheOrDownload 可以指定版本,如果本地版本是新的,将不会从服务器读取
b.如果是多个资源打包在一起,我们要通过bundle.Load(),加载特定的资源
c.挂载在模型上的脚本也可以一起打包,但是保证脚本在原目录也要存在,否则加载出来无法运行。关于如何更新脚本,我将放在以后的章节中阐述。

  • AssetBundle依赖关系

如果一个公共对象被多个对象依赖,我们打包的时候,可以有两种选取。一种是比较省事的,就是将这个公共对象打包到每个对象中。这样会有很多弊端:内存被浪费了;加入公共对象改变了,每个依赖对象都得重新打包。AssetBundle提供了依赖关系打包。我们通过一个简单的例子来学习

  1. //启用交叉引用,用于所有跟随的资源包文件,直到我们调用PopAssetDependencies
  2. BuildPipeline.PushAssetDependencies();
  3. var options =
  4. BuildAssetBundleOptions.CollectDependencies |
  5. BuildAssetBundleOptions.CompleteAssets;
  6. //所有后续资源将共享这一资源包中的内容,由你来确保共享的资源包是否在其他资源载入之前载入
  7. BuildPipeline.BuildAssetBundle(
  8. AssetDatabase.LoadMainAssetAtPath("assets/artwork/lerpzuv.tif"),
  9. null, "Shared.unity3d", options);
  10. //这个文件将共享这些资源,但是后续的资源包将无法继续共享它
  11. BuildPipeline.PushAssetDependencies();
  12. BuildPipeline.BuildAssetBundle(
  13. AssetDatabase.LoadMainAssetAtPath("Assets/Artwork/Lerpz.fbx"),
  14. null, "Lerpz.unity3d", options);
  15. BuildPipeline.PopAssetDependencies();
  16. 这个文件将共享这些资源,但是后续的资源包将无法继续共享它
  17. BuildPipeline.PushAssetDependencies();
  18. BuildPipeline.BuildAssetBundle(
  19. AssetDatabase.LoadMainAssetAtPath("Assets/Artwork/explosive guitex.prefab"),
  20. null, "explosive.unity3d", options);
  21. BuildPipeline.PopAssetDependencies();
  22. BuildPipeline.PopAssetDependencies();

我们在程序加载的时候必须保证先加载公共对象。否则,只能是在各个对象加载成功后,再通过程序手动添加进来,比较繁琐。在实际项目中,由于是团队开发,对象间的依赖关系通常会比较凌乱,最好在开发周期就定好相关的规范约束,方便管理。

  • 总结

这一节的内容偏实际操作,官方文档和雨松的blog中已经有更加详细介绍。如果大家还有不明白的地方,可以结合文档再实际操作一下。后面的章节,我将花更多的时间介绍核心的内容:资源的增量更新,和代码程序的更新。

  • 源码

http://pan.baidu.com/s/1i3BOAPn

  • 参考资料

1.http://www.xuanyusong.com/
2.http://game.ceeger.com/Manual/DownloadingAssetBundles.html

上一篇:loadrunner中如何将MD5加密的值转换为大写


下一篇:(转)Unity3D研究院之Assetbundle的原理(六十一)