当前位置: 首页 > 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);

}

}

相关文章:

  • HTML 元素
  • 冷启动算法简介和示例
  • 【了解】数字孪生网络(Digital Twin Network,DTN)
  • 代码随想录算法训练营第60期第二十七天打卡
  • ABC 404
  • sudo useradd -r -s /bin/false -U -m -d /usr/share/ollama ollama解释这行代码的含义
  • 机器人强化学习入门学习笔记(二)
  • HTML05:超链接标签及应用
  • 永磁同步电机控制算法--基于PI和前馈的位置伺服控制
  • 告别(Python)if elif else错误使用方法
  • 介绍分治、动态规划、回溯分别是什么?有什么联系和区别?给出对象的场景和java代码?
  • 【硬核攻坚】告别CUDA OOM!DeepSeek部署显存瓶颈终极解决方案:三大策略高效落地
  • day04_计算机常识丶基本数据类型转换
  • 15.日志分析入门
  • 架构思维:构建高并发读服务_热点数据查询的架构设计与性能调优
  • 三维重建(二十一)——第二步和第三步
  • 数据集-目标检测系列- 印度人脸 检测数据集 indian face >> DataBall
  • 对于1年来开发的程序化股票交易的做一个总结
  • linux inotify 资源详解
  • 【Qt】配置环境变量
  • 山东如意集团及实控人等被出具警示函,此前实控人已被罚十年禁止入市
  • 以总理:在加沙地带扩大的军事行动将是“高强度”的
  • 上音校园春日花艺引路人打卡,阳台音乐会吹响《玫瑰人生》
  • 中国海警局回应日本民用飞机侵闯我钓鱼岛领空:依法警告驱离
  • 我的诗歌阅读史
  • “五一”前两日湖北20多家景区实施限流