(转)【风宇冲】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 ...
随机推荐
- SecureCRT发送心跳机制保持SSH在线(解决阿里云ECS)
设置如下:
- 在Asp.net core中使用WebScocket
今天小试了一下在Asp.net core中使用websocket,这里记录一下: 在 Startup 类的 Configure 方法中添加 WebSocket 中间件. app.UseWebSocke ...
- lodash用法系列(2),处理对象
Lodash用来操作对象和集合,比Underscore拥有更多的功能和更好的性能. 官网:https://lodash.com/引用:<script src="//cdnjs.clou ...
- 使用Coding4Fun工具包
Coding4Fun是一款很受WP开发者喜爱的开源类库,对于开发者来说,Coding4Fun上手很简单.只要从CodePlex下载Coding4Fun工具包,下载完成后,解压文件到一个文件夹中,里面有 ...
- vs已停止工作
第一步: 开始-->所有程序-->Microsoft Visual Studio 2012-->VisualStudio Tools-->VS2012 开发人员命令提示(以管理 ...
- python测试开发django-40.模型(model)中choices使用
前言 之前一直在想页面上如果一个字段只有固定的几个选项,类似select下拉框这种,如果在表里面设置一个外键的话,是不是有点傻了,这样为了几个选项弄一张表不值得. 后来看到Django模型中的字段有个 ...
- python测试开发django-30.发送附件EmailMessage
前言 Django的 send_mail() 和 send_mass_mail() 函式事实上是对 EmailMessage 类使用方式 的一个轻度封装.send_mail() 和相关的其他封装函式并 ...
- 关于面试总结6-SQL经典面试题
前言 用一条SQL 语句查询xuesheng表每门课都大于80 分的学生姓名,这个是面试考sql的一个非常经典的面试题 having和not in 查询 xuesheng表每门课都大于80 分的学生姓 ...
- IOS Devices Version
游戏项目中有一个专门用于收集IOS崩溃的接口和查询页,运营/测试的同事有时候会通过查询页大概看一下每日崩溃的情况,经常会问iPhone6,1是什么,iPhone7,1又是什么设备? 我从网上仔细搜 ...
- libcurl断点下载遇到的问题
最近游戏把资源(图片.配置.lua)的加载.更新全部改了 ,加载其实还好,就是不走之前的zip解压方式. 以前的大体流程: 下载 –> 启动 –> 解压 –> 更新 –> ...