(转)【风宇冲】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);
- }
- }
- }
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即可
- [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);
- }
- }
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里预制体上搭载的脚本必须和工程里的脚本一致。
否则会提示错误。
(转)【风宇冲】Unity3D教程宝典之AssetBundles:第一讲的更多相关文章
- (转)【风宇冲】Unity3D教程宝典之AssetBundles:第二讲
原创文章如需转载请注明:转载自风宇冲Unity3D教程学院 AssetBundles第二讲:AssetBundles与脚本 所有Unity的As ...
- (转)【风宇冲】Unity3D教程宝典之Blur
原创文章如需转载请注明:转载自风宇冲Unity3D教程学院 BlurBlur模糊其实理解了以后非常简单.核心原理就是 1个点的颜色 并不用该点的颜色,而是用该点周围 ...
- Unity3D教程宝典之Web服务器篇:(第三讲)PHP的Hello World
转载自风宇冲Unity3D教程学院 引言:PHP是比较简单的编程语言,即使没接触过的也可以现学现用.PHP教程文档PHP100视频教程 Unity接 ...
- Unity3D教程宝典之Web服务器篇:(第二讲)从服务器下载图片
转载自风宇冲Unity3D教程学院 从Web服务器下载图片 上一讲风宇冲介绍了wamp服务器及安装.这回介绍如何从服务器下载内容至 ...
- Unity3D教程宝典之Web服务器篇:(第一讲)服务器的架设
转载自风宇冲Unity3D教程学院 引言:本文主要介绍WAMP服务器的架设. 第一部分WAMP介绍;第二部分WAMP安装及使用. 第一部分WAMP介绍 什 ...
- Unity3D教程宝典之Shader篇
教程目录 基础讲:Shader学习方法基础讲:基础知识特别讲:常见问题解答特别讲:CG函数 第一讲: Shader总篇第二讲: Fixed Function Shader 第三讲: Vertex&am ...
- U3D教程宝典之两步实现超实用的XML存档
两步实现超实用的XML存档 本套存档的优点:易使用,跨平台,防作弊(内容加密 + 防拷贝) 脚本下载地址 使用方法非常简单:把GameDataManager和XmlSaver两个脚本添加至工程后(1) ...
- Unity3D教程:无缝地形场景切换的解决方法
http://www.unitymanual.com/6718.html 当我们开发一个大型项目的时候-会遇到这样的问题(地形场景的切换)这个只是字面意思-并不是重场景1的100 100 100坐标 ...
- 《ArcGIS Engine+C#实例开发教程》第一讲桌面GIS应用程序框架的建立
原文:<ArcGIS Engine+C#实例开发教程>第一讲桌面GIS应用程序框架的建立 摘要:本讲主要是使用MapControl.PageLayoutControl.ToolbarCon ...
随机推荐
- AutoMapper实际项目运用
AutoMapper是对象到对象的映射工具.在完成映射规则之后,AutoMapper可以将源对象转换为目标对象. 配置AutoMapper映射规则 AutoMapper是基于约定的,因此在实用映射之前 ...
- 使用CefSharp在.Net程序中嵌入Chrome浏览器(九)——性能问题
在使用CEF的过程中,我发现了一个现象:WPF版的CEF比Chrome性能要差:一些有动画的地方会掉帧(例如,CSS动画,全屏图片拖动等),视频播放的效果也没有Chrome流畅. 查了一下相关资料,发 ...
- 使用Puppeteer进行数据抓取(一)——安装和使用
Puppeteer是 Google Chrome 团队官方的Chrome 自动化工具.它本身是基于Chrome Dev Protocol协议实现的,但它提供了更高层次API封装,使用起来更加方便快捷. ...
- C#实现的三种方式实现模拟键盘按键
1.System.Windows.Forms.SendKeys 组合键:Ctrl = ^ .Shift = + .Alt = % 模拟按键:A private void button1_Click(o ...
- asp.net MVC 中 Session统一验证的方法
验证登录状态的方法有:1 进程外Session 2 方法过滤器(建一个类继承ActionFilterAttribute)然后给需要验证的方法或控制器加特性标签 3 :新建一个BaseContro ...
- 《Go语言实战》摘录:7.1 并发模式 - runner
7.1 并发模式 - runner
- stap 命令
SystemTap accepts script as command line option or external file, for example: * Command-line script ...
- 该对象尚未初始化。请确保在所有其他初始化代码后面的应用程序启动代码中调用 HttpConfiguration.EnsureInitialized()。
WebAPI使用属性路由,配置config.MapHttpAttributeRoutes();后出现错误: System.InvalidOperationException: 该对象尚未初始化.请确保 ...
- Bootstrap碎语
这里记录下某段时间Bootstrap的零散碎片. 1.有关Bootstrap的参考网站: ● 官方:http://getbootstrap.com/● 主题:http://bootswatch.com ...
- 什么是.Net, IL, CLI, BCL, FCL, CTS, CLS, CLR, JIT
什么是.NET? 起源:比尔盖茨在2000年的Professional Developers Conference介绍了一个崭新的平台叫作Next Generation Windows Service ...