阅读量: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 { /// <summary> /// 所加载的AB的父物体 /// 可根据自己的项目情况调整 /// </summary> public GameObject equipParent; void Start() { LoadMode("JDModel",LoadOverStartEvent); //LoadMode("XXX路", "XXX装备", LoadOverStartEvent); } /// <summary> /// 加载建筑物 /// </summary> /// <param name="buildName"></param> /// <param name="finish"></param> 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<GameObject>(strs[0])); obj.transform.parent = equipParent.transform; index--; if (index == 0) { finish?.Invoke(); } } }); } /// <summary> /// 加载模型、地形 /// </summary> /// <param name="terrainName"></param> /// <param name="equipName"></param> /// <param name="finish"></param> 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<GameObject>(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<GameObject>(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(); } } }); } /// <summary> /// 加载AB包后开始执行事件 /// </summary> 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的分享