欢迎使用CSDN-markdown编辑器

来源:互联网 发布:网络营销必备软件 编辑:程序博客网 时间:2024/06/05 19:53
制作 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: 0CRC: 2422268106Hashes:  AssetFileHash:    serializedVersion: 2    Hash: 8b6db55a2344f068cf8a9be0a662ba15  TypeTreeHash:    serializedVersion: 2    Hash: 37ad974993dbaa77485dd2a0c38f347aHashAppended: 0ClassTypes:- Class: 91  Script: {instanceID: 0}Assets:  Asset_0: Assets/Mecanim/StateMachine.controllerDependencies: {}

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

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

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

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

ManifestFileVersion: 0AssetBundleManifest:  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接口

  • 可以使用AssetImporter.assetBundleName设置AssetBundle的名字。

简单APIs

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

BuildPipeline.BuildAssetBundles()

只需要提供:

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

操作asset Database里的AssetBundle名字的API

  • AssetDatabase.GetAllAssetBundleNames() 返回AssetDataBase中所有的AssetBundle的名字。
  • AssetDatabase.GetAssetPathsFromAssetBundle 返回指定AssetBundle的资源路径。
  • AssetDatabase.RemoveAssetBundleName() 移除指定的AssetBundle 的Name。
  • AssetDatabase.GetUnusedAssetBundleNames() 返回所有不使用的AssetBundle的Names。
  • AssetDatabase.RemoveUnusedAssetBundleNames() 移除所有不使用的AssetBundle的Names。
  • AssetPostProcessor.OnPostprocessAssetbundleNameChanged 当用户改变了某个AssetBundle的名字时的回调方法。

BuildAssetBundleOptions

  • CollectDependencies 和 DeterministicAssetBundle 默认永远为true。
  • CompleteAssets 被忽略了,默认只能是true,因为我们操作的总是assets而不是objects。
  • ForceRebuildAssetBundle 新加的属性,你可以强制重新生成AssetBundle,即使资源没有发生改变。
  • IngoreTypeTreeChanges 新加的属性,你可以无视TypeTree的改变,即使TypeTree发生了改变。
  • DisableWriteTypeTree conflicts with IngoreTypeTreeChanges. 你不能无视TypeTree的改变,如果TypeTree发生了改变。

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:

  • GetAllAssetBundles() 返回本次build过程中所有的AssetBundle的名字。
  • GetDirectDependencies() 返回AssetBundle的直接依赖的名字。
  • GetAllDependencies() 返回AssetBundle的所有依赖的名字。
  • GetAssetBundleHash(string) 根据指定的AssetBundle返回Hash ID.
  • GetAllAssetBundlesWithVariant() 返回所有AssetBundle包含Variant多样资源。

AssetBundle 加载API

Now we have:

  • AssetBundle.GetAllAssetNames(). 返回AssetBundle中的所有资源的名字。
  • AssetBundle.GetAllScenePaths(). 返回场景AssetBundle中所有的场景资源的路径。
  • AssetBundle.LoadAsset(). 从一个AssetBundle中加载资源。
  • AssetBundle.LoadAllAssets().
  • AssetBundle.LoadAssetWithSubAssets().
  • 所有的异步版本仍然存在。
  • 组件类型不再返回,由获取GameObject然后GetComponent方法代替。

原创粉丝点击