在Unity中创建可远程加载的.unity3d包
在一个Unity项目中,发布包本身不一定要包括所有的Asset(译为资产或组件),其它的部分可以单独发布为.unity3d,再由程序从本地/远程加载执行,这部分不在本文讨论范围。虽然Unity并没有直接提供.unity3d的导出功能,但可以通过其手册了解到一些,并打开菜单项。
翻看Unity关于AssetBundle的手册,有相关的链接:
【注意】导出.unity3d格式需要pro版本,非pro版本可以打开菜单项,但导出时会提示错误:
我们可以使用Untiy提供的现成的脚本打开两个导出.unity3d的菜单项,也可以使用API根据自己的需求来写。当项目变得越来越大时,手工导出AssetBundle会越来越吃力,这时可能就需要自己来开发导出功能,自动创建AssetBundle了。
打开菜单项
在Unity中创建名为ExprotAssetBundles的C#脚本,放到Editor目录下(必须是这个目录,以便在编辑器中生效)。把下面的代码复制到ExprotAssetBundles脚本中(可以在Building AssetBundles中找到这段代码)
- <span style="font-size: 14px;"> // C# Example
- // Builds an asset bundle from the selected objects in the project view.
- // Once compiled go to "Menu" -> "Assets" and select one of the choices
- // to build the Asset Bundle
- 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);
- 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);
- }
- }
- }</span>
这时,在Assets菜单下可以看到两个新的菜单项:
如何在创建组件包时修改属性
可以在调用 BuildPipeline.BuildAssetBundle以后使用 AssetDatabase.ImportAsset来强制导入组件,然后用 AssetPostprocessor.OnPreprocessTexture来设置需要的属性。
下面的示例来展示构建组件包时如何设置不同的纹理贴图。
- // Builds an asset bundle from the selected objects in the project view,
- // and changes the texture format using an AssetPostprocessor.
- using UnityEngine;
- using UnityEditor;
- public class ExportAssetBundles {
- // Store current texture format for the TextureProcessor.
- public static TextureImporterFormat textureFormat;
- [MenuItem("Assets/Build AssetBundle From Selection - PVRTC_RGB2")]
- static void ExportResourceRGB2 () {
- textureFormat = TextureImporterFormat.PVRTC_RGB2;
- ExportResource();
- }
- [MenuItem("Assets/Build AssetBundle From Selection - PVRTC_RGB4")]
- static void ExportResourceRGB4 () {
- textureFormat = TextureImporterFormat.PVRTC_RGB4;
- ExportResource();
- }
- 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);
- foreach (object asset in selection) {
- string assetPath = AssetDatabase.GetAssetPath((UnityEngine.Object) asset);
- if (asset is Texture2D) {
- // Force reimport thru TextureProcessor.
- AssetDatabase.ImportAsset(assetPath);
- }
- }
- BuildPipeline.BuildAssetBundle(Selection.activeObject, selection, path, BuildAssetBundleOptions.CollectDependencies | BuildAssetBundleOptions.CompleteAssets);
- Selection.objects = selection;
- }
- }
- }
- // Changes the texture format when building the Asset Bundle.
- using UnityEngine;
- using UnityEditor;
- public class TextureProcessor : AssetPostprocessor
- {
- void OnPreprocessTexture() {
- TextureImporter importer = assetImporter as TextureImporter;
- importer.textureFormat = ExportAssetBundles.textureFormat;
- }
- }
在Unity中创建可远程加载的.unity3d包的更多相关文章
- unity中实现场景之间加载进度条
using UnityEngine; using System.Collections; using UnityEngine.SceneManagement; using UnityEngine.UI ...
- 钓鱼攻击之远程加载恶意Word模版文件上线CS
0x00 前言 利用Word文档加载附加模板时的缺陷所发起的恶意请求而达到的攻击目的,所以当目标用户点开攻击者发给他的恶意word文档就可以通过向远程服务器请求恶意模板并执行恶意模板上的恶意代码.这里 ...
- Android中常见的图片加载框架
图片加载涉及到图片的缓存.图片的处理.图片的显示等.而随着市面上手机设备的硬件水平飞速发展,对图片的显示要求越来越高,稍微处理不好就会造成内存溢出等问题.很多软件厂家的通用做法就是借用第三方的框架进行 ...
- javascript动态创建script标签,加载完成后调用回调
代码如下: var head = document.getElementsByTagName('head')[0]; var script = document.createElement('scri ...
- 在Unity3D的网络游戏中实现资源动态加载
用Unity3D制作基于web的网络游戏,不可避免的会用到一个技术-资源动态加载.比如想加载一个大场景的资源,不应该在游戏的开始让用户长时间等待全部资源的加载完毕.应该优先加载用户附近的场景资源,在游 ...
- CSS远程加载字体
CSS 远程加载字体的方法,做网站CSS的都知道,用户浏览网站时,网页上的字体是加载本地的.换言之,如果网站使用了用户电脑所没有安装的字体,那显示字体就会被默认字体所代替了,自然效果就大受影响了. 上 ...
- MapXtreme在asp.net中的使用之加载地图(转)
MapXtreme在asp.net中的使用之加载地图(转) Posted on 2010-05-04 19:44 Happy Coding 阅读(669) 评论(0) 编辑 收藏 1.地图保存在本地的 ...
- 018 关联映射文件中<class>标签中的lazy(懒加载)属性
Lazy(懒加载): 只有在正真使用该对象时,才会创建这个对象 Hibernate中的lazy(懒加载): 只有我们在正真使用时,它才会发出SQL语句,给我们去查询,如果不使用对象则不会发SQL语句进 ...
- Bootstrap Modal 使用remote从远程加载内容
Bootstrap的Modal这个模态窗组件还是很好用的,但在开发的过程中模态窗中的内容大部分都是从后端加载的.要实现模态窗的内容是从后端加载话,常用的实现方式有2种.它们是: (1) ...
随机推荐
- 线性控制原理——PID算法应用
使用控制系统(PID)控制被控对象 PID控制的三要素:控制器,被控对象,反馈器.控制器就是一个数学模型,就PID来说,等同于PID算法.是对反馈量的一个处理与输出.通俗的说就是对于每个被控的量,我的 ...
- Bootstrap <基础十八>面包屑导航(Breadcrumbs)
面包屑导航(Breadcrumbs)是一种基于网站层次信息的显示方式.以博客为例,面包屑导航可以显示发布日期.类别或标签.它们表示当前页面在导航层次结构内的位置. Bootstrap 中的面包屑导航( ...
- Eclipse 项目中有红色感叹号,怎么办?
/** * JDK1.5中增加了自动拆装箱的语言特性,在基本类型和包装类型之间可以相互地转换和运算. * 大家也都知道Java中==运算符是比较两个对象间的引用是否相同.在自动拆装箱与“==”运算符之 ...
- Spring(3)
一.Spring容器 Spring 提供了两种类型的IOC容器(后面还会讲到AOP容器): (1)BeanFactory:IOC容器的基本实现,是Spring框架的基础设施,面向Spring的本身 ( ...
- gnome3.X添加开机启动项
背景:升级gnome后发现gnome-session-properties不见了,想把sslocal随机启动遇到了麻烦... 特别说明:此为图形桌面开机启动项,因此只有通过图形桌面登陆用户后才能启动. ...
- 006-Map、Tuple、Zip实战解析
006-Map.Tuple.Zip实战解析 实战代码 Map实战 本身是映射,映射关系在实际中应用非常广泛,比如:配置信息都是Key-Value形式 键值对不存在下边关系 是一个immutable(不 ...
- 关于<form>标签
<form>用于为用户输入创建HTML表单,表单用于向服务器传输数据 form是块级元素,其前后会产生折行 <form>包含: 1.input元素:(根据不同的type属性,输 ...
- Psp个人软件开发软件需求分析和用例分析
Psp个人软件开发软件需求分析和用例分析 一.需求分析 1.业务需求 1.1 应用背景 开发项目进度计划总是那么不明确,延期经常出现,甚至无法给出一个相对比较明确的延迟时间.这样给市场的推广会带来很大 ...
- kanboard邮件通知
1. 复制config.default.php为config.php 2. 修改一下内容 define('MAIL_TRANSPORT', 'smtp');define('MAIL_SMTP_HOST ...
- wchar_t内置还是别名?小问题一则(升级公司以前代码遇到的问题)
问题: 原来的2008工程用2010编译后,运行程序出现无法定位程序输入点 *@basic_string@_WU@*和*@basic_string@G@* 解决: 关闭“语言选项”中“将WChar_t ...