Unity5.X打包與加載AssetBundle

所有腳本代碼來自官方

打包AssetBundle:

在Editor界面,點擊prefab或文件,在AssetsBundle處設置此prefab的Assetsbundles屬性。
點New可以創建AssetBundle名稱,輸入“/”可設置下一級名稱。
多個物體可設爲同一個AssetBundle名稱。
這裏寫圖片描述

添加一個腳本,給Editor增加一個打包的功能:

using UnityEditor;

public class CreateAssetBundles
{
    [MenuItem ("Assets/Build AssetBundles")]
    static void BuildAllAssetBundles ()
    {
        BuildPipeline.BuildAssetBundles ("Assets/AssetBundles", BuildAssetBundleOptions.None, BuildTarget.StandaloneOSXUniversal);
    }
}

其中”AssetBundles”爲需要手工添加的文件夾名稱。

在頂部Assets目錄中的底部會出現Build Assetbundles選項。點擊後會開始build進程。
這裏寫圖片描述

AssetBundles目錄中會出現一個AssetBundles文件(作用未知)和一個AssetBundles.manifest文件,manifest文件裏寫明瞭各AssetBundles的關係。

如果之前設置的Assetbundle屬性有多個層級,則AssetBundles文件夾中會出現相應的下一級文件夾,AssetBundle文件以及它對應的manifest文件。

加載AssetBundle:

有兩種方式
1,下載但不儲存到緩存中

using System;
using UnityEngine;
using System.Collections; class NonCachingLoadExample : MonoBehaviour {
    public string BundleURL;
    public string AssetName;
    IEnumerator Start() {
        // Download the file from the URL. It will not be saved in the Cache
        using (WWW www = new WWW(BundleURL)) {
            yield return www;
            if (www.error != null)
                throw new Exception("WWW download had an error:" + www.error);
            AssetBundle bundle = www.assetBundle;
            if (AssetName == "")
                Instantiate(bundle.mainAsset);
            else
                Instantiate(bundle.LoadAsset(AssetName));
            // Unload the AssetBundles compressed contents to conserve memory
            bundle.Unload(false);

        } // memory is freed from the web stream (www.Dispose() gets called implicitly)
    }
}

2,下載並儲存到緩存中(推薦)

using System;
using UnityEngine;
using System.Collections;

public class CachingLoadExample : MonoBehaviour {
    public string BundleURL;
    public string AssetName;
    public int version;

    void Start() {
        BundleURL="file://"+Application.dataPath+BundleURL;
        StartCoroutine (DownloadAndCache());
    }

    IEnumerator DownloadAndCache (){
        // Wait for the Caching system to be ready
        while (!Caching.ready)
            yield return null;

        // Load the AssetBundle file from Cache if it exists with the same version or download and store it in the cache
        using(WWW www = WWW.LoadFromCacheOrDownload (BundleURL, version)){
            yield return www;
            if (www.error != null)
                throw new Exception("WWW download had an error:" + www.error);
            AssetBundle bundle = www.assetBundle;
            if (AssetName == "")
                Instantiate(bundle.mainAsset);
            else
//              Instantiate(bundle.mainAsset);

//              Instantiate(bundle.LoadAsset(AssetName));
                foreach(UnityEngine.Object temp in bundle.LoadAllAssets())
                {
                    Instantiate(temp);
                }
    //      Instantiate(bundle.LoadAllAssets());
            // Unload the AssetBundles compressed contents to conserve memory
            bundle.Unload(false);

        } // memory is freed from the web stream (www.Dispose() gets called implicitly)
    }
}

加載腳本中兩個變量的說明:
-變量BundleURL的格式:
–如果爲UnityEditor本地:
—“file://”+“application.datapath”+“/文件夾名稱/文件名”
–如果爲服務器:
—http://….

-變量AssetName:
–爲prefab或文件的名字。

如果要同時實例化所有AssetBundle中的物體(如果都是prefab),可用LoadAllAssets()方法,例如:

foreach(UnityEngine.Object temp in bundle.LoadAllAssets())
{
    Instantiate(temp);
}

這樣,之前被打包進AssetBundle內的prefab就會出現在場景中了。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章