自:http://blog.sina.com.cn/s/blog_471132920101gz8z.html

原创文章如需转载请注明:转载自风宇冲Unity3D教程学院

                                     AssetBundles第一讲:基本使用

AssetBundles是从unity导出你选择的assets,它使用特有的压缩格式并且应用可以实时去读取它。包括模型贴图音频等任何asset类型,甚至整个场景。压缩大小基本能达到zip的效果。AssetBundles从设计时就定位为可以很简单就下载到应用里。如果你想包括自定义的binary数据,就要用.bytes后缀,Unity将作为TextAssets导入他们。

 
注意:
AssetBundles并不像Unity官方说的那样,各种unity版本兼容。经测试在unity4中创建的ab,在4中正常使用,但在3.5.0里无法正常使用。说明用AB不能向下兼容。
开发阶段:
(1)创建AssetBundles:
不能是scene objects的objects使用BuildPipeline.BuildAssetBundle 
targetplatform要指定,不能用默认参数,默认模式是webplayer

  1. using UnityEngine;
  2. using UnityEditor;
  3. public class ExportAssetBundles {
  4. [MenuItem("Assets/Build AssetBundle From Selection - Track dependencies")]
  5. static void ExportResource () {
  6. // Bring up save panel
  7. string path = EditorUtility.SaveFilePanel ("Save Resource", "", "New Resource","unity3d");
  8. if (path.Length != 0) {
  9. // Build the resource file from the active selection.
  10. Object[] selection = Selection.GetFiltered(typeof(Object), SelectionMode.DeepAssets);
  11. BuildPipeline.BuildAssetBundle(Selection.activeObject, selection,
  12. path, BuildAssetBundleOptions.CollectDependencies |BuildAssetBundleOptions.CompleteAssets
  13. ,BuildTarget.StandaloneWindows);
  14. Selection.objects = selection;
  15. }
  16. }
  17. [MenuItem("Assets/Build AssetBundle From Selection - No dependency tracking")]
  18. static void ExportResourceNoTrack () {
  19. // Bring up save panel
  20. string path = EditorUtility.SaveFilePanel ("Save Resource", "", "New Resource","unity3d");
  21. if (path.Length != 0) {
  22. // Build the resource file from the active selection.
  23. BuildPipeline.BuildAssetBundle(Selection.activeObject, Selection.objects, path);
  24. }
  25. }
  26. }

bool BuildPipeline.BuildAssetBundleExplicitAssetNames(Object[] assets, string[] assetName, string pathName, BuildAssetBundleOptions assetBundleOptions, Buildtarget targetPlatform)
创建bundle并自定义名称。assetName的长度及排列与assets对应。并不是真正改变物体的名称,只是在assetBundle.Load的时候有个唯一对应的名称

 

