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

来源:互联网 发布:xp ie8 淘宝 版本过低 编辑:程序博客网 时间:2024/05/22 14:56

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

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

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

[csharp] view plain copy
  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.     
  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盘)

[csharp] view plain copy
  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和场景。

[csharp] view plain copy
  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. }    

注意事项
a.LoadFromCacheOrDownload 可以指定版本,如果本地版本是新的,将不会从服务器读取
b.如果是多个资源打包在一起,我们要通过bundle.Load(),加载特定的资源
c.挂载在模型上的脚本也可以一起打包,但是保证脚本在原目录也要存在,否则加载出来无法运行。关于如何更新脚本,我将放在以后的章节中阐述。
  • AssetBundle依赖关系
如果一个公共对象被多个对象依赖,我们打包的时候,可以有两种选取。一种是比较省事的,就是将这个公共对象打包到每个对象中。这样会有很多弊端:内存被浪费了;加入公共对象改变了,每个依赖对象都得重新打包。AssetBundle提供了依赖关系打包。我们通过一个简单的例子来学习

[csharp] view plain copy
  1. //启用交叉引用,用于所有跟随的资源包文件,直到我们调用PopAssetDependencies    
  2.     BuildPipeline.PushAssetDependencies();    
  3.     
  4.     var options =    
  5.         BuildAssetBundleOptions.CollectDependencies |    
  6.         BuildAssetBundleOptions.CompleteAssets;    
  7.     
  8.     
  9.     //所有后续资源将共享这一资源包中的内容,由你来确保共享的资源包是否在其他资源载入之前载入    
  10.     BuildPipeline.BuildAssetBundle(    
  11.         AssetDatabase.LoadMainAssetAtPath("assets/artwork/lerpzuv.tif"),    
  12.         null"Shared.unity3d", options);    
  13.     
  14.     
  15.         //这个文件将共享这些资源,但是后续的资源包将无法继续共享它    
  16.     BuildPipeline.PushAssetDependencies();    
  17.     BuildPipeline.BuildAssetBundle(    
  18.         AssetDatabase.LoadMainAssetAtPath("Assets/Artwork/Lerpz.fbx"),    
  19.         null"Lerpz.unity3d", options);    
  20.     BuildPipeline.PopAssetDependencies();    
  21.     
  22.     
  23.     这个文件将共享这些资源,但是后续的资源包将无法继续共享它    
  24.     BuildPipeline.PushAssetDependencies();    
  25.     BuildPipeline.BuildAssetBundle(    
  26.         AssetDatabase.LoadMainAssetAtPath("Assets/Artwork/explosive guitex.prefab"),    
  27.         null"explosive.unity3d", options);    
  28.     BuildPipeline.PopAssetDependencies();    
  29.     
  30.     
  31.     BuildPipeline.PopAssetDependencies();    
我们在程序加载的时候必须保证先加载公共对象。否则,只能是在各个对象加载成功后,再通过程序手动添加进来,比较繁琐。在实际项目中,由于是团队开发,对象间的依赖关系通常会比较凌乱,最好在开发周期就定好相关的规范约束,方便管理。
  • 总结

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

  • 源码

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

  • 参考资料

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



转载自 : http://blog.csdn.net/yupu56/article/details/50426108