AssetBundle
AssetBundle是Unity用戶存儲資源的一種壓縮格式的打包集合,他可以存任意一種Unity引擎可以識別的資源: 模型,音頻,紋理圖,動畫, 開發者自定義的二進制文件; 安裝包小,更新資源;
AssetBundle開發步驟
(1): 創建AssetBundle: 項目的資源打包AssetBundle的集合裏面;
(2): 部署到web服務器, 讓客戶端下載我們的AssetBundle;
(3): 加載AssetBundle, 加載裏面的資源;
(4): 卸載AssetBundle, 壓縮包,鏡像;
AssetBundle創建
1:Assets窗口的資源纔可以打包;
2: 創建一個AssetBundle文件,它的名字固定式小寫如果有大寫系統也會換成小寫;
3: AssetBundle可以設置一個Varaint,就是一個後綴。可以通過後綴來設置不同分辨率的資源;
4: 將一個資源打入到AssetsBundle: 點擊資源,選擇對應的AssetBundle就可以了;
5: 編寫代碼導出AssetBundle文件,調用Unity的Api: BuildPipeline.BuildAssetBundles(outpath, BuildAssetBundleOptions, BuildTarget); 文件夾的路徑需要手動創建,否者會報錯;
public class export_assetbundle : Editor {
[MenuItem("build/build_assetbundle")]
static void run() {
// 調用函數來打包asset bundle;
// 輸出路徑: "Assets/AssetBundles", 手動創建好
BuildPipeline.BuildAssetBundles("Assets/AssetBundles", BuildAssetBundleOptions.None, BuildTarget.StandaloneWindows64);
// end
/* 第二種打包方式
AssetBundleBuild[] buildMap = new AssetBundleBuild[2]; //定義AssetBuild數組
buildMap[0].assetBundleName = "resources"; //打包的資源包名稱,開發者可以隨便命名
string[] resourcesAssets = new string[2]; //定義字符串,用來記錄此資源包文件名稱
resourcesAssets[0] = "resources/1.prefab"; //將需要打包的資源名稱賦給數組
resourcesAssets[1] = "resources/MainO.cs";
buildMap[0].assetNames = resourcesAssets; //將資源名稱數組賦給AssetBuild
BuildPipeline.BuildAssetBundles("Assets/AssetBundles", buildMap, BuildAssetBundleOptions.None, BuildTarget.StandaloneWindows64)
*/
}
}
這裏包含了資源的引用和一些基本信息。
AssetBundle下載
把我們打包好的資源放到web服務器上,服務器提供一個URL,用WWW實例下載AssetBundle
1:非緩衝下載: 創建一個WWW的實例來下載AssetBundle;
(1)使用協程下載AssetBundle,
(2) 使用WWW的URL接口來下載;
2: 緩衝下載:使用WWW類的LoadFromCacheOrDownload來實現下載AssetBundle, 當再次下載的時候,只有當版本低或不存在的時候才下載;web平臺緩衝的大小是50M, IOS/android緩衝的大小爲4GB;
using UnityEngine;
using System.Collections;
public class game_scene : MonoBehaviour {
public string url;
public int version; // 自己控制的,
// Use this for initialization
void Start () {
// this.StartCoroutine(this.nocache_load());
this.StartCoroutine(this.cache_load());
}
//下載assetbundle,不緩存在本地
IEnumerator nocache_load() {
WWW w = new WWW(this.url);
yield return w;
// 下載完成;
Debug.Log("download end");
if (w.error != null) {
Debug.Log(w.error);
}
// 內存鏡像, assetbundle,
AssetBundle bundle = w.assetBundle;
Debug.Log("download success...");
// end
// 使用裏面的資源
Object prefab = bundle.LoadAsset("Assets/res/Cube.prefab");
GameObject obj = (GameObject)Instantiate(prefab);
obj.transform.parent = this.transform;
// end
// 協助AssetBundle鏡像, 壓縮鏡像
bundle.Unload(false); // 只會卸載assetbundle內存鏡像
// bundle.Unload(true); // 會卸載內存鏡像,也會釋放掉從這個assetbunle裏面加載起來的資源;
// end
}
IEnumerator cache_load() {
WWW w = WWW.LoadFromCacheOrDownload(this.url, this.version);
yield return w;
// 下載完成;
Debug.Log("download end");
if (w.error != null) {
Debug.Log(w.error);
}
// 內存鏡像, assetbundle,
AssetBundle bundle = w.assetBundle;
Debug.Log("download success...");
// end
// 使用裏面的資源
Object prefab = bundle.LoadAsset("Assets/res/Cube.prefab");
GameObject obj = (GameObject)Instantiate(prefab);
obj.transform.parent = this.transform;
// end
// 協助AssetBundle鏡像, 壓縮鏡像
bundle.Unload(false); // 只會卸載assetbundle內存鏡像
// bundle.Unload(true); // 會卸載內存鏡像,也會釋放掉從這個assetbunle裏面加載起來的資源;
// end
}
// 下載assetbundle, nocache, cache, version
// end
// Update is called once per frame
void Update () {
}
}
簡單說明一些:
AssetBundle加載
1:AssetBundle.LoadAsset(路徑); // 資源所在的路徑名字
3: AssetBundle.LoadAllAsset()加載所有的資源
AssetBundle卸載
1:AssetBoundle.Unload(false); false: 卸載內存鏡像不卸載Asset內存實例;
2:AssetBoundle.Unload(true); true: 卸載內存鏡像以及Asset的內存實例;
3: 不能使用WWW對象去下載一個已經被加載進來的AssetBundle;