对上面代码仅需修改第11行。将BuildPipeline.BuildAssetBundle(Selection.activeObject, selection,

path, BuildAssetBundleOptions.CollectDependencies | BuildAssetBundleOptions.CompleteAssets

,BuildTarget.StandaloneWindows); 里的第一个参数mainAsset去掉,并在selection也就是Object[]之后加string[] assetName即可

 
  1. [MenuItem("Assets/Build AssetBundle From Selection Names- Track dependencies")]
  2. static void ExportResourceWithNames () {
  3. // Bring up save panel
  4. string path = EditorUtility.SaveFilePanel ("Save Resource", "", "New Resource","unity3d");
  5. if (path.Length != 0) {
  6. // Build the resource file from the active selection.
  7. Object[] selection = Selection.GetFiltered(typeof(Object), SelectionMode.DeepAssets);
  8. string[] names = new string[selection.Length];
  9. for(int i =0;i
  10. {
  11. names[i] = i+"_"+ selection[i].name;
  12. }
  13. BuildPipeline.BuildAssetBundleExplicitAssetNames( selection,names,
  14. path, BuildAssetBundleOptions.CollectDependencies |BuildAssetBundleOptions.CompleteAssets
  15. ,BuildTarget.StandaloneWindows);
  16. Selection.objects = selection;
  17. }
  18. }
 
 
 
 
  1. using System;
  2. using System.IO;
  3. using UnityEngine;
  4. public class LoadAssetBundle : MonoBehaviour {
  5. private AssetBundle assetBundle;
  6. private AssetBundleCreateRequest request;
  7. void Update () {
  8. }
  9. void OnGUI()
  10. {
  11. if(GUI.Button(new Rect(0,0,100,50),"Load"))
  12. {
  13. byte[] bs = File.ReadAllBytes(Application.dataPath+ "/withNames.unity3d");
  14. request = AssetBundle.CreateFromMemory(bs);
  15. }
  16. if(GUI.Button(new Rect(0,50,100,50),"Check"))
  17. {
  18. if(request.isDone == true)
  19. {
  20. assetBundle = request.assetBundle;
  21. UnityEngine.Object obj = assetBundle.Load("0_Cube");
  22. Instantiate(obj);
  23. }
  24. }
  25. }
将场景做成AB时,BuildPipeline.BuildStreamedSceneAssetBundle

将一个或者多个场景打包到asset bundle里,可以为任何平台,并总创建单一的压缩.unity3d文件。
在下载后,可以用WWW.LoadFromCacheOrDownload从缓存里读取。 
  1. using UnityEngine;
  2. using UnityEditor;
  3. public class BuildScene : MonoBehaviour {
  4. [MenuItem ("Build/BuildWebplayerStreamed")]
  5. static void BuildScenes()
  6. {
  7. string [] levels = new string[1];
  8. levels[0] = "Assets/scene.unity";
  9. BuildPipeline.BuildStreamedSceneAssetBundle(levels, "Assets/myLevel.unity3d",BuildTarget.StandaloneOSXIntel);
  10. }
  11. }

兼容性

Platform compatibility for AssetBundles
  Standalone Webplayer iOS Android
Editor Y Y Y Y
Standalone Y Y    
Webplayer Y Y    
iOS     Y  
Android       Y

(2)上传AssetBundles: 基于你所使用的服务器决定如何上传。

使用阶段:
(1)下载AssetBundles:
1 AssetBundle.CreateFromFile: 

   (1)只能用于pc和mac standalone
   (2)只支持Uncompressed:也就是在build的时候buildoption还要加上UncompressedAssetBundle 。
   (3)必须得是绝对路径。
       AssetBundle assetBundle = AssetBundle.CreateFromFile("D:/myBundle4.unity3d");

2 AssetBundle.CreateFromMemory(bs);

  1. using System;
  2. using System.IO;
  3. using UnityEngine;
  4. public class LoadAssetBundle : MonoBehaviour {
  5. private AssetBundle assetBundle;
  6. private AssetBundleCreateRequest request;
  7. void Update () {
  8. if(request!=null)
  9. print(request.progress);
  10. }
  11. void OnGUI()
  12. {
  13. if(GUI.Button(new Rect(0,0,100,50),"Load"))
  14. {
  15. byte[] bs = File.ReadAllBytes("D:/myBundle.unity3d");
  16. request = AssetBundle.CreateFromMemory(bs);
  17. }
  18. if(GUI.Button(new Rect(0,50,100,50),"Check"))
  19. {
  20. if(request.isDone == true)
  21. {
  22. print("name: "+request.assetBundle.mainAsset.name);
  23. Instantiate(request.assetBundle.mainAsset);
  24. }
  25. }
  26. }
  27. }

3 AssetBundle bundle = www.assetBundle;

注:WWW也是可以读取本地文件的,
只需要在路径前file://即可,如
WWW myWWW = new WWW("file://E://LSY/wamp/www/cat.jpg");
  1. using System;
  2. using System.IO;
  3. using UnityEngine;
  4. using System.Collections;
  5. public class LoadAssetBundle : MonoBehaviour {
  6. private AssetBundle assetBundle;
  7. private string address = "http://127.0.0.1/AssetBundles/myBundle.unity3d";
  8. void OnGUI()
  9. {
  10. if(GUI.Button(new Rect(0,0,100,50),"Load web"))
  11. {
  12. StartCoroutine(Load());
  13. }
  14. }
  15. IEnumerator Load() {
  16. // Download the file from the URL. It will not be saved in the Cache
  17. string    AssetName="";
  18. WWW www = new WWW(address);
  19. yield return www;
  20. if (www.error != null)
  21. throw new Exception("WWW download had an error:" + www.error);
  22. AssetBundle bundle = www.assetBundle;
  23. if (AssetName == "")
  24. Instantiate(bundle.mainAsset);
  25. else
  26. Instantiate(bundle.Load(AssetName));
  27. // Unload the AssetBundles compressed contents to conserve memory
  28. bundle.Unload(false);
  29. }
  30. }
WWW.LoadFromCacheOrDownload
下载过程中可以更换下载地址,并保证版本一致, Webplayer的cache限制在50MB以内。
与普通的www下载仅仅是一句代码的区别
WWW www = new WWW(address);
WWW www = WWW.LoadFromCacheOrDownload(address,1);
  1. using System;
  2. using System.IO;
  3. using UnityEngine;
  4. using System.Collections;
  5. public class LoadAssetBundle : MonoBehaviour {
  6. private AssetBundle assetBundle;
  7. private string address = "http://127.0.0.1/AssetBundles/myBundle.unity3d";
  8. void OnGUI()
  9. {
  10. if(GUI.Button(new Rect(0,0,100,50),"Load web"))
  11. {
  12. StartCoroutine(Load());
  13. }
  14. }
  15. IEnumerator Load() {
  16. string    AssetName="";
  17. WWW www = WWW.LoadFromCacheOrDownload(address,1);
  18. yield return www;
  19. if (www.error != null)
  20. throw new Exception("WWW download had an error:" + www.error);
  21. AssetBundle bundle = www.assetBundle;
  22. if (AssetName == "")
  23. Instantiate(bundle.mainAsset);
  24. else
  25. Instantiate(bundle.Load(AssetName));
  26. // Unload the AssetBundles compressed contents to conserve memory
  27. bundle.Unload(false);
  28. }
  29. }
(2)使用AssetBundles里的资源:
bool AssetBundle.Contains(string name) bundle中是否含有名为name的asset

Object AssetBundle.Load(string name) 读取bundle中名称为name的asset

Object AssetBundle.LoadAll()
UnityEngine.Object[] objs = assetBundle.LoadAll();
在本例中assetBundle.mainAsset是GameObject,但是并不代表assetBundle.LoadAll();的返回值数组的第一个数据是GameObject即obj[0]等于assetBundle.mainAsset

assetBundle.mainAsset

AssetBundle.Unload(bool unloadAllLoadedObjects)
清空bundle里的所有资源。释放相关内存。清空后不能再通过该bundle创建物体。
unloadAllLoadedObjects为false: AssetBundle里的数据将会被释放,不影响已经scene中已经创建的相关物体。
unloadAllLoadedObjects为true: 不仅AssetBundle里的数据将会被释放,从该bundle创建的贴图材质等等asset将会被清空。如果scene中已经物体使用这些,连接将丢失。

 
读取场景:
当AssetBundle下载好后,直接Application.LoadLevel("scene");即可
 
  1. //******************************************************
  2. //AssetBundle.CreateFromMemory -> LoadLevel
  3. //******************************************************
  4. using System;
  5. using System.IO;
  6. using UnityEngine;
  7. public class LoadAssetBundle : MonoBehaviour {
  8. private AssetBundle assetBundle;
  9. private AssetBundleCreateRequest request;
  10. void Update () {
  11. }
  12. void OnGUI()
  13. {
  14. if(GUI.Button(new Rect(0,0,100,50),"Load"))
  15. {
  16. byte[] bs = File.ReadAllBytes(Application.dataPath+ "/myLevel.unity3d");
  17. request = AssetBundle.CreateFromMemory(bs);
  18. }
  19. if(GUI.Button(new Rect(0,50,100,50),"Check"))
  20. {
  21. if(request.isDone == true)
  22. {
  23. Application.LoadLevel("scene");
  24. }
  25. }
  26. }
  27. }
 
总结:
制作AssetBundle主要有
1 BuildPipeline.BuildAssetBundle 非场景
2 BuildPipeline.BuildStreamedSceneAssetBundle  场景
 
使用AssetBundle主要有
1 AssetBundle.CreateFromFile   只能是Uncompressed格式
2 AssetBundle.CreateFromMemory    需要处理request
3 AssetBundle bundle = www.assetBundle;  www又分为2种
  (1)WWW www = new WWW(address);
  (2)WWW www = WWW.LoadFromCacheOrDownload(address,1,crc);
          其中网游用的最多的是LoadFromCacheOrDownload,因为第一次下载后就存在本地缓存了,之后就直接从本地缓存读取.crc是用来做数据校验的。
 

其中推荐 LoadFromCacheOrDownload。不推荐CreateFromMemory,因为需要一个解析建AB结构的过程,比较耗时。CreateFromFile也不是很推荐,因为只支持非压缩格式,所以占容量比较多。

预制体打包成AssetBundle时:预制体可以搭脚本,并且指定关系等都可以照常使用。

要求:

(1)但是脚本必须是工程里有的

(2)AssetBundle里预制体上搭载的脚本必须和工程里的脚本一致。

否则会提示错误。

(转)【风宇冲】Unity3D教程宝典之AssetBundles:第一讲的更多相关文章

  1. (转)【风宇冲】Unity3D教程宝典之AssetBundles:第二讲

    原创文章如需转载请注明:转载自风宇冲Unity3D教程学院                             AssetBundles第二讲:AssetBundles与脚本 所有Unity的As ...

  2. (转)【风宇冲】Unity3D教程宝典之Blur

    原创文章如需转载请注明:转载自风宇冲Unity3D教程学院                   BlurBlur模糊其实理解了以后非常简单.核心原理就是 1个点的颜色 并不用该点的颜色,而是用该点周围 ...

  3. Unity3D教程宝典之Web服务器篇:(第三讲)PHP的Hello World

    转载自风宇冲Unity3D教程学院 引言:PHP是比较简单的编程语言,即使没接触过的也可以现学现用.PHP教程文档PHP100视频教程                           Unity接 ...

  4. Unity3D教程宝典之Web服务器篇:(第二讲)从服务器下载图片

    转载自风宇冲Unity3D教程学院                                    从Web服务器下载图片 上一讲风宇冲介绍了wamp服务器及安装.这回介绍如何从服务器下载内容至 ...

  5. Unity3D教程宝典之Web服务器篇:(第一讲)服务器的架设

    转载自风宇冲Unity3D教程学院 引言:本文主要介绍WAMP服务器的架设. 第一部分WAMP介绍;第二部分WAMP安装及使用.                        第一部分WAMP介绍 什 ...

  6. Unity3D教程宝典之Shader篇

    教程目录 基础讲:Shader学习方法基础讲:基础知识特别讲:常见问题解答特别讲:CG函数 第一讲: Shader总篇第二讲: Fixed Function Shader 第三讲: Vertex&am ...

  7. U3D教程宝典之两步实现超实用的XML存档

    两步实现超实用的XML存档 本套存档的优点:易使用,跨平台,防作弊(内容加密 + 防拷贝) 脚本下载地址 使用方法非常简单:把GameDataManager和XmlSaver两个脚本添加至工程后(1) ...

  8. Unity3D教程:无缝地形场景切换的解决方法

    http://www.unitymanual.com/6718.html 当我们开发一个大型项目的时候-会遇到这样的问题(地形场景的切换)这个只是字面意思-并不是重场景1的100  100 100坐标 ...

  9. 《ArcGIS Engine+C#实例开发教程》第一讲桌面GIS应用程序框架的建立

    原文:<ArcGIS Engine+C#实例开发教程>第一讲桌面GIS应用程序框架的建立 摘要:本讲主要是使用MapControl.PageLayoutControl.ToolbarCon ...

随机推荐

  1. AutoMapper实际项目运用

    AutoMapper是对象到对象的映射工具.在完成映射规则之后,AutoMapper可以将源对象转换为目标对象. 配置AutoMapper映射规则 AutoMapper是基于约定的,因此在实用映射之前 ...

  2. 使用CefSharp在.Net程序中嵌入Chrome浏览器(九)——性能问题

    在使用CEF的过程中,我发现了一个现象:WPF版的CEF比Chrome性能要差:一些有动画的地方会掉帧(例如,CSS动画,全屏图片拖动等),视频播放的效果也没有Chrome流畅. 查了一下相关资料,发 ...

  3. 使用Puppeteer进行数据抓取(一)——安装和使用

    Puppeteer是 Google Chrome 团队官方的Chrome 自动化工具.它本身是基于Chrome Dev Protocol协议实现的,但它提供了更高层次API封装,使用起来更加方便快捷. ...

  4. C#实现的三种方式实现模拟键盘按键

    1.System.Windows.Forms.SendKeys 组合键:Ctrl = ^ .Shift = + .Alt = % 模拟按键:A private void button1_Click(o ...

  5. asp.net MVC 中 Session统一验证的方法

    验证登录状态的方法有:1  进程外Session   2 方法过滤器(建一个类继承ActionFilterAttribute)然后给需要验证的方法或控制器加特性标签 3 :新建一个BaseContro ...

  6. 《Go语言实战》摘录:7.1 并发模式 - runner

    7.1 并发模式 - runner

  7. stap 命令

    SystemTap accepts script as command line option or external file, for example: * Command-line script ...

  8. 该对象尚未初始化。请确保在所有其他初始化代码后面的应用程序启动代码中调用 HttpConfiguration.EnsureInitialized()。

    WebAPI使用属性路由,配置config.MapHttpAttributeRoutes();后出现错误: System.InvalidOperationException: 该对象尚未初始化.请确保 ...

  9. Bootstrap碎语

    这里记录下某段时间Bootstrap的零散碎片. 1.有关Bootstrap的参考网站: ● 官方:http://getbootstrap.com/● 主题:http://bootswatch.com ...

  10. 什么是.Net, IL, CLI, BCL, FCL, CTS, CLS, CLR, JIT

    什么是.NET? 起源:比尔盖茨在2000年的Professional Developers Conference介绍了一个崭新的平台叫作Next Generation Windows Service ...