1 AssetBundle
AssetBundle 是Unity提供的一個功能禁悠,可以把資源(包括預設(shè)氧吐、模型、貼圖等)壓縮成一個資源包壁顶,可以應到到游戲的熱更新上。
2 AssetBundel 打包
Unity5 簡化了AssetBundle的打包步驟溜歪,只需要給每一個需要打包的資源定義報名若专,然后通過BuildPipeline的API,就可以導出一個資源包蝴猪。
Step1 新建一個Cube對象调衰,把它制作成預設(shè)膊爪,在資源預覽窗口中設(shè)置Cube的名稱和變量。參考上圖嚎莉,ab-cube 是 Cube 對象的名稱米酬,a 是資源名變量。 最后通過 ab-cube.a 的定位這個資源包趋箩。
Step2 使用編輯器腳本打包資源赃额。在項目中新建一個Editor的文件夾,新建一個腳本AssetBundleBuilder叫确。這個腳本在工程中創(chuàng)建了一個目錄跳芳,然后把做了AssetBundle 打包標記的的資源進行打包處理。執(zhí)行菜單-> Assets -> BuildAssetBundle ,Unity 就會開始進行打包進程竹勉。
using UnityEngine;
using System.Collections;
using UnityEditor;
using System.IO;
public class AssetBundleBuilder
{
[MenuItem("Assets/Build AssetBundle")]
static public void BuildAssetBundle()
{
Caching.CleanCache ();
string path = Application.streamingAssetsPath + "/" + "AssetBundles" + "/" + "OSX";
if (!Directory.Exists (path))
{
Directory.CreateDirectory(path);
}
BuildPipeline.BuildAssetBundles (path,0,EditorUserBuildSettings.activeBuildTarget);
AssetDatabase.Refresh ();
}
}
打包進程結(jié)束后的AssetBundle 文件飞盆。結(jié)果有4個文件,其中兩個是.manifest 文件次乓,兩個是資源包文件吓歇。 manifest 包含了資源的依賴項和資源的打包說明,OSX是總包票腰,ab-cube.a 才是包含Cube 的資源包城看。
3 從本地加載AssetBundle
開發(fā)的時候可以先從本地加載AssetBundle,真正發(fā)行游戲的時候才做網(wǎng)絡(luò)熱更新加載,這樣會比較方便杏慰。
新建一個腳本,把腳本綁定在一個GameObject 上析命,點擊運行。 Cube 預設(shè)就會從AssetBundle 中加載出來逃默。
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class LoadAssetBundle : MonoBehaviour {
public string url;
public int version;
public string assetName;
public string assetBundleName;
string manifestName;
void Start ()
{
InitializeAssetBundlePath ();
StartCoroutine(LoadAssetBundleAsObject());
}
void InitializeAssetBundlePath()
{
#if UNITY_EDITOR
url = "file://" + Application.streamingAssetsPath+"/AssetBundles/OSX/";
assetBundleName ="ab-cube.a";
assetName="Cube";
#endif
}
public IEnumerator LoadAssetBundleAsObject()
{
WWW www2 = new WWW (url + assetBundleName);
yield return www2;
if (!string.IsNullOrEmpty (www2.error)) {
Debug.Log (www2.error);
} else {
AssetBundle ab = www2.assetBundle;
GameObject gobj = ab.LoadAsset (assetName) as GameObject;
Instantiate (gobj);
}
www2.Dispose ();
}
}
4 從網(wǎng)絡(luò)中加載AssetBundle
Unity的資源熱更新需要把AssetBundle的資源包上傳到網(wǎng)絡(luò),然后再通過WWW的接口下載到本地簇搅。這里使用的Mac系統(tǒng)自帶的apache,可以參考Mac OS X 上的Apache配置 完域。
step1 把資源包ab-cube.a (只需要這一個文件) 放到apache的根目錄中,可以在瀏覽器中通過網(wǎng)址 http://127.0.0.1/ab-cube.a 來訪問這個資源包瘩将。
step2 新建一個腳本并綁定在場景中吟税。
using UnityEngine;
using System.Collections;
public class WebLoadAsset : MonoBehaviour {
// Use this for initialization
void Start () {
StartCoroutine (LoadAssets());
}
IEnumerator LoadAssets()
{
WWW w = WWW.LoadFromCacheOrDownload ("http://127.0.0.1/ab-cube.a",0);
yield return w;
AssetBundle ab = w.assetBundle;
GameObject go= ab.LoadAsset<GameObject> ("Cube");
Instantiate (go);
}
}
PS
1 不同平臺的AssetBundle包是不兼容的,android的資源包只能用于android平臺姿现,ios的資源包只能用在ios平臺肠仪。
2 windows和mac 加載資源包時,文件路徑前需要添加 "file://"备典。例如 url = "file://" + Application.streamingAssetsPath+"/AssetBundles/OSX/";
3 要注意資源的依賴關(guān)系异旧,公用的資源盡量獨立處理,以免重復打包提佣。
4 以上是簡單示例吮蛹,沒有做錯誤處理荤崇。