欢迎使用CSDN-markdown编辑器

制作 AssetBundles

在Unity 4.x版本中,创建AssetBundle的过程是用脚本实现的。为了简化这个过程,在Unity5.x版本中包括AssetBundle创建工具。现在只需要在编辑器中选择Asset文件,在Inspector最下面就会出现一个选项,让你选择这个文件是否需要打包成AssetBundle。如果AssetBundle选项默认设置成为None,代表这个Asset文件不会被被打包成AssetBundle文件。可以创建新的AssetBundle,创建新的名字,并将这些名字作为Asset打包的目标地址。

AssetBundle creation

选中编辑器中的Asset文件,会在Inspector下面出现选项

在上面的图片中,状态机Asset被添加到了scene1/animdata。这个AssetBundle可能会包含了之前被添加的其他Asset文件。

空的AssetBundle可以使用菜单选项中的”New”选项创建。当你创建了一个新的AssetBundle文件之后,它就会出现在添加列表中。

AssetBundle的名字都是小写的,使用包含了大写字母的名字也会被转化成为小写。名字中包含分隔符“/”会自动创建文件夹,选中也

会出现二级菜单(如上图所示)。

 如果你创建了AssetBundle,但是未指定任何Asset文件,那么可以使用“Remove Unused Names”来删除空AssetBundle文件。

Asset文件的.meta文件也会被写入选择的AssetBundle文件。


输出AssetBundle文件

 AssetBundle文件是通过脚本输出。(和Unity 4.x相似)下面的脚本展示了如何输出:

using UnityEditor;

public class CreateAssetBundles
{
    [MenuItem ("Assets/Build AssetBundles")]
    static void BuildAllAssetBundles ()
    {
        BuildPipeline.BuildAssetBundles ("AssetBundles");
    }
}

上面的代码在Assets目录下创建了一个”Build AssetBundles”选项。当选择这个选项后,AssetBundles文件会被创建。这会创建一个

带有进度条的创建对话框。BuildPipeLine.BuildAssetBundles将AssetBundle创建到一个“AssetBundles”的文件夹。这个文件夹必须

在调用之前创建。AssetBundle将会被创建在这个文件夹内。

每个输出的AssetBundle文件都会出现在AssetBundle菜单中。另外,每个AssetBundle文件都会有一个关联文件.manifest。这个

manifest文件是文本类型,可以使用文本编辑器打开。这个文件提供了CRC和asset依赖的相关信息。下面是一个.manifest文件的例

子:


ManifestFileVersion: 0
CRC: 2422268106
Hashes:
  AssetFileHash:
    serializedVersion: 2
    Hash: 8b6db55a2344f068cf8a9be0a662ba15
  TypeTreeHash:
    serializedVersion: 2
    Hash: 37ad974993dbaa77485dd2a0c38f347a
HashAppended: 0
ClassTypes:
- Class: 91
  Script: {instanceID: 0}
Assets:
  Asset_0: Assets/Mecanim/StateMachine.controller
Dependencies: {}

除了创建这两个文件,还有另外两个文件会被创建:另外一个AssetBundle和另外一个manifest文件。这两个文件只要任何

AssetBundle被创建的时候都会被创建。这两个文件是为了放置AssetBundles的文件夹创建,所以你如果一直把AssetBundle放在同

一个文件夹,只会得到两个额外的文件。额外的文件不仅会包含其他的manifest的信息,还会包含AssetBundle之间的引用关系。

 在上面的例子中,因为我们只创建了一个AssetBundle文件,所以没有其他依赖关系。

ManifestFileVersion: 0
AssetBundleManifest:
  AssetBundleInfos:
    Info_0:
      Name: scene1assetbundle
      Dependencies: {}

AssetBundle 编辑器工具

获取 AssetBundles 的名字

下面的脚本可以显示所以可以创建的AssetBundle的名字

using UnityEditor;
using UnityEngine;

public class GetAssetBundleNames
{
    [MenuItem ("Assets/Get AssetBundle names")]
    static void GetNames ()
    {
        var names = AssetDatabase.GetAllAssetBundleNames();
        foreach (var name in names)
            Debug.Log ("AssetBundle: " + name);
    }
}

当重新打包文件改变了AssetBundle文件的回调

你可以使用AssetPostprocessor类中的OnPostprocessAssetbundleNameChanged方法,当AssetBundle发生改变的时候得到回调函数。

using UnityEngine;
using UnityEditor;

public class MyPostprocessor : AssetPostprocessor {

    void OnPostprocessAssetbundleNameChanged ( string path,
            string previous, string next) {
        Debug.Log("AB: " + path + " old: " + previous + " new: " + next);
    }
}

AssetBundle Variants(多样资源)

AssetBundle Variants 是Unity5.x新的特性。可以使用这个特性达到和虚拟的Assets类似的效果。例如,你可以创建AssetBundle的

变体例如“MyAssets.hd”或者“MyAsset.sd”。确保这些文件精确匹配。这两个AssetBundle的变体文件拥有相同的内部ID,在Unity创

建管道中使用。这两个AssetBundle在运行的过程中可以通过不同的后缀确定。


如何设立AssetBundle variants?

  1. 在编辑器中,在右边的标签中加上后缀名;
  2. 在代码中使用AssetImporter.assetBundleVariant选项。

AssetBundle variants

AssetBundle variants(多样资源)

完整的AssetBundle的名字将由AssetBundle的名字和variant的名字共同确定。

例如,如果For example, if you want to add “MyAssets.hd” as a variant AssetBundle,

脚本编写建议

将asset标记进AssetBundle的API接口

简单APIs

有一些非常简单的API可以用来创建AssetBundle。

BuildPipeline.BuildAssetBundles()

只需要提供:

  • 还有一个重载的版本提供AssetBundleBuild的数组,AssetBundleBuild包括asset与AssetBundle之间的映射关系。提供了更大的便利
  • 性,可以使用脚本控制映射关系。而且这个映射关系不会破坏或者代替数据库中已经存在的映射关系。

操作asset Database里的AssetBundle名字的API

BuildAssetBundleOptions

Manifest 文件

A manifest file is created for every AssetBundle which contains the following information:

  • The manifest file is next to the AssetBundle.
  • CRC
  • Asset file hash. A single hash for all the assets included in this AssetBundle, only used for incremental build check.
  • Type tree hash. A single hash for all the types included in this AssetBundle, only used for incremental build check.
  • Class types. All the class types included in this AssetBundle. These are used to get the new single hash when doing the type tree incremental build check.
  • Asset names. All the assets explicitly included in this AssetBundle.
  • Dependent AssetBundle names. All the AssetBundles which this AssetBundle depends on.
  • This manifest file is only used for incremental build, not necessary for runtime.

signle manifest file 

We generate a single manifest file which includes:

  • All the AssetBundles.
  • All the AssetBundle dependencies.

Single manifest AssetBundle

It only contains an AssetBundleManifest object which has following APIs:

AssetBundle 加载API

Now we have:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值