一、什么是AssetBundle?
可以归为两点:
1,它是一个存在于硬盘上的文件。可以称之为压缩包。这个压缩包可以认为是一个文件夹,里面包含了多个文件。这些文件可以分为两类:serialized file 和 resource files。(序列化文件和源文件) serialized file:资源被打碎放在一个对象中,最后统一被写进一个单独的文件(只有一个) resource files:某些二进制资源(图片、声音)被单独保存,方便快速加载
2,它是一个AssetBundle对象,我们可以通过代码从一个特定的压缩包加载出来的对象。这个对象包含了所有我们当初添加到这个压缩包里面的内容,我们可以通过这个对象加载出来使用。
二、AssetBundle使用流程(简称AB)
1,指定资源的AssetBundle属性 (xxxa/xxx)这里xxxa会生成目录,名字为xxx
选中你要打包的材质,为其添加assetbundle属性
2,构建AssetBundle包
创建一个Editor文件夹(名字确定),写一个扩展脚本CreatAssetBundle
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
using System.IO;
public class CreatAssetBundle{
[MenuItem("Assets/Build AssetBundles")]//菜单栏添加
static void BuildAllAssetBundle() {
string dir = "AssetBundles";
if (Directory.Exists(dir) == false) {//创建文件
Directory.CreateDirectory(dir);
}
BuildPipeline.BuildAssetBundles(dir, BuildAssetBundleOptions.None, BuildTarget.StandaloneWindows64);
///<summary>
///BuildAssetBundleOptions.None:使用LZMA算法压缩,压缩的包更小,但是加载时间更长。使用
///前需要整体解压。一旦被解压,这个包会使用LZ4重新压缩。使用资源的时候不需要整体解压。
///在下载的时候可以使用LZMA算法,一旦它被下载了之后,它会使用LZ4算法保存到本地上。
/// BuildAssetBundleOptions.UncompressedAssetBundle:不压缩,包大,加载快
/// BuildAssetBundleOptions.ChunkBasedCompression:使用LZ4压缩,压缩率没有LZMA高,但是我们可以加载指定资源而不用解压全部。
///注意使用LZ4压缩,可以获得可以跟不压缩想媲美的加载速度,而且比不压缩文件要小。
///</summary>
}
}
资源打包
脚本添加后,assets 下会出现Build AssetBundles,点击进行打包
3,上传AB包
将ab包放到服务器上
4,加载AB包和包里面的资源
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.IO;
using UnityEngine.Networking;
public class LoadFormFileExample : MonoBehaviour {
IEnumerator Start () {
//string path = "AssetBundles/wall.unity3d";
//AssetBundle ab2 = AssetBundle.LoadFromFile("AssetBundles/shear.3d");
//AssetBundle ab1 = AssetBundle.LoadFromFile("AssetBundles/wall.unity3d");
//GameObject wallPrefab = ab1.LoadAsset<GameObject>("Cube");
//Instantiate(wallPrefab);
//第一种加载方式 LoadFromMemoryAsync,异步加载需要转换为bytes数组,返回request
//AssetBundleCreateRequest request = AssetBundle.LoadFromMemoryAsync(File.ReadAllBytes(path));
//yield return request;
//AssetBundle ab = request.assetBundle;
//第二种方式 ,LoadFromMemory 同步加载,直接返回AssetBuandle
//AssetBundle ab = AssetBundle.LoadFromMemory(File.ReadAllBytes(path));
//第三种LoadFromFile 从本地文件加载
//AssetBundleCreateRequest request = AssetBundle.LoadFromFileAsync(path);
//yield return request;
//AssetBundle ab = request.assetBundle;
//第四种使用方式UnityWebRequest
string uri = "http://localhost/AssetBundles/wall.unity3d";//修改地址即可
UnityWebRequest request = UnityWebRequestAssetBundle.GetAssetBundle(uri);
yield return request.SendWebRequest();
AssetBundle ab = DownloadHandlerAssetBundle.GetContent(request);
//www,加载方式
//while (Caching.ready == false) {
// yield return null;
//}
//WWW www = WWW.LoadFromCacheOrDownload(@uri,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>("Cube");
Instantiate(wallPrefab);
AssetBundle manifestAB = AssetBundle.LoadFromFile("AssetBundles/AssetBundles");
AssetBundleManifest manifest = manifestAB.LoadAsset<AssetBundleManifest>("AssetBundleManifest");
string[] st = manifest.GetAllDependencies("wall.unity3d");//获取所有依赖的包名
foreach(string i in st) {
AssetBundle.LoadFromFile("AssetBundles/" + i);
}
}
}