[原]在Unity中创建可远程加载的.unity3d包

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

  翻看Unity关于AssetBundle的手册,有相关的链接:

 

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


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

打开菜单项

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

 1  // 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     
 6     using UnityEngine;
 7     using UnityEditor;
 8     public class ExportAssetBundles {
 9         [MenuItem("Assets/Build AssetBundle From Selection - Track dependencies")]
10         static void ExportResource () {
11             // Bring up save panel
12             string path = EditorUtility.SaveFilePanel ("Save Resource", "", "New Resource", "unity3d");
13             if (path.Length != 0) {
14                 // Build the resource file from the active selection.
15                 Object[] selection = Selection.GetFiltered(typeof(Object), SelectionMode.DeepAssets);
16                 BuildPipeline.BuildAssetBundle(Selection.activeObject, selection, path, BuildAssetBundleOptions.CollectDependencies | BuildAssetBundleOptions.CompleteAssets);
17                 Selection.objects = selection;
18             }
19         }
20         [MenuItem("Assets/Build AssetBundle From Selection - No dependency tracking")]
21         static void ExportResourceNoTrack () {
22             // Bring up save panel
23             string path = EditorUtility.SaveFilePanel ("Save Resource", "", "New Resource", "unity3d");
24             if (path.Length != 0) {
25                 // Build the resource file from the active selection.
26                 BuildPipeline.BuildAssetBundle(Selection.activeObject, Selection.objects, path);
27             }
28         }
29     }

这时,在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 
 4 using UnityEngine;
 5 using UnityEditor;
 6 
 7 public class ExportAssetBundles {
 8 
 9     // Store current texture format for the TextureProcessor.
10     public static TextureImporterFormat textureFormat;
11 
12     [MenuItem("Assets/Build AssetBundle From Selection - PVRTC_RGB2")]
13     static void ExportResourceRGB2 () {
14         textureFormat = TextureImporterFormat.PVRTC_RGB2;
15         ExportResource();        
16     }    
17 
18     [MenuItem("Assets/Build AssetBundle From Selection - PVRTC_RGB4")]
19     static void ExportResourceRGB4 () {
20         textureFormat = TextureImporterFormat.PVRTC_RGB4;
21         ExportResource();
22     }
23 
24     static void ExportResource () {
25         // Bring up save panel.
26         string path = EditorUtility.SaveFilePanel ("Save Resource", "", "New Resource", "unity3d");
27 
28         if (path.Length != 0) {
29             // Build the resource file from the active selection.
30             Object[] selection = Selection.GetFiltered(typeof(Object), SelectionMode.DeepAssets);
31 
32             foreach (object asset in selection) {
33                 string assetPath = AssetDatabase.GetAssetPath((UnityEngine.Object) asset);
34                 if (asset is Texture2D) {
35                     // Force reimport thru TextureProcessor.
36                     AssetDatabase.ImportAsset(assetPath);
37                 }
38             }
39 
40             BuildPipeline.BuildAssetBundle(Selection.activeObject, selection, path, BuildAssetBundleOptions.CollectDependencies | BuildAssetBundleOptions.CompleteAssets);
41             Selection.objects = selection;
42         }
43     }
44 }
45 
46 // Changes the texture format when building the Asset Bundle.
47 using UnityEngine;
48 using UnityEditor;
49 public class TextureProcessor : AssetPostprocessor
50 {   
51     void OnPreprocessTexture() {
52         TextureImporter importer = assetImporter as TextureImporter;
53         importer.textureFormat = ExportAssetBundles.textureFormat;
54     }
55 }

 

也可以使用AssetDatabase.ImportAssetOptions.来控制如何导入组件。

 

首发地址: http://www.cnblogs.com/basecn/archive/2013/05/20/3264297.html

转载于:https://www.cnblogs.com/basecn/archive/2013/05/20/3264297.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值