您的位置:首页 > 移动开发 > Unity3D

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

2013-05-20 15:45 267 查看
首发:http://blog.csdn.net/u010732148/article/details/8950552

--------------------------------------------------------------------------------------------------------

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

BuildPipeline.BuildAssetBundle
Building AssetBundles

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



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

打开菜单项

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

// 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);
}
}
}


这时,在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来设置需要的属性。
  下面的示例来展示构建组件包时如何设置不同的纹理贴图。

// 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;
}
}

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

【注】远程加载.unity3d包,可以使用WWW类
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息