Unity3D资源异步加载——AssetBundle资源打包加载

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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使用。

以上是关于Unity3D资源异步加载——AssetBundle资源打包加载的主要内容,如果未能解决你的问题,请参考以下文章

Unity3D资源异步加载——图片音视频资源加载

Unity3D研究院之加密Assetbundle不占内存(一百零五)

Unity3d C# 实现AA包(Addressables)资源热更新的多个包异步加载并显示加载实时进度功能(含源码)

Unity资源加载与释放

unity3d 异步加载场景代码

unity3d怎么实现异步加载场景