【Unity】生成AB包,加载AB包
创始人
2025-01-09 08:34:14
0

一、描述

1、Unity中可以将模型、地形、动画、贴图、shader等之类的资源加载成 AssetBundle  2、AssetBundle 是一种资源压缩包 3、使用AB包可以减小安装包的大小,资源可以在进入游戏的初始时进行网络下载或者更新 4、具体的AssetBundle 相关细节知识可百度查询,这里边我只介绍怎么用代码实现生成AB包和加载AB包 

二、生成 AssetBundle(AB) 包

  • 首先,将模型或者地形拖成prefab,动画、shader、UI等资源不需要拖成prefab
  • 选中需要打包的资源,点击unity菜单栏——Tools——打包工具
  • 弹出打包界面后选择【打包】按钮即可,最终生成.p的AB包,位置在【StreamingAssets/Dabao/】
    请添加图片描述
    请添加图片描述

三、加载 AssetBundle(AB) 包

加载AB包之前
如果是本地加载,则可以把需要加载的AB包提前放在指定文件夹中(需要和代码中设置的路径一致);
如果是远端加载,则可以直接从远端下载到本地,然后再加载
视情况而定,本文用的是直接从本地文件夹中加载

测试脚本【TestLoadModel.cs】

using System.Collections; using System.Collections.Generic; using UnityEngine; using System;  public class TestLoadModel : MonoBehaviour {          ///      /// 所加载的AB的父物体     /// 可根据自己的项目情况调整     ///      public GameObject equipParent;          void Start()     {         LoadMode("JDModel",LoadOverStartEvent);         //LoadMode("XXX路", "XXX装备", LoadOverStartEvent);     }      ///      /// 加载建筑物     ///      ///      ///      private void LoadMode(string buildName, Action finish)     {         int index = 1;         ResourcesLoadABManager.LoadAssetBundlesAnimAsync(buildName, "", BundleType.Build, (assetBundleObject) =>         {             GameObject obj = null;             if (assetBundleObject != null)             {                 AssetBundle assetBundle = assetBundleObject.assetBundleCreateRequest.assetBundle;                 string[] strs = assetBundleObject.assetBundleCreateRequest.assetBundle.GetAllAssetNames();                 obj = Instantiate(assetBundle.LoadAsset(strs[0]));                 obj.transform.parent = equipParent.transform;                 index--;                 if (index == 0)                 {                     finish?.Invoke();                 }             }         });     }               ///      /// 加载模型、地形     ///      ///      ///      ///      private void LoadMode(string terrainName, string equipName, Action finish)     {         int index = 2;         ResourcesLoadABManager.LoadAssetBundlesAnimAsync(terrainName, "", BundleType.Terrain,             (assetBundleObject) =>             {                 Debug.LogError("加载场景中......");                 Debug.LogError("场景名称" + terrainName);                 GameObject obj1 = null;                 if (assetBundleObject != null)                 {                     AssetBundle assetBundle = assetBundleObject.assetBundleCreateRequest.assetBundle;                     string[] strs = assetBundleObject.assetBundleCreateRequest.assetBundle.GetAllAssetNames();                     obj1 = Instantiate(assetBundle.LoadAsset(strs[0]));                     index--;                     if (index == 0)                     {                         finish?.Invoke();                     }                 }             });          ResourcesLoadABManager.LoadAssetBundlesAnimAsync(equipName, "", BundleType.Equip,             (assetBundleObject) =>             {                 Debug.LogError("加载模型中......");                 Debug.LogError("模型名称" + equipName);                 GameObject obj = null;                 if (assetBundleObject != null)                 {                     AssetBundle assetBundle = assetBundleObject.assetBundleCreateRequest.assetBundle;                     string[] strs = assetBundleObject.assetBundleCreateRequest.assetBundle.GetAllAssetNames();                     obj = Instantiate(assetBundle.LoadAsset(strs[0]));                     obj.transform.parent = equipParent.transform;                      Vector3 equip_position = new Vector3(-326.505f, 37.688f, 1875.078f);                     Vector3 rotation = new Vector3(0f, 353.297f, 0f);                                          obj.transform.position = equip_position;                     obj.transform.eulerAngles = rotation;                                          index--;                     if (index == 0)                     {                         finish?.Invoke();                     }                 }             });     }      ///      /// 加载AB包后开始执行事件     ///      private void LoadOverStartEvent()     {         Debug.LogError("加载AB包完成,开始事件......");         // TODO...              } } 

相关链接

链接:https://pan.baidu.com/s/1a-dKySaisWP2zDTl9pmFIw?pwd=0q58
提取码:0q58
–来自百度网盘超级会员V7的分享

链接:https://pan.baidu.com/s/1FUJMMUIyfUJPiGOpChUcuw?pwd=g3m4
提取码:g3m4
–来自百度网盘超级会员V7的分享

相关内容

热门资讯

第九分钟晓得!we-poker... 第九分钟晓得!we-poker有人玩吗,wepoker模拟器哪个好用(透视)2025新版技巧(有挂功...
重大科普!德扑之星有作弊,德扑... 重大科普!德扑之星有作弊,德扑ai软件购买,2025新版技巧(了解有挂)-哔哩哔哩;德扑之星有作弊是...
第2分钟领会!wepoker脚... 第2分钟领会!wepoker脚本,约局吧辅助器(透视)攻略教程(果真有挂)1、约局吧辅助器透视辅助简...
安装程序教程!微扑克ai机器人... 安装程序教程!微扑克ai机器人打德州,德扑ai软件,技巧教程(有挂分享)-哔哩哔哩;1、超多福利:超...
第1分钟了解!wejoker辅... 第1分钟了解!wejoker辅助器怎么卖,wepoker公共底牌(透视)微扑克教程(揭秘有挂)1、在...
科技介绍!wepoke有假,微... 科技介绍!wepoke有假,微扑克软件发牌管理,AI教程(揭秘有挂)-哔哩哔哩;1、不需要AI权限,...
第2分钟俱乐部!hhpoker... 第2分钟俱乐部!hhpoker辅助软件下载,wejoker辅助软件视频(透视)2025新版总结(有挂...
热点推荐!nzt德州辅助软件,... 热点推荐!nzt德州辅助软件,智星菠萝德州辅助工具,可靠技巧(有挂详细)-哔哩哔哩;是一款可以让一直...
7分钟了解!epoker透视,... 7分钟了解!epoker透视,hhpoker真的假的(透视)2025新版教程(有挂教学)1、hhpo...
总算了解!德州ai辅助有用,微... 总算了解!德州ai辅助有用,微扑克发牌机制,安装教程(详细教程)-哔哩哔哩;微扑克发牌机制是一种具有...