当前位置: 首页 > news >正文

【Unity】AssetBundle热更新

1.新建两个预制体:

Cube1:GameObject

Material1:Material

Cube1使用了Material1材质

之后设置打包配置

Cube1的打包配置为custom.ab

Material1的打包配置为mat.ab

2.在Asset文件夹下创建Editor文件夹,并在Editor下创建BuildBundles.cs

using System.IO;

using UnityEditor;

using UnityEngine;

public class BuildBundles : Editor

{

[MenuItem("Custom/BuildBundles")]

static void BuildBundle()

{

//参数(打包路径、选项为None、使用平台)

BuildPipeline.BuildAssetBundles(Path.Combine(Application.dataPath + "/AssetBundle"), BuildAssetBundleOptions.None, BuildTarget.StandaloneWindows64);

}

}

创建完成后,可在工具栏看到Custom/BuildBundles选项

3.将预制体打包成AssetBundle文件

在Asset文件夹下创建AssetBundle文件夹

之后点击工具栏的Custom/BuildBundles,AssetBundle文件被生成至AssetBundle文件夹中

AssetBundle是本次生成的所有的AssetBundle文件的配置文件,其中的AssetBundleManifest属性描述了文件的信息和依赖关系

custom.ab为Cube1的配置信息,上面也说明了其与mat.ab有依赖关系

以下是mat.ab的配置信息

4.获取AssetBundle文件并编译至场景中

新建AssetBundleTest.cs,并挂载至场景内任何一物体中

using System.Collections;

using System.IO;

using UnityEngine;

using UnityEngine.Networking;

public class AssetBundleTest : MonoBehaviour

{

// Start is called before the first frame update

void Start()

{

//loadMat();//同步加载(依赖)

//loadCube();//同步加载(Cube)

StartCoroutine(LoadCubeAsync());//异步加载

//StartCoroutine(LoadCubeWWW());//从服务端获取

}

//加载依赖(同步)

void loadMat()

{

//加载依赖

//读取AssetBundle下名称为AssetBundle的文件

string path = Application.dataPath + "/AssetBundle/AssetBundle";

AssetBundle assetBundle = AssetBundle.LoadFromFile(path);

//加载该文件夹里的配置信息

AssetBundleManifest assetBundleManifest = assetBundle.LoadAsset<AssetBundleManifest>("AssetBundleManifest");

//加载名为custom.ab的文件的所有依赖并转为字符串数组

string[] strs = assetBundleManifest.GetAllDependencies("custom.ab");

foreach(string name in strs)

{

//加载出需要用到的AssetBundle文件

AssetBundle.LoadFromFile(Application.dataPath + "/AssetBundle/" + name);

}

}

//加载Cube(同步)

void loadCube()

{

string Cubepath = Path.Combine(Application.dataPath + "/AssetBundle/custom.ab");

AssetBundle ab = AssetBundle.LoadFromFile(Cubepath);

GameObject CubePre = ab.LoadAsset<GameObject>("Cube");

Instantiate(CubePre);

}

//异步加载Cube

private IEnumerator LoadCubeAsync()

{

//加载依赖

//读取AssetBundle下名称为AssetBundle的文件

string path1 = Application.dataPath + "/AssetBundle/AssetBundle";

AssetBundleCreateRequest assetBundle1 = AssetBundle.LoadFromFileAsync(path1);

yield return assetBundle1;

//加载该文件夹里的配置信息

AssetBundleManifest assetBundleManifest = assetBundle1.assetBundle.LoadAsset<AssetBundleManifest>("AssetBundleManifest");

//加载名为custom.ab的文件的所有依赖并转为字符串数组

string[] strs = assetBundleManifest.GetAllDependencies("custom.ab");

foreach (string name in strs)

{

//加载出需要用到的AssetBundle文件

AssetBundle.LoadFromFile(Application.dataPath + "/AssetBundle/" + name);

}

//加载Cube

//读取custom.ab文件

string path = Application.dataPath + "/AssetBundle/custom.ab";

//string path4 = Path.Combine(Application.dataPath, "AssetBundle", "custom.ab");//使用Path.Combine拼接路径

AssetBundleCreateRequest assetBundleCreateRequest = AssetBundle.LoadFromFileAsync(path);

//异步等待加载完成

yield return assetBundleCreateRequest;

//加载完成,之后在场景中生成出来

AssetBundle assetBundle = assetBundleCreateRequest.assetBundle;

GameObject CubePre = assetBundle.LoadAsset<GameObject>("Cube1");

Instantiate(CubePre);

}

private IEnumerator LoadCubeWWW()

{

//从服务端获取AssetBundle文件

//获取路径并通过UnityWebRequest获取指定文件

string path1 = Path.Combine("http://192.168.0.102/web/custom.ab");

UnityWebRequest unityWebRequest = UnityWebRequest.Get(path1);

//等待文件获取完成

yield return unityWebRequest.SendWebRequest();

//加载完成

AssetBundle assetBundle = DownloadHandlerAssetBundle.GetContent(unityWebRequest);

GameObject CubePre = assetBundle.LoadAsset<GameObject>("Cube");

Instantiate(CubePre);

}

}

http://www.xdnf.cn/news/288955.html

相关文章:

  • Java中线程间数据共享:ThreadLocal与ScopedValue
  • 二、【LLaMA-Factory实战】数据工程全流程:从格式规范到高质量数据集构建
  • Qt 显示QRegExp 和 QtXml 不存在问题
  • 线程池配置不合理:系统性能的隐形杀手(深度解析版)
  • Python基本环境搭配
  • 代码随想录第32天:动态规划5(组合、排列、最小方法数)
  • 二、Python变量基础(2)
  • STM32 PulseSensor心跳传感器驱动代码
  • 常用非对称加密算法的Python实现及详解
  • simulink使能子系统的四种配置
  • uniapp开发06-视频组件video的使用注意事项
  • 大数据分析在视频监视方面的应用综述
  • ROS2 开发踩坑记录(持续更新...)
  • Serverless
  • 机器学习项目流程极简入门:从数据到部署的完整指南
  • 物联网mqtt和互联网http协议区别
  • 硬件工程师面试常见问题(14)
  • [学习] RTKlib详解:功能、工具与源码结构解析
  • 基于MATLAB的图像色彩识别项目,彩色图像矩阵识别
  • 大模型推理--从零搭建大模型推理服务器:硬件选购、Ubuntu双系统安装与环境配置
  • Python实战:基于控制台与MySQL的电影票预订系统开发指南
  • 学习路线(机器人系统)
  • 模糊控制理论(含仿真)
  • 7400MB/s5050TBW完美结合,全新希捷酷玩530R SSD体验评测
  • 10 种最新的思维链(Chain-of-Thought, CoT)增强方法
  • 攻防世界-php伪协议和文件包含
  • 第一章-Rust入门
  • 音频感知动画新纪元:Sonic让你的作品更生动
  • PE文件结构(导出表)
  • 专家系统的推理流程深度解析