Unity AssetBundle详解,加载本地包、加载网络包代码全分享

在Unity中,AssetBundle(简称AB包)是一种将多个文件或资源打包到一个文件中的方式,用于优化资源的加载和管理。使用AB包,可以按需加载资源,减少应用的初始加载时间,并可以实现热更新等功能。下面是一个基本的流程,展示如何在Unity中加载AB包并显示其中的资源。

步骤1:创建和构建AssetBundle

  • 标记资源:在Unity编辑器中,选择你想打包的资源(如Texture、Prefab等),在Inspector面板中,找到"Asset Bundle"选项,为资源设置一个AssetBundle标签。
    在这里插入图片描述

  • 构建AssetBundle:编写脚本或使用Unity编辑器的AssetBundle Browser工具来构建AssetBundle。构建时可以选择不同的平台作为目标,比如Windows、Android等。

  • 构建的AssetBundle会默认放到StreamingAssets文件里

步骤2:将AssetBundle放置到可访问的路径

构建完成后,将生成的AB包放置到项目的StreamingAssets文件夹中,或者上传到Web服务器,以便运行时加载。

步骤3:编写代码加载AssetBundle并使用其中的资源

示例1:加载StreamingAssets文件夹里

假设打包的文件在StreamingAssets文件夹下windows目录里,Asset Name处填入步骤1中设置的AssetBundle标签名称
在这里插入图片描述

using System.Collections;
using UnityEngine;
using UnityEngine.Networking;

public class LoadAssetBundle : MonoBehaviour
{
    public string assetName = "";
   
    private string assetBundleName = "";
    private string prefabName = "";

    void Start()
    {
        assetBundleName = assetName + ".unity3d";
        string bundleUrl = System.IO.Path.Combine(Application.streamingAssetsPath, "windows", assetBundleName);
        print(bundleUrl);
        bundleUrl = "file://" + bundleUrl; // 确保路径以file://开头
        StartCoroutine(LoadAssetBundleCoroutine(bundleUrl));
     
    }

    IEnumerator LoadAssetBundleCoroutine(string bundleUrl)
    {
        // 使用UnityWebRequest获取AssetBundle
        using (UnityWebRequest uwr = UnityWebRequestAssetBundle.GetAssetBundle(bundleUrl))
        {
            yield return uwr.SendWebRequest();

            if (uwr.result != UnityWebRequest.Result.Success)
            {
                Debug.LogError("AssetBundle加载失败: " + uwr.error);
            }
            else
            {
                // 获取下载的AssetBundle
                AssetBundle bundle = DownloadHandlerAssetBundle.GetContent(uwr);
                if (bundle != null)
                {
                    Debug.Log("AssetBundle加载成功!");
                    // 获取并打印所有资源的名称
                    string[] assetNames = bundle.GetAllAssetNames();
                    foreach (string assetName in assetNames)
                    {
                        Debug.Log("AssetBundle包含资源: " + assetName);
                        // 加载每个对象
                        Object asset = bundle.LoadAsset(assetName);
                        if (asset is GameObject)
                        {
                            // 如果对象是GameObject(例如Prefab),则实例化它
                            Instantiate(asset as GameObject);
                        }
                        else
                        {
                            // 如果对象不是GameObject,可以在这里处理(例如打印日志)
                            Debug.Log("非GameObject资源,跳过实例化: " + assetName);
                        }
                    }
                    //实例化Prefab
                   
                    // 使用完毕后,记得卸载AssetBundle
                    bundle.Unload(false);
                }
                else
                {
                    Debug.LogError("AssetBundle加载失败!");
                }
            }
        }
    }
}

示例2:加载AssetBundle从网络

把AssetBundle包放到服务器上,再从服务器下载AssetBundle
在这里插入图片描述

using System.Collections;
using UnityEngine;
using UnityEngine.Networking;

public class LoadAssetBundleFromUrl : MonoBehaviour
{
    public string assetBundleURL = "";

    private string assetBundleName = "";
    private string prefabName = "";

    void Start()
    {
       
      
        StartCoroutine(LoadAssetBundleCoroutine(assetBundleURL));
     
    }

    IEnumerator LoadAssetBundleCoroutine(string bundleUrl)
    {
        print(bundleUrl);
        // 使用UnityWebRequest获取AssetBundle
        using (UnityWebRequest uwr = UnityWebRequestAssetBundle.GetAssetBundle(bundleUrl))
        {
            yield return uwr.SendWebRequest();

            if (uwr.result != UnityWebRequest.Result.Success)
            {
                Debug.LogError("AssetBundle加载失败: " + uwr.error);
            }
            else
            {
                // 获取下载的AssetBundle
                AssetBundle bundle = DownloadHandlerAssetBundle.GetContent(uwr);
                if (bundle != null)
                {
                    Debug.Log("AssetBundle加载成功!");
                    // 获取并打印所有资源的名称
                    string[] assetNames = bundle.GetAllAssetNames();
                    foreach (string assetName in assetNames)
                    {
                        Debug.Log("AssetBundle包含资源: " + assetName);
                        // 加载每个对象
                        Object asset = bundle.LoadAsset(assetName);
                        if (asset is GameObject)
                        {
                            // 如果对象是GameObject(例如Prefab),则实例化它
                            Instantiate(asset as GameObject);
                        }
                        else
                        {
                            // 如果对象不是GameObject,可以在这里处理(例如打印日志)
                            Debug.Log("非GameObject资源,跳过实例化: " + assetName);
                        }
                    }
                    //实例化Prefab
                   
                    // 使用完毕后,记得卸载AssetBundle
                    bundle.Unload(false);
                }
                else
                {
                    Debug.LogError("AssetBundle加载失败!");
                }
            }
        }
    }
}
  • 10
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小~小

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值