【风宇冲】Unity3D教程宝典之AssetBundles:第一讲
自:http://blog.sina.com.cn/s/blog_471132920101gz8z.html
原创文章如需转载请注明:转载自风宇冲Unity3D教程学院
AssetBundles是从unity导出你选择的assets,它使用特有的压缩格式并且应用可以实时去读取它。包括模型贴图音频等任何asset类型,甚至整个场景。压缩大小基本能达到zip的效果。AssetBundles从设计时就定位为可以很简单就下载到应用里。如果你想包括自定义的binary数据,就要用.bytes后缀,Unity将作为TextAssets导入他们。
开发阶段:
(1)创建AssetBundles:
不能是scene objects的objects使用BuildPipeline.BuildAssetBundle
- 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);
- }
- }
- }
- 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:
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;
- 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);
- }
- }
下载过程中可以更换下载地址,并保证版本一致, 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.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");
- }
- }
- }
- }
(2)WWW www = WWW.LoadFromCacheOrDownload(address,1,crc);
其中推荐 LoadFromCacheOrDownload。不推荐CreateFromMemory,因为需要一个解析建AB结构的过程,比较耗时。CreateFromFile也不是很推荐,因为只支持非压缩格式,所以占容量比较多。
预制体打包成AssetBundle时:预制体可以搭脚本,并且指定关系等都可以照常使用。
要求:
(1)但是脚本必须是工程里有的
(2)AssetBundle里预制体上搭载的脚本必须和工程里的脚本一致。
否则会提示错误。
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。