原创文章如需转载请注明:转载自风宇冲Unity3D教程学院
AssetBundles第一讲:基本使用AssetBundles是从unity导出你选择的assets,它使用特有的压缩格式并且应用可以实时去读取它。包括模型贴图音频等任何asset类型,甚至整个场景。压缩大小基本能达到zip的效果。AssetBundles从设计时就定位为可以很简单就下载到应用里。如果你想包括自定义的binary数据,就要用.bytes后缀,Unity将作为TextAssets导入他们。
注意:AssetBundles并不像Unity官方说的那样,各种unity版本兼容。经测试在unity4中创建的ab,在4中正常使用,但在3.5.0里无法正常使用。说明用AB不能向下兼容。
开发阶段:
(1)创建AssetBundles:
不能是 sceneobjects的objects使用BuildPipeline.BuildAssetBundletargetplatform要指定,不能用默认参数,默认模式是webplayer
- usingUnityEngine;
- usingUnityEditor;
- publicclassExportAssetBundles{
- [MenuItem("Assets/BuildAssetBundle From Selection - Track dependencies")]
- staticvoidExportResource() {
- // Bring up save panel
- stringpath=EditorUtility.SaveFilePanel ("SaveResource","","NewResource","unity3d");
- if(path.Length!=0) {
- // Build the resource file fromthe 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/BuildAssetBundle From Selection - No dependency tracking")]
- staticvoidExportResourceNoTrack() {
- // Bring up save panel
- stringpath=EditorUtility.SaveFilePanel ("SaveResource","","NewResource","unity3d");
- if(path.Length!=0) {
- // Build the resource file fromthe 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/BuildAssetBundle From Selection Names- Trackdependencies")]
- staticvoidExportResourceWithNames() {
- // Bring up savepanel
- stringpath=EditorUtility.SaveFilePanel("SaveResource","","NewResource","unity3d");
- if(path.Length!=0) {
- // Build the resourcefile from the active selection.
- Object[] selection=Selection.GetFiltered(typeof(Object),SelectionMode.DeepAssets);
- string[] names=newstring[selection.Length];
- for(inti=0;i
- {
- names[i] =i+"_"+selection[i].name;
- }
- BuildPipeline.BuildAssetBundleExplicitAssetNames(selection,names,
- path,BuildAssetBundleOptions.CollectDependencies|BuildAssetBundleOptions.CompleteAssets
- ,BuildTarget.StandaloneWindows);
- Selection.objects= selection;
- }
- }
- usingSystem;
- usingSystem.IO;
- usingUnityEngine;
- publicclassLoadAssetBundle:MonoBehaviour{
- privateAssetBundleassetBundle;
- privateAssetBundleCreateRequestrequest;
- voidUpdate () {
- }
- voidOnGUI()
- {
- if(GUI.Button(newRect(0,0,100,50),"Load"))
- {
- byte[] bs =File.ReadAllBytes(Application.dataPath+"/withNames.unity3d");
- request=AssetBundle.CreateFromMemory(bs);
- }
- if(GUI.Button(newRect(0,50,100,50),"Check"))
- {
- if(request.isDone==true)
- {
- assetBundle = request.assetBundle;
- UnityEngine.Objectobj =assetBundle.Load("0_Cube");
- Instantiate(obj);
- }
- }
- }
- usingUnityEngine;
- usingUnityEditor;
- public classBuildScene:MonoBehaviour{
- [MenuItem("Build/BuildWebplayerStreamed")]
- staticvoidBuildScenes()
- {
- string[] levels=newstring[1];
- levels[0] ="Assets/scene.unity";
- BuildPipeline.BuildStreamedSceneAssetBundle(levels,"Assets/myLevel.unity3d",BuildTarget.StandaloneOSXIntel);
- }
- }
兼容性
Platform compatibility forAssetBundles | |||||
Standalone | Webplayer | iOS | Android | ||
Editor | Y | Y | Y | Y | |
Standalone | Y | Y | |||
Webplayer | Y | Y | |||
iOS | Y | ||||
Android | Y |
(2)上传AssetBundles: 基于你所使用的服务器决定如何上传。
使用阶段:
(1)下载AssetBundles:
1AssetBundle.CreateFromFile:(1)只能用于pc和macstandalone(2)只支持Uncompressed:也就是在build的时候buildoption还要加上UncompressedAssetBundle。(3)必须得是绝对路径。 AssetBundleassetBundle =AssetBundle.CreateFromFile("D:/myBundle4.unity3d");
2AssetBundle.CreateFromMemory(bs);
- using System;
- using System.IO;
- using UnityEngine;
- public class LoadAssetBundle : MonoBehaviour {
- privateAssetBundle assetBundle;
- privateAssetBundleCreateRequest request;
- voidUpdate () {
- if(request!=null)
- print(request.progress);
- }
- voidOnGUI()
- {
- 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://即可,如WWW myWWW = new WWW("file://E://LSY/wamp/www/cat.jpg");
- using System;
- using System.IO;
- using UnityEngine;
- using System.Collections;
- public class LoadAssetBundle : MonoBehaviour {
- privateAssetBundle assetBundle;
- privatestring address ="http://127.0.0.1/AssetBundles/myBundle.unity3d";
- voidOnGUI()
- {
- if(GUI.Button(new Rect(0,0,100,50),"Load web"))
- {
- StartCoroutine(Load());
- }
- }
- IEnumerator Load() {
- // Download the file from theURL. It will not be saved in the Cache
- stringAssetName="";
- WWW www = newWWW(address);
- yield return www;
- if (www.error != null)
- throw new Exception("WWWdownload 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 conservememory
- bundle.Unload(false);
- }
- }
下载过程中可以更换下载地址,并保证版本一致, 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 {
- privateAssetBundle assetBundle;
- privatestring address ="http://127.0.0.1/AssetBundles/myBundle.unity3d";
- voidOnGUI()
- {
- if(GUI.Button(new Rect(0,0,100,50),"Load web"))
- {
- StartCoroutine(Load());
- }
- }
- IEnumerator Load() {
- stringAssetName="";
- WWW www =WWW.LoadFromCacheOrDownload(address,1);
- yield return www;
- if (www.error != null)
- throw new Exception("WWWdownload 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 conservememory
- 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
- //******************************************************
- usingSystem;
- usingSystem.IO;
- usingUnityEngine;
- public classLoadAssetBundle:MonoBehaviour{
- private AssetBundleassetBundle;
- private AssetBundleCreateRequestrequest;
- voidUpdate () {
- }
- voidOnGUI()
- {
- if(GUI.Button(newRect(0,0,100,50),"Load"))
- {
- byte[] bs =File.ReadAllBytes(Application.dataPath+"/myLevel.unity3d");
- request=AssetBundle.CreateFromMemory(bs);
- }
- if(GUI.Button(newRect(0,50,100,50),"Check"))
- {
- if(request.isDone ==true)
- {
- Application.LoadLevel("scene");
- }
- }
- }
- }
总结:制作AssetBundle主要有1BuildPipeline.BuildAssetBundle非场景2BuildPipeline.BuildStreamedSceneAssetBundle场景
使用AssetBundle主要有1AssetBundle.CreateFromFile 只能是Uncompressed格式2AssetBundle.CreateFromMemory 需要处理request3AssetBundle bundle = www.assetBundle;www又分为2种 (1)WWW www = newWWW(address);
(2)WWW www =WWW.LoadFromCacheOrDownload(address,1,crc); 其中网游用的最多的是LoadFromCacheOrDownload,因为第一次下载后就存在本地缓存了,之后就直接从本地缓存读取.crc是用来做数据校验的。
其中推荐LoadFromCacheOrDownload。不推荐CreateFromMemory,因为需要一个解析建AB结构的过程,比较耗时。CreateFromFile也不是很推荐,因为只支持非压缩格式,所以占容量比较多。
预制体打包成AssetBundle时:预制体可以搭脚本,并且指定关系等都可以照常使用。
要求:
(1)但是脚本必须是工程里有的
(2)AssetBundle里预制体上搭载的脚本必须和工程里的脚本一致。
否则会提示错误。