在一个Unity项目中,发布包本身不一定要包括所有的Asset(译为资产或组件),其它的部分可以单独发布为.unity3d,再由程序从本地/远程加载执行,这部分不在本文讨论范围。虽然Unity并没有直接提供.unity3d的导出功能,但可以通过其手册了解到一些,并打开菜单项。
  翻看Unity关于AssetBundle的手册,有相关的链接:

【注意】导出.unity3d格式需要pro版本,非pro版本可以打开菜单项,但导出时会提示错误:

  我们可以使用Untiy提供的现成的脚本打开两个导出.unity3d的菜单项,也可以使用API根据自己的需求来写。当项目变得越来越大时,手工导出AssetBundle会越来越吃力,这时可能就需要自己来开发导出功能,自动创建AssetBundle了。

打开菜单项

  在Unity中创建名为ExprotAssetBundles的C#脚本,放到Editor目录下(必须是这个目录,以便在编辑器中生效)。把下面的代码复制到ExprotAssetBundles脚本中(可以在Building AssetBundles中找到这段代码)

  1. <span style="font-size: 14px;">    // C# Example
  2. // Builds an asset bundle from the selected objects in the project view.
  3. // Once compiled go to "Menu" -> "Assets" and select one of the choices
  4. // to build the Asset Bundle
  5. using UnityEngine;
  6. using UnityEditor;
  7. public class ExportAssetBundles {
  8. [MenuItem("Assets/Build AssetBundle From Selection - Track dependencies")]
  9. static void ExportResource () {
  10. // Bring up save panel
  11. string path = EditorUtility.SaveFilePanel ("Save Resource", "", "New Resource", "unity3d");
  12. if (path.Length != 0) {
  13. // Build the resource file from the active selection.
  14. Object[] selection = Selection.GetFiltered(typeof(Object), SelectionMode.DeepAssets);
  15. BuildPipeline.BuildAssetBundle(Selection.activeObject, selection, path, BuildAssetBundleOptions.CollectDependencies | BuildAssetBundleOptions.CompleteAssets);
  16. Selection.objects = selection;
  17. }
  18. }
  19. [MenuItem("Assets/Build AssetBundle From Selection - No dependency tracking")]
  20. static void ExportResourceNoTrack () {
  21. // Bring up save panel
  22. string path = EditorUtility.SaveFilePanel ("Save Resource", "", "New Resource", "unity3d");
  23. if (path.Length != 0) {
  24. // Build the resource file from the active selection.
  25. BuildPipeline.BuildAssetBundle(Selection.activeObject, Selection.objects, path);
  26. }
  27. }
  28. }</span>

这时,在Assets菜单下可以看到两个新的菜单项:

1. Build AssetBundle From Selection - Track dependencies
  这个选项会把当前对象打包到一个asset bundle中,并包含所有依赖。
2. Build AssetBundle From Selection - No dependency tracking
  与前一个相反的选项,只包含所选的asset
  例:创建一个Cube,拖拽生成一个预置体。右键点击预置体,选择"Build AssetBundle From Selection - Track dependencies",这时可以看到.unity3d的保存窗口。在项目中创建一个名为AssetBundles的目录,并把选中的预置体存为Cube.unity3d,可以看到窗口显示如下:
现在,就可以把Cube.unity3d放到任意位置,或自己的服务器上。

如何在创建组件包时修改属性

  可以在调用 BuildPipeline.BuildAssetBundle以后使用 AssetDatabase.ImportAsset来强制导入组件,然后用 AssetPostprocessor.OnPreprocessTexture来设置需要的属性。

  下面的示例来展示构建组件包时如何设置不同的纹理贴图。

  1. // Builds an asset bundle from the selected objects in the project view,
  2. // and changes the texture format using an AssetPostprocessor.
  3. using UnityEngine;
  4. using UnityEditor;
  5. public class ExportAssetBundles {
  6. // Store current texture format for the TextureProcessor.
  7. public static TextureImporterFormat textureFormat;
  8. [MenuItem("Assets/Build AssetBundle From Selection - PVRTC_RGB2")]
  9. static void ExportResourceRGB2 () {
  10. textureFormat = TextureImporterFormat.PVRTC_RGB2;
  11. ExportResource();
  12. }
  13. [MenuItem("Assets/Build AssetBundle From Selection - PVRTC_RGB4")]
  14. static void ExportResourceRGB4 () {
  15. textureFormat = TextureImporterFormat.PVRTC_RGB4;
  16. ExportResource();
  17. }
  18. static void ExportResource () {
  19. // Bring up save panel.
  20. string path = EditorUtility.SaveFilePanel ("Save Resource", "", "New Resource", "unity3d");
  21. if (path.Length != 0) {
  22. // Build the resource file from the active selection.
  23. Object[] selection = Selection.GetFiltered(typeof(Object), SelectionMode.DeepAssets);
  24. foreach (object asset in selection) {
  25. string assetPath = AssetDatabase.GetAssetPath((UnityEngine.Object) asset);
  26. if (asset is Texture2D) {
  27. // Force reimport thru TextureProcessor.
  28. AssetDatabase.ImportAsset(assetPath);
  29. }
  30. }
  31. BuildPipeline.BuildAssetBundle(Selection.activeObject, selection, path, BuildAssetBundleOptions.CollectDependencies | BuildAssetBundleOptions.CompleteAssets);
  32. Selection.objects = selection;
  33. }
  34. }
  35. }
  1. // Changes the texture format when building the Asset Bundle.
  2. using UnityEngine;
  3. using UnityEditor;
  4. public class TextureProcessor : AssetPostprocessor
  5. {
  6. void OnPreprocessTexture() {
  7. TextureImporter importer = assetImporter as TextureImporter;
  8. importer.textureFormat = ExportAssetBundles.textureFormat;
  9. }
  10. }

