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

 

 

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

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

打开菜单项

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

 

[csharp] view plaincopy
 
  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.       
  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.     }</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来设置需要的属性。

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

 

[csharp] view plaincopy
 
  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. }  

[csharp] view plaincopy
 
  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. }  
posted on 2015-01-26 11:36  zdBingo  阅读(421)  评论(0编辑  收藏  举报