Unity3D資源異步加載(二)——AssetBundle資源打包、加載

AssetBundle就像一個ZIP壓縮文件,裏面存儲着不一樣平臺的特殊資源(models/texture/prefabs/materials/audio clip/scenes...), 這些資源均可以在運行時進行加載。
上一章介紹了,資源異步加載的方法,這篇介紹AssetBundle資源舊的打包加載方法;
AssetBundle打包:異步

using UnityEngine;
using System.Collections;
using UnityEditor;
using System.IO;

public class AssetbundlePack : MonoBehaviour {

    /// <summary>
    /// 查看全部的Assetbundle名稱
    /// </summary>
    [MenuItem("AssetBundle/Get AssetBundle names")]
    static void GetAllAssetbundleNames()
    {
        var names = AssetDatabase.GetAllAssetBundleNames(); //獲取全部設置的AssetBundle
        foreach (var name in names)
            Debug.Log("AssetBundle: " + name);
    }

    /// <summary>
    /// 自動打包全部資源
    /// </summary>
    [MenuItem("AssetBundle/Create AssetBundles")] //設置編輯器菜單選項
    static void CreateAllAssetBundles()
    {
        string targetPath = Application.dataPath + "/StreamingAssets/PCAssetsResources/";
        if(!Directory.Exists(targetPath))
        {
            Directory.CreateDirectory(targetPath);
        }
        //打包資源
                ////參數一爲打包到哪一個路徑,參數二壓縮選項  參數三 平臺的目標
        BuildPipeline.BuildAssetBundles(targetPath, BuildAssetBundleOptions.None, BuildTarget.StandaloneWindows);
        //刷新編輯器
        AssetDatabase.Refresh();
    }

    /// <summary>
    /// 清除以前設置過的AssetBundleName,避免產生沒必要要的資源也打包 
    /// 由於只要設置了AssetBundleName的,都會進行打包,不論在什麼目錄下 
    /// </summary> 
    [MenuItem("AssetBundle/Clear All Assetbundle Name")]
    public static void ClearAssetBundlesName()
    {
        string[] oldAssetBundleNames = AssetDatabase.GetAllAssetBundleNames();
        for (int j = 0; j < oldAssetBundleNames.Length; j++)
        {
            AssetDatabase.RemoveAssetBundleName(oldAssetBundleNames[j], true);
        }
    }
}

異步加載AssetBundle編輯器

/// <summary>
    /// 請求AssetBundle
    /// </summary>
    /// <param name="url">AssetBundle地址</param>
    /// <param name="actionResult">請求發起後處理回調結果的委託,處理請求結果的AssetBundle</param>
    /// <returns></returns>
    IEnumerator _LoadAssetBundle(string url, Action<bool, string, AssetBundle> actionResult)
    {
         using (UnityWebRequest request = UnityWebRequest.Get(url))
        {
            DownloadHandlerAssetBundle downloadAssetBundle = new DownloadHandlerAssetBundle(url, uint.MaxValue);
            request.downloadHandler = downloadAssetBundle;
            yield return request.SendWebRequest();
            AssetBundle assetBundle = null;
            string text = null;
            if (!(request.isNetworkError || request.isHttpError))
            {
                assetBundle = downloadAssetBundle.assetBundle;
            }
            else
            {
                text = request.error;
            }
            if (actionResult != null)
            {
                actionResult.Invoke((request.isNetworkError || request.isHttpError), text, assetBundle);
            }
        }
    }

AssetBundle舊方法打包加載就介紹到這裏,自2018版Unity發佈以後出現新的資源管理方式Addressable,不過舊的依然能用,我的以爲新Addressable方式用起來方便的多,Addressable也是基於AssetBundle之上,下一篇主要介紹Addressable使用。ide

相關文章
相關標籤/搜索