AssetBundle是unity自定义的资源格式,通过调用引擎的资源打包接口对资源进行打包成.assetbundle格式的资源包。本文介绍了AssetBundle的生成,使用,加载,卸载以及Unity资源更新的一个基本步骤。
目录
1.定义:
2.AssetBundle的生成:
1)设置AssetBundle包的属性——通过编辑器界面
补充:分组策略
2)调用引擎接口API 生成AssetBundle包
补充:打包参数
3)完全通过代码来打包AssetBundle
实例:
4)查看工程目录下生成的文件
1. 在 dir = Assetbundles 下会生成额外的两个文件,跟文件夹同名,一个没有后缀,一个有.manifest。
2. 每个AB包对应生成两个文件:
5)Assetbundle的哈希值:
3. 加载AssetBundle
从服务器下载到本地和从本地加载到内存,创建AssetBundle内存对象
从AssetBundle中加载Assets
4. 关于卸载资源
资源包卸载接口 AssetBundle.Unload(bool)
资源卸载 Resources.UnloadUnusedAssets
5. Unity资源更新的基本步骤
6. 参考
Asset+ Bundle(捆的意思)= AssetBundle(unity自定义格式的资源包)。一个AssetBundle就是一组资源的集合, 简称AB包。
可以打包任何unity引擎能够识别的资源,即Assets:模型,贴图,预置件(prefab),声音,场景等(如果是二进制文件,比如.bin,把扩展名改成.bytes,unity引擎即可将其识别为TextAsset)。
为了实现资源的热更新,使用AssetBundle+Lua 可以实现两种热更新框架方案 xLua 和 toLua。
选中Assets文件夹下的资源文件,可以在Inspector面板的最底下关于AseetBundle编辑窗口,将Asset标记到某个AssetBundle中:
通过【3】编辑的出的【1】,自动是小写。图中的“cube”就是AssetBundle包的名字。
【2】是变体的编辑框,即对于同一个包名,可以有不同的后缀====》如果对于一个同一个资源有两个版本,可以考虑通过不同的变体来区分 ,这里设置的变体就是unity3d。(可以达到在运行时动态替换AssetsBundle。因为AssetBundle名字相同,变体不同的AssetBundle之间拥有共同的内部id,他们之间可以任意切换。)
【4】是用来删除没有用过的包名的,目前只有这种删除的方法。
关于如何对资源进行划分,组成一个AssetBundle包,大概有这么几种:
一个UI界面或者所有UI界面一个包(这个界面里面的贴图和布局信息一个包)
一个角色或者所有角色一个包(这个角色里面的模型和动画一个包)
所有的场景所共享的部分一个包(包括贴图和模型)
所有声音资源打成一个包
所有shader打成一个包
所有模型打成一个包
所有材质打成一个包
把需要同时加载的资源或者某一时间内使用的所有资源打成一个包。
可以按照关卡分,一个关卡所需要的所有资源包括角色、贴图、声音等打成一个包。
也可以按照场景分,一个场景所需要的资源一个包。
把经常更新的资源放在一个单独的包里面,跟不经常更新的包分离。
可以把其他包 共享的资源 放在一个单独的包里面。减少包大小。即只存在一份共享的资源,其他都引用依赖这个包,而不是拥有一份这个资源的拷贝。
在编辑器界面设置完了资源的AssetBundle属性以后,就可以调用如下的编辑器脚本对设置过的资源进行打包了:
using UnityEditor;// MenuItem 以及 BuildPipeline
using System.IO;// Directory
public class CreateAssetBundlesScript{
[MenuItem("Assets/Build AssetsBundles")]
static void BuildAllAssetBundles()
{
string dir = "AssetBundles";// 大小写不敏感,即工程目录下存在一个叫做 Assetbundle 的文件夹,就是存在了
if(Directory.Exists(dir) == false)
{
Directory.CreateDirectory(dir);
}
BuildPipeline.BuildAssetBundles(dir, BuildAssetBundleOptions.None, BuildTarget.StandaloneWindows64);
}
}
关键函数为:
BuildPipeline.BuildAssetBundles(dir, BuildAssetBundleOptions.None, BuildTarget.StandaloneWindows64);
第一个参数:
AB包都会生成到此目录下,只要是在硬盘下的目录都可以。下面会具体查看在这个目录下生成了什么。
第二个参数:
打包参数。
第三个参数:
目标的构建平台,AssetBundle在不同平台之间是不完全兼容的。
a 关于压缩算法的有:
BuildAssetBundleOptions.None:LZMA压缩,包小,加载长。
BuildAssetBundleOptions.UncompressedAssetBundle:不压缩,包大,加载快。
BuildAssetBundleOptions.ChunkBasedCompression:LZ4压缩,压缩率没有LZMA高,但是我们可以加载指定资源而不用解压全部。
(使用LZMA算法压缩的在使用之前需要整体解压。使用BuildAssetBundleOptions.None压缩的包一旦被整体解压后,这个包会使用LZ4重新压缩。再次资源的时候不需要整体解压
(使用LZ4压缩,可以获得可以跟不压缩想媲美的加载速度,而且比不压缩文件要小。
b 关于类型信息的有:
BuildAssetBundleOptions.DisableWriteTypeTree:不写入类型信息,会降低对 Unity 不同版本的兼容性,但是资源包会变小,加载会变快。如果要发布到Web平台上,不能使用改选项。
BuildAssetBundleOptions.IgnoreTypeTreeChanges:Ignore the type tree changes when doing the incremental build check.忽略TypeTree的变化,不能与DisableWriteTypeTree同时使用。
c 强制重新打包的有:
BuildAssetBundleOptions.ForceRebuildAssetBundle
d 防止CDN缓存造成的bug:
BuildAssetBundleOptions.AppendHashToAssetBundleName(文件名后面加上 Hash 值):保证不一样的文件有不一样的文件名,这样从 CDN 服务器上的下载就不会因为缓存而获取到错误的文件。
使用到的接口是BuildPipeline.BuildAssetBundles的一个重载版本(多了第二个参数):
public static AssetBundleManifest BuildAssetBundles(string outputPath, AssetBundleBuild[] builds, BuildAssetBundleOptions assetBundleOptions, BuildTarget targetPlatform);
代码中设置资源的AssetBundle属性——通过构建若干 AssetBundleBuilder 对象,再传给BuildPipeLine.BuildAssetBundles 函数。
[MenuItem("Assets/Build Asset Bundles Using AssetBundleBuild")]
static void BuildMapABs()
{
// Create the array of bundle build details.
AssetBundleBuild[] buildMap = new AssetBundleBuild[1];
buildMap[0].assetBundleName = "newbundle";
string[] prefabAssets = new string[2];
prefabAssets[0] = "Assets/Prefabs/Capsule.prefab";
prefabAssets[1] = "Assets/Prefabs/Cube.prefab"; ;
buildMap[0].assetNames = prefabAssets;
string dir = "AssetBundles";
if (Directory.Exists(dir) == false)
{
Directory.CreateDirectory(dir);
}
BuildPipeline.BuildAssetBundles(dir, buildMap, BuildAssetBundleOptions.None, BuildTarget.StandaloneWindows64);
}
其中manifest文件记录了的信息有:
【Unity5 以后,资源包会附带一个 Manifest 文件,说明资源包的内容和依赖关系,自动检测并管理依赖关系。由于 Manifest 的存在,在使用的时候,加载一个 AB 之前可以确保先去加载它依赖的别的资源包。】
AssetBundleManifest类提供的访问接口: https://docs.unity3d.com/ScriptReference/AssetBundleManifest.html
下面是使用了 GetAllAssetBundles 和 GetAssetBundleHash 两个接口,用获取assetbundle的hash值。
///
/// 获取AssetBundle的Hash
///
/// 同名文件中,没有后缀的的那个文件
/// 根目录
/// 返回的结果
static void UpdateAssetBundleHash(string assetBundleManifestPath, string rootFolder, Dictionary curDic)
{
// 如上述的 Assetbundles
AssetBundle manifestBundle = AssetBundle.LoadFromFile(assetBundleManifestPath);
// 如上述的 Assetbundles.manifest
var manifest = (AssetBundleManifest)manifestBundle.LoadAsset("AssetBundleManifest");
// 根据manifest,获取具体有哪些ab包
var allAssetBundle = manifest.GetAllAssetBundles();
foreach (var item in allAssetBundle)
{
string itemPath = rootFolder.TrimStart('/') + "/" + item;
if (curDic.ContainsKey(itemPath))
{
curDic[itemPath].assetBundleHash = manifest.GetAssetBundleHash(item).ToString();
}
else
{
Debug.LogError("Update Asset Bundle Hash Error:" + itemPath);
}
}
manifestBundle.Unload(true);
}
1. AssetBundle.LoadFromMemory 和异步AssetBundle.LoadFromMemoryAsync
2. AssetBundle.LoadFromFile(上述例子中有使用) 和异步AssetBundle.LoadFromFileAsync
3. WWW.LoadfromCacheOrDownload(将会被弃用,用 AssetBundle.LoadFromFile 之类的 API + UnityWebRequest 类型 来代替了)
4. UnityWebRequest’s DownloadHandlerAssetBundle (Unity 5.3 or newer) 也可以用来下载文件列表。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.IO; // File
using UnityEngine.Networking; // UnityWebRequest
public class LoadAB : MonoBehaviour {
// Use this for initialization
IEnumerator Start () {
string path = "AssetBundles/assetbud/jow/cunb.unity3d";
//============
// 第一种方式 从内存加载
// AssetBundle ab = AssetBundle.LoadFromMemory(File.ReadAllBytes(path));
// 第二种方式 从文件加载
//AssetBundle ab = AssetBundle.LoadFromFile(path);
// 第四种方式 从文件加载 本地 file:/// 网页 http://
//string uri = @"file:///H:\unity\AssetBundleProject\webserver\AssetBundles\assetbud\jow\cunb.unity3d";
string uri = @"http://localhost/AssetBundles/assetbud/jow/cunb.unity3d";
UnityWebRequest request = UnityWebRequestAssetBundle.GetAssetBundle(uri);
yield return request.SendWebRequest();// 开始下载
//AssetBundle ab = DownloadHandlerAssetBundle.GetContent(request);// 一种方法
AssetBundle ab = (request.downloadHandler as DownloadHandlerAssetBundle).assetBundle;
//============
// 加载AB包中的某个资源 传入名字 加载资源也有对应的异步方式 具体事例看官网
GameObject cubePrefab = ab.LoadAsset("Cube");
Instantiate(cubePrefab);
// 加载AB包中的所有资源
Object[] objs = ab.LoadAllAssets();
foreach(Object obj in objs)
{
Instantiate(obj);
}
}
}
从内存加载,主要用于需要给资源包加密的时候。AssetBundle 无法识别你加密的资源包,所以要先把它用文件 IO 的方式读出来,在内存中解密成 AssetBundle。
AssetBundle的访问接口https://docs.unity3d.com/ScriptReference/AssetBundle.html
从场景assetbundle加载assets略有不同 https://docs.unity3d.com/Manual/AssetBundles-Manager.html
AssetBundle.Unload(false),则只将 AssetBundle 本身卸载掉,如果你从其中获取了图片、声音等资源,这些资源不会卸载。这样做的好处是资源包占掉的内存可以快速释放掉,坏处是,它和加载出来的图片、声音等资源之间的联系会被切断。如果再度从这个资源包中获取相同的图片,内存里同样的图片就有两份。
AssetBundle.Unload(true),卸载所有资源,即使有资源被使用着。(1在关卡切换、场景切换 2资源没被用的时候 调用)
在场景切换的时候会自动调用这个函数
----》版本检查(服务器和本地的app版本号一致,但资源版本号比本地高)
----》文件列表更新(服务器提供了哪些资源文件的下载,以及每个文件的效验码文件大小)
----》比较(与本地的文件列表进行对比以后,知道哪些文件需要下载)
----》资源下载
版版本检查本检查
http://www.sikiedu.com/course/74
某PPT分享