自:http://blog.sina.com.cn/s/blog_471132920101gz8z.html
原创文章如需转载请注明:转载自风宇冲Unity3D教程学院
AssetBundles第一讲:基本使用
AssetBundles是从unity导出你选择的assets,它使用特有的压缩格式并且应用可以实时去读取它。包括模型贴图音频等任何asset类型,甚至整个场景。压缩大小基本能达到zip的效果。AssetBundles从设计时就定位为可以很简单就下载到应用里。如果你想包括自定义的binary数据,就要用.bytes后缀,Unity将作为TextAssets导入他们。
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
创建bundle并自定义名称。assetName的长度及排列与assets对应。并不是真正改变物体的名称,只是在assetBundle.Load的时候有个唯一对应的名称
(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中已经物体使用这些,连接将丢失。
- using UnityEngine;
- using UnityEditor;
- public class ExportAssetBundles {
- [MenuItem("Assets/Build AssetBundle From Selection - Track dependencies")]
- static void ExportResource () {
- // Bring up save panel
- string path = EditorUtility.SaveFilePanel ("Save Resource", "", "New Resource","unity3d");
- if (path.Length != 0) {
- // Build the resource file from the active selection.
- Object[] selection = Selection.GetFiltered(typeof(Object), SelectionMode.DeepAssets);
- BuildPipeline.BuildAssetBundle(Selection.activeObject, selection,
- path, BuildAssetBundleOptions.CollectDependencies |BuildAssetBundleOptions.CompleteAssets
- ,BuildTarget.StandaloneWindows);
- Selection.objects = selection;
- }
- }
- [MenuItem("Assets/Build AssetBundle From Selection - No dependency tracking")]
- static void ExportResourceNoTrack () {
- // Bring up save panel
- string path = EditorUtility.SaveFilePanel ("Save Resource", "", "New Resource","unity3d");
- if (path.Length != 0) {
- // Build the resource file from the active selection.
- BuildPipeline.BuildAssetBundle(Selection.activeObject, Selection.objects, path);
- }
- }
- }
创建bundle并自定义名称。assetName的长度及排列与assets对应。并不是真正改变物体的名称,只是在assetBundle.Load的时候有个唯一对应的名称
对上面代码仅需修改第11行。将BuildPipeline.BuildAssetBundle(Selection.activeObject, selection,
path, BuildAssetBundleOptions.CollectDependencies | BuildAssetBundleOptions.CompleteAssets
,BuildTarget.StandaloneWindows); 里的第一个参数mainAsset去掉,并在selection也就是Object[]之后加string[] assetName即可
- [MenuItem("Assets/Build AssetBundle From Selection Names- Track dependencies")]
- static void ExportResourceWithNames () {
- // Bring up save panel
- string path = EditorUtility.SaveFilePanel ("Save Resource", "", "New Resource","unity3d");
- if (path.Length != 0) {
- // Build the resource file from the active selection.
- Object[] selection = Selection.GetFiltered(typeof(Object), SelectionMode.DeepAssets);
- string[] names = new string[selection.Length];
- for(int i =0;i
- {
- names[i] = i+"_"+ selection[i].name;
- }
- BuildPipeline.BuildAssetBundleExplicitAssetNames( selection,names,
- path, BuildAssetBundleOptions.CollectDependencies |BuildAssetBundleOptions.CompleteAssets
- ,BuildTarget.StandaloneWindows);
- Selection.objects = selection;
- }
- }
- using System;
- using System.IO;
- using UnityEngine;
- public class LoadAssetBundle : MonoBehaviour {
- private AssetBundle assetBundle;
- private AssetBundleCreateRequest request;
- void Update () {
- }
- void OnGUI()
- {
- if(GUI.Button(new Rect(0,0,100,50),"Load"))
- {
- byte[] bs = File.ReadAllBytes(Application.dataPath+ "/withNames.unity3d");
- request = AssetBundle.CreateFromMemory(bs);
- }
- if(GUI.Button(new Rect(0,50,100,50),"Check"))
- {
- if(request.isDone == true)
- {
- assetBundle = request.assetBundle;
- UnityEngine.Object obj = assetBundle.Load("0_Cube");
- Instantiate(obj);
- }
- }
- }
将场景做成AB时,BuildPipeline.BuildStreamedSceneAssetBundle
兼容性
(2)上传AssetBundles: 基于你所使用的服务器决定如何上传。
使用阶段:
(1)下载AssetBundles:
1 AssetBundle.CreateFromFile:
3 AssetBundle bundle = www.assetBundle;
将一个或者多个场景打包到asset bundle里,可以为任何平台,并总创建单一的压缩.unity3d文件。
在下载后,可以用WWW.LoadFromCacheOrDownload从缓存里读取。
- using UnityEngine;
- using UnityEditor;
- public class BuildScene : MonoBehaviour {
- [MenuItem ("Build/BuildWebplayerStreamed")]
- static void BuildScenes()
- {
- string [] levels = new string[1];
- levels[0] = "Assets/scene.unity";
- BuildPipeline.BuildStreamedSceneAssetBundle(levels, "Assets/myLevel.unity3d",BuildTarget.StandaloneOSXIntel);
- }
- }
兼容性
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);
2 AssetBundle.CreateFromMemory(bs);
- using System;
- using System.IO;
- using UnityEngine;
- public class LoadAssetBundle : MonoBehaviour {
- private AssetBundle assetBundle;
- private AssetBundleCreateRequest request;
- void Update () {
- if(request!=null)
- print(request.progress);
- }
- void OnGUI()
- {
- if(GUI.Button(new Rect(0,0,100,50),"Load"))
- {
- byte[] bs = File.ReadAllBytes("D:/myBundle.unity3d");
- request = AssetBundle.CreateFromMemory(bs);
- }
- if(GUI.Button(new Rect(0,50,100,50),"Check"))
- {
- if(request.isDone == true)
- {
- print("name: "+request.assetBundle.mainAsset.name);
- Instantiate(request.assetBundle.mainAsset);
- }
- }
- }
- }
3 AssetBundle bundle = www.assetBundle;
注:WWW也是可以读取本地文件的,
只需要在路径前file://即可,如
- using System;
- using System.IO;
- using UnityEngine;
- using System.Collections;
- public class LoadAssetBundle : MonoBehaviour {
- private AssetBundle assetBundle;
- private string address = "http://127.0.0.1/AssetBundles/myBundle.unity3d";
- void OnGUI()
- {
- if(GUI.Button(new Rect(0,0,100,50),"Load web"))
- {
- StartCoroutine(Load());
- }
- }
- IEnumerator Load() {
- // Download the file from the URL. It will not be saved in the Cache
- string AssetName="";
- WWW www = new WWW(address);
- yield return www;
- if (www.error != null)
- throw new Exception("WWW download had an error:" + www.error);
- AssetBundle bundle = www.assetBundle;
- if (AssetName == "")
- Instantiate(bundle.mainAsset);
- else
- Instantiate(bundle.Load(AssetName));
- // Unload the AssetBundles compressed contents to conserve memory
- bundle.Unload(false);
- }
- }
WWW.LoadFromCacheOrDownload
下载过程中可以更换下载地址,并保证版本一致, Webplayer的cache限制在50MB以内。
与普通的www下载仅仅是一句代码的区别
WWW www = new WWW(address);
WWW www = WWW.LoadFromCacheOrDownload(address,1);
下载过程中可以更换下载地址,并保证版本一致, Webplayer的cache限制在50MB以内。
与普通的www下载仅仅是一句代码的区别
WWW www = new WWW(address);
WWW www = WWW.LoadFromCacheOrDownload(address,1);
- using System;
- using System.IO;
- using UnityEngine;
- using System.Collections;
- public class LoadAssetBundle : MonoBehaviour {
- private AssetBundle assetBundle;
- private string address = "http://127.0.0.1/AssetBundles/myBundle.unity3d";
- void OnGUI()
- {
- if(GUI.Button(new Rect(0,0,100,50),"Load web"))
- {
- StartCoroutine(Load());
- }
- }
- IEnumerator Load() {
- string AssetName="";
- WWW www = WWW.LoadFromCacheOrDownload(address,1);
- yield return www;
- if (www.error != null)
- throw new Exception("WWW download had an error:" + www.error);
- AssetBundle bundle = www.assetBundle;
- if (AssetName == "")
- Instantiate(bundle.mainAsset);
- else
- Instantiate(bundle.Load(AssetName));
- // Unload the AssetBundles compressed contents to conserve memory
- bundle.Unload(false);
- }
- }
(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");即可
- //******************************************************
- //AssetBundle.CreateFromMemory -> LoadLevel
- //******************************************************
- using System;
- using System.IO;
- using UnityEngine;
- public class LoadAssetBundle : MonoBehaviour {
- private AssetBundle assetBundle;
- private AssetBundleCreateRequest request;
- void Update () {
- }
- void OnGUI()
- {
- if(GUI.Button(new Rect(0,0,100,50),"Load"))
- {
- byte[] bs = File.ReadAllBytes(Application.dataPath+ "/myLevel.unity3d");
- request = AssetBundle.CreateFromMemory(bs);
- }
- if(GUI.Button(new Rect(0,50,100,50),"Check"))
- {
- if(request.isDone == true)
- {
- Application.LoadLevel("scene");
- }
- }
- }
- }
总结:
制作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);
(2)WWW www = WWW.LoadFromCacheOrDownload(address,1,crc);
其中网游用的最多的是LoadFromCacheOrDownload,因为第一次下载后就存在本地缓存了,之后就直接从本地缓存读取.crc是用来做数据校验的。
其中推荐 LoadFromCacheOrDownload。不推荐CreateFromMemory,因为需要一个解析建AB结构的过程,比较耗时。CreateFromFile也不是很推荐,因为只支持非压缩格式,所以占容量比较多。
预制体打包成AssetBundle时:预制体可以搭脚本,并且指定关系等都可以照常使用。
要求:
(1)但是脚本必须是工程里有的
(2)AssetBundle里预制体上搭载的脚本必须和工程里的脚本一致。
否则会提示错误。