在Unity中创建可远程加载的.unity3d包的更多相关文章

  1. unity中实现场景之间加载进度条

    using UnityEngine; using System.Collections; using UnityEngine.SceneManagement; using UnityEngine.UI ...

  2. 钓鱼攻击之远程加载恶意Word模版文件上线CS

    0x00 前言 利用Word文档加载附加模板时的缺陷所发起的恶意请求而达到的攻击目的,所以当目标用户点开攻击者发给他的恶意word文档就可以通过向远程服务器请求恶意模板并执行恶意模板上的恶意代码.这里 ...

  3. Android中常见的图片加载框架

    图片加载涉及到图片的缓存.图片的处理.图片的显示等.而随着市面上手机设备的硬件水平飞速发展,对图片的显示要求越来越高,稍微处理不好就会造成内存溢出等问题.很多软件厂家的通用做法就是借用第三方的框架进行 ...

  4. javascript动态创建script标签,加载完成后调用回调

    代码如下: var head = document.getElementsByTagName('head')[0]; var script = document.createElement('scri ...

  5. 在Unity3D的网络游戏中实现资源动态加载

    用Unity3D制作基于web的网络游戏,不可避免的会用到一个技术-资源动态加载.比如想加载一个大场景的资源,不应该在游戏的开始让用户长时间等待全部资源的加载完毕.应该优先加载用户附近的场景资源,在游 ...

  6. CSS远程加载字体

    CSS 远程加载字体的方法,做网站CSS的都知道,用户浏览网站时,网页上的字体是加载本地的.换言之,如果网站使用了用户电脑所没有安装的字体,那显示字体就会被默认字体所代替了,自然效果就大受影响了. 上 ...

  7. MapXtreme在asp.net中的使用之加载地图(转)

    MapXtreme在asp.net中的使用之加载地图(转) Posted on 2010-05-04 19:44 Happy Coding 阅读(669) 评论(0) 编辑 收藏 1.地图保存在本地的 ...

  8. 018 关联映射文件中<class>标签中的lazy(懒加载)属性

    Lazy(懒加载): 只有在正真使用该对象时,才会创建这个对象 Hibernate中的lazy(懒加载): 只有我们在正真使用时,它才会发出SQL语句,给我们去查询,如果不使用对象则不会发SQL语句进 ...

  9. Bootstrap Modal 使用remote从远程加载内容

        Bootstrap的Modal这个模态窗组件还是很好用的,但在开发的过程中模态窗中的内容大部分都是从后端加载的.要实现模态窗的内容是从后端加载话,常用的实现方式有2种.它们是:     (1) ...

随机推荐

  1. 立体角的单位——立体弧度(sr)

    国际单位制(SI)中,关于物理量 发光强度 的介绍: 1cd(坎德拉)为一光源在给定方向的发光强度,该光源发出频率为540×1012Hz(赫兹)的单色辐射,且在此方向上的辐射强度为 1/683 W/s ...

  2. artDialog 配置参数

    http://blog.csdn.net/techbirds_bao/article/details/8531083

  3. 更改星级评分条 RatingBar 的样式

    1.首先在布局中引用星级评分条: <RatingBar            android:id="@+id/room_ratingbar"            styl ...

  4. java 通过反射获取调用类方法及属性

    首先说下反射是什么?反射是Sun公司推出的一组API,此组API位于Java.lang.reflect中 反射的作用是编写工具(例如eclipse),编写框架,当然对于一般的程序,我们不可能用反射来做 ...

  5. 转-squid介绍及其简单配置

    本文原始出处:http://linuxme.blog.51cto.com/1850814/372960 1.Squid是什么? Squid中文权威指南:http://zyan.cc/book/squi ...

  6. How to create your own custom 404 error page and handle redirect in SharePoint 分类: Sharepoint 2015-07-08 00:22 4人阅读 评论(0) 收藏

    1. In your MOSS server, make a copy of %systemdrive%\Program Files\Common Files\Microsoft Shared\Web ...

  7. storyboard简单认识

    ## storyboard文件的认识 - 作用:描述软件界面 - 程序启动的简单过程 - 程序一启动,就会加载`Main.storyboard`文件 - 会创建箭头所指的控制器,并且显示控制器所管理的 ...

  8. android base64 和 aes 加密 解密

    package pioneerbarcode.ccw.com.encryptanddecode;import android.os.Bundle;import android.support.v7.a ...

  9. [网络技术][转]网卡的offload概念

    网络数据包分析 网卡Offload 对于网络安全来说,网络传输数据包的捕获和分析是个基础工作,绿盟科技研究员在日常工作中,经常会捕获到一些大小远大于MTU值的数据包,经过分析这些大包的特性,发现和网卡 ...

  10. 音频软件消除人声的一点体会(cood edit ,goldwav)

    音频软件消除人声的一点体会(cood  edit ,goldwav) 使用方法: 1.打开文件 2.命令处理(红色位置可以调整到你认为合适的数据或效果) 3.效果:两个软件均处理后的效果均可以接受.不 ...