1.AssetBundle的定义和作用
用处?
1,AssetBundle是一个压缩包包含模型、贴图、预制体、声音、甚至整个场景,可以在游戏运行的时候被加载;
2,AssetBundle自身保存着互相的依赖关系;
3,压缩包可以使用LZMA和LZ4压缩算法,减少包大小,更快的进行网络传输;
4,把一些可以下载内容放在AssetBundle里面,可以减少安装包的大小;
2.什么是AssetBundl
可以归为两点:
1,它是一个存在于硬盘上的文件。可以称之为压缩包。这个压缩包可以认为是一个文件夹,里面包含了多个文件。这些文件可以分为两类:serializedfile 和 resource files。(序列化文件和源文件)
serialized file:资源被打碎放在一个对象中,最后统一被写进一个单独的文件(只有一个)
resource files:某些二进制资源(图片、声音)被单独保存,方便快速加载
2,它是一个AssetBundle对象,我们可以通过代码从一个特定的压缩包加载出来的对象。这个对象包含了所有我们当初添加到这个压缩包里面的内容,我们可以通 过这个对象加载出来使用。
3.AssetBundle使用流程图
4.AssetBundle使用流程(简称AB)
1,指定资源的AssetBundle属性
(xxxa/xxx)这里xxxa会生成目录,名字为xxx
2,构建AssetBundle包
3,上传AB包
4,加载AB包和包里面的资源
5.AssetBundle使用相关API
压缩
BuildPipeline.BuildAssetBundles(dir, BuildAssetBundleOptions.None,BuildTarget.StandaloneWindows64);
加载
AssetBundle ab =AssetBundle.LoadFromFile("AssetBundles/scene/wall.unity3d");
GameObject wallPrefab =ab.LoadAsset<GameObject>("CubeWall");
Instantiate(wallPrefab);
using UnityEditor;
using System.IO;
public class CreateAssetsBundles {
[MenuItem("Assets/Build AssetBundles")]
static void BulidAssetsBundle()
{
string dir = "AssetsBundles";
if(Directory.Exists(dir)==false)
{
Directory.CreateDirectory(dir);
}
BuildPipeline.BuildAssetBundles(dir, BuildAssetBundleOptions.None, BuildTarget.StandaloneWindows64);
}
}
6.AssetBundle分组策略(仅供参考)
1,逻辑实体分组
a,一个UI界面或者所有UI界面一个包(这个界面里面的贴图和布局信息一个包)
b,一个角色或者所有角色一个包(这个角色里面的模型和动画一个包)
c,所有的场景所共享的部分一个包(包括贴图和模型)
2,按照类型分组
所有声音资源打成一个包,所有shader打成一个包,所有模型打成一个包,所有材质打成一个包
3,按照使用分组
把在某一时间内使用的所有资源打成一个包。可以按照关卡分,一个关卡所需要的所有资源包括角色、贴图、声音等打成一个包。也可以按照场景分,一个场景 所 需要的资源一个包
1,把经常更新的资源放在一个单独的包里面,跟不经常更新的包分离
2,把需要同时加载的资源放在一个包里面
3,可以把其他包共享的资源放在一个单独的包里面
4,把一些需要同时加载的小资源打包成一个包
5,如果对于一个同一个资源有两个版本,可以考虑通过后缀来区分 v1 v2 v3 unity3dv1 unity3dv2
7.依赖打包
8.Build AssetBundles - 1
1,Build的路径(随意只要是在硬盘上都可以的)
2,BuildAssetBundleOptions
BuildAssetBundleOptions.None:使用LZMA算法压缩,压缩的包更小,但是加载时间更长。使用之前需要整体解压。一旦被解压,这个包会使用LZ4重新压缩。使用资源的时候不需要整体解压。在下载的时候可以使用LZMA算法,一旦它被下载了之后,它会使用LZ4算法保存到本地上。
BuildAssetBundleOptions.UncompressedAssetBundle:不压缩,包大,加载快
BuildAssetBundleOptions.ChunkBasedCompression:使用LZ4压缩,压缩率没有LZMA高,但是我们可以加载指定资源而不用解压全部。
注意使用LZ4压缩,可以获得可以跟不压缩想媲美的加载速度,而且比不压缩文件要小。
3.BuildTarget
选择build出来的AB包要使用的平台
9.依赖包的使用
原则上在使用一个资源前先加载出他所依赖资源所在的包。
10. AssetBundles的使用
1,AssetBundle.LoadFromMemoryAsync(从内存加载,异步)
2,AssetBundle.LoadFromFile
3,WWW.LoadFromCacheOrDownload
4,UnityWebRequest
10.1 AssetBundle.LoadFromMemoryAsync(异步)
IEnumerator Start()
{
string path = "AssetsBundles/cubewall.unity3d";
//第一种加载ab的方式,LoadFromMemoryAsync(异步加载)
AssetBundleCreateRequest request = AssetBundle.LoadFromMemoryAsync(File.ReadAllBytes(path));
yield return request;
AssetBundle ab = request.assetBundle;
//使用里面的资源
GameObject wallPrefab = ab.LoadAsset<GameObject>("CubeWall");
Instantiate(wallPrefab);
}
10.2 LoadFromMemory(同步加载)
void Start () {
string path = "AssetsBundles/cubewall.unity3d";
///第一种加载ab的方式,LoadFromMemory(同步加载)
AssetBundle ab = AssetBundle.LoadFromMemory(File.ReadAllBytes(path));
//使用里面的资源
GameObject wallPrefab = ab.LoadAsset<GameObject>("CubeWall");
Instantiate(wallPrefab);
}
10.3 WWW
IEnumerator Start()
{
string path = "AssetsBundles/cubewall.unity3d";
第三种方式,www
while (Caching.ready == false)
{
yield return null;
}
//从服务器
WWW www = WWW.LoadFromCacheOrDownload(@"http://localhost/AssetBundleProject/AssetsBundles/cubewall.unity3d", 1);
//file:// file:///
//从本地
//WWW www = WWW.LoadFromCacheOrDownload(@"file://E:\All_Of_Code\unity_Project\AssetBundleProject\AssetsBundles\cubewall.unity3d", 1);
yield return www;
if (string.IsNullOrEmpty(www.error) == false)
{
Debug.Log(www.error);
yield break;
}
AssetBundle ab = www.assetBundle;
//使用里面的资源
GameObject wallPrefab = ab.LoadAsset<GameObject>("CubeWall");
Instantiate(wallPrefab);
}
10.4 AssetBundle.LoadFromFile
void Start () {
string path = "AssetsBundles/cubewall.unity3d";
//第二种方式,同步
AssetBundle shareab = AssetBundle.LoadFromFile("AssetsBundles/share.unity3d");
AssetBundle ab = AssetBundle.LoadFromFile(path);
//使用里面的资源
GameObject wallPrefab = ab.LoadAsset<GameObject>("CubeWall");
Instantiate(wallPrefab);
}
10.5 UnityWebRequest
IEnumerator Start()
{
///第四种方式,使用UnityWebRequest
//(1)加载本地资源
//string uri = @"file://E:\All_Of_Code\unity_Project\AssetBundleProject\AssetsBundles\cubewall.unity3d";
//(2)加载服务器资源
string uri = @"http://localhost/AssetBundleProject/AssetsBundles/cubewall.unity3d";
UnityWebRequest request = UnityWebRequest.GetAssetBundle(uri);
yield return request.SendWebRequest();
//1.
//AssetBundle ab = DownloadHandlerAssetBundle.GetContent(request);
//2.
AssetBundle ab = (request.downloadHandler as DownloadHandlerAssetBundle).assetBundle;
使用里面的资源
GameObject wallPrefab = ab.LoadAsset<GameObject>("CubeWall");
Instantiate(wallPrefab);
}
11. 通过Manifest文件得到某个包的依赖
IEnumerator Start()
{
///第四种方式,使用UnityWebRequest
//(1)加载本地资源
string uri = @"file://E:\All_Of_Code\unity_Project\AssetBundleProject\AssetsBundles\cubewall.unity3d";
//(2)加载服务器资源
//string uri = @"http://localhost/AssetBundleProject/AssetsBundles/cubewall.unity3d";
UnityWebRequest request = UnityWebRequest.GetAssetBundle(uri);
yield return request.SendWebRequest();
//1.
//AssetBundle ab = DownloadHandlerAssetBundle.GetContent(request);
//2.
AssetBundle ab = (request.downloadHandler as DownloadHandlerAssetBundle).assetBundle;
使用里面的资源
GameObject wallPrefab = ab.LoadAsset<GameObject>("CubeWall");
Instantiate(wallPrefab);
AssetBundle manifestAB = AssetBundle.LoadFromFile("AssetsBundles/AssetsBundles");
AssetBundleManifest manifest = manifestAB.LoadAsset<AssetBundleManifest>("AssetBundleManifest");
/* foreach(string name in manifest.GetAllAssetBundles())
{
print(name);
}*/
string[] strs = manifest.GetAllDependencies("cubewall.unity3d");
foreach(string name in strs)
{
print(name);
AssetBundle.LoadFromFile("AssetsBundles/" + name);
}
}