Unity DOTS系列之Filter Baking Output与Prefab In Baking核心分析

最近DOTS发布了正式的版本, 我们来分享一下DOTS里面Baking核心机制,方便大家上手学习掌握Unity DOTS开发。今天给大家分享的Baking机制中的Filter Baking OutputPrefab In Baking

对啦!这里有个游戏开发交流小组里面聚集了一帮热爱学习游戏的零基础小白,也有一些正在从事游戏开发的技术大佬,欢迎你来交流学习。

Filter Baking Output 机制

在默认情况下,Baking会为每个GameObject生成的Entity与Component, 这些entity都会被创建到Conversion World里面。然后在创作的时候不是所有的GameObject都需要被转换成Entity。例如: 在一个样条曲线上,一个控制点在创作的时候被用到了,但是bake成ecs数据后可能就再也没有用了,所以不需要把这个控制点Bake成entity。

为了不把这个GameObject Bake产生的Entity输出到World并保存到entity scene里面,我们可以给这个Entity添加一个BakingOnlyEntity tag Component。当你添加了这个tag component后,Baking系统就不会把你这个entity存储到entity scene里面,也不会把这个entity生成到运行的main World里面。我们可以直接在Baker函数里面添给entity 添加一个BakingOnlyEntity的组件数据,也可以在Authoring GameObject里面添加一个 BakingOnlyEntityAuthoring的组件,这两种方式都可以达到同样的效果没有区别。

你也可以给组件添加注解[BakingType],[TemporaryBakingType] attributes过滤掉掉组件component,让它不输出到entity中。

[TemporaryBakingType]:被这个attributes注记的Component会在Baking Output的时候不会输出到entity。这个Component只会存活在Baker过程中,Baker结束以后就会销毁。

我们有时候需要在Baking System里面批量处理一些组件数据,处理完后这些组件就没有用了。例如,baker 把一个bounding box 转换成了ecs component 数据,接下来我们定义了一个Baking System批量处理所有的entity,来计算entity的凸包。计算完成后原来的bounding box组件就可以删除了,这种情况下就可以使用上面的Filter Component Bake output机制。

Prefab In Baking机制

生成entity prefab到entity scene以后,我们就可以像使用普通的Prefab创建GameObject一样来创建entity到世界。但是使用enity prefab之前一定要确保它已经被Baker到了entity scene。当预制体实例化到Authoring Scene中的时候,Baking把它当作普通的GameObject来进行转换,不会把它当作预制体。

生成一个entity prefab你需要注册一个Baker,在Bake函数里面添加一个依赖关系,让这个依赖于Authoring GameObject Prefab。然后Prefab将会被bake出来。我们搞一个组件保存了entity prefab的一个引用,那么unity就会把这个entity prefab 序列化到subscene中。当需要使用这个entity prefab的时候就能获取到。代码如下:

public struct EntityPrefabComponent : IComponentData{public Entity Value;}public class GetPrefabAuthoring : MonoBehaviour{public GameObject Prefab;}public class GetPrefabBaker : Baker<GetPrefabAuthoring>{public override void Bake(GetPrefabAuthoring authoring){// Register the Prefab in the Bakervar entityPrefab = GetEntity(authoring.Prefab, TransformUsageFlags.Dynamic);// Add the Entity reference to a component for instantiation latervar entity = GetEntity(TransformUsageFlags.Dynamic);AddComponent(entity, new EntityPrefabComponent() {Value = entityPrefab});}}#endregion

在Baking的时候,当我们需要引用一个entity prefab, 可以使用EntityPrefabReference。这个会把它序列化到entity scene文件里面去。运行的时候直接load进来,就可以使用了。这样可以防止多个subscene不用重复拷贝生成同一个Prefab。

#region InstantiateLoadedPrefabspublic partial struct InstantiatePrefabReferenceSystem : ISystem{public void OnStartRunning(ref SystemState state){// Add the RequestEntityPrefabLoaded component to the Entities that have an// EntityPrefabReference but not yet have the PrefabLoadResult// (the PrefabLoadResult is added when the prefab is loaded)// Note: it might take a few frames for the prefab to be loadedvar query = SystemAPI.QueryBuilder().WithAll<EntityPrefabComponent>().WithNone<PrefabLoadResult>().Build();state.EntityManager.AddComponent<RequestEntityPrefabLoaded>(query);}public void OnUpdate(ref SystemState state){var ecb = new EntityCommandBuffer(Allocator.Temp);// For the Entities that have a PrefabLoadResult component (Unity has loaded// the prefabs) get the loaded prefab from PrefabLoadResult and instantiate itforeach (var (prefab, entity) inSystemAPI.Query<RefRO<PrefabLoadResult>>().WithEntityAccess()){var instance = ecb.Instantiate(prefab.ValueRO.PrefabRoot);// Remove both RequestEntityPrefabLoaded and PrefabLoadResult to prevent// the prefab being loaded and instantiated multiple times, respectivelyecb.RemoveComponent<RequestEntityPrefabLoaded>(entity);ecb.RemoveComponent<PrefabLoadResult>(entity);}ecb.Playback(state.EntityManager);ecb.Dispose();}}#endregion
实例化entity prefab可以使用EntityManager与entity command buffer。
#region InstantiateEmbeddedPrefabspublic partial struct InstantiatePrefabSystem : ISystem{public void OnUpdate(ref SystemState state){var ecb = new EntityCommandBuffer(Allocator.Temp);// Get all Entities that have the component with the Entity referenceforeach (var prefab inSystemAPI.Query<RefRO<EntityPrefabComponent>>()){// Instantiate the prefab Entityvar instance = ecb.Instantiate(prefab.ValueRO.Value);// Note: the returned instance is only relevant when used in the ECB// as the entity is not created in the EntityManager until ECB.Playbackecb.AddComponent<ComponentA>(instance);}ecb.Playback(state.EntityManager);ecb.Dispose();}}#endregion
实例化EntityPrefabReference,可以使用如下代码:#region InstantiateLoadedPrefabspublic partial struct InstantiatePrefabReferenceSystem : ISystem{public void OnStartRunning(ref SystemState state){// Add the RequestEntityPrefabLoaded component to the Entities that have an// EntityPrefabReference but not yet have the PrefabLoadResult// (the PrefabLoadResult is added when the prefab is loaded)// Note: it might take a few frames for the prefab to be loadedvar query = SystemAPI.QueryBuilder().WithAll<EntityPrefabComponent>().WithNone<PrefabLoadResult>().Build();state.EntityManager.AddComponent<RequestEntityPrefabLoaded>(query);}public void OnUpdate(ref SystemState state){var ecb = new EntityCommandBuffer(Allocator.Temp);// For the Entities that have a PrefabLoadResult component (Unity has loaded// the prefabs) get the loaded prefab from PrefabLoadResult and instantiate itforeach (var (prefab, entity) inSystemAPI.Query<RefRO<PrefabLoadResult>>().WithEntityAccess()){var instance = ecb.Instantiate(prefab.ValueRO.PrefabRoot);// Remove both RequestEntityPrefabLoaded and PrefabLoadResult to prevent// the prefab being loaded and instantiated multiple times, respectivelyecb.RemoveComponent<RequestEntityPrefabLoaded>(entity);ecb.RemoveComponent<PrefabLoadResult>(entity);}ecb.Playback(state.EntityManager);ecb.Dispose();}}#endregion

在实例化EntityPrefabReference之前,Unity必须要先加载对应的entity prefab,然后才能使用它,添加RequestEntityPrefabLoaded组件能确保entity prefab被加载。Unity会PrefabLoadResult加载到带有RequestEntityPrefabLoaded同一个entity上。

预制体也是entity,也可以被查询到,如果需要把一个预制体被查询到,可以在查询条件上添加IncludePrefab。

 #region PrefabsInQueries// This query will return all baked entities, including the prefab entitiesvar prefabQuery = SystemAPI.QueryBuilder().WithAll<BakedEntity>().WithOptions(EntityQueryOptions.IncludePrefab).Build();
#endregion
使用EntityManager与entity command buffer也可以销毁一个预制体节点,代码如下:#region DestroyPrefabsvar ecb = new EntityCommandBuffer(Allocator.Temp);foreach (var (component, entity) inSystemAPI.Query<RefRO<RotationSpeed>>().WithEntityAccess()){if (component.ValueRO.RadiansPerSecond <= 0){ecb.DestroyEntity(entity);}}ecb.Playback(state.EntityManager);ecb.Dispose();#endregion

今天的Baking 系列就分享到这里了,关注我学习更多的最新Unity DOTS开发技巧。

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/117304.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

SQL Delete 语句(删除表中的记录)

SQL DELETE 语句 DELETE语句用于删除表中现有记录。 SQL DELETE 语法 DELETE FROM table_name WHERE condition; 请注意删除表格中的记录时要小心&#xff01;注意SQL DELETE 语句中的 WHERE 子句&#xff01; WHERE子句指定需要删除哪些记录。如果省略了WHERE子句&#xff…

【数据结构】数组和字符串(二):特殊矩阵的压缩存储:对角矩阵——一维数组

文章目录 4.2.1 矩阵的数组表示4.2.2 特殊矩阵的压缩存储a. 对角矩阵的压缩存储结构体初始化元素设置元素获取打印矩阵主函数输出结果代码整合 4.2.1 矩阵的数组表示 【数据结构】数组和字符串&#xff08;一&#xff09;&#xff1a;矩阵的数组表示 4.2.2 特殊矩阵的压缩存储…

Elasticsearch配置文件

一 前言 在elasticsearch\config目录下,有三个核心的配置文件: elasticsearch.yml,es相关的配置。jvm.options,Java jvm相关参数的配置。log4j2.properties,日志相关的配置,因为es采用了log4j的日志框架。这里以elasticsearch6.5.4版本为例,并且由于版本不同,配置也不…

UG\NX二次开发 实现“适合窗口”的功能

文章作者:里海 来源网站:王牌飞行员_里海_里海NX二次开发3000例,里海BlockUI专栏,C\C++-CSDN博客 感谢粉丝订阅 感谢 shsjdj 订阅本专栏,非常感谢。 简介 实现“适合窗口”的功能 效果 代码1 #include "me.hpp"extern DllExport void ufusr(char* param, int* re…

【数据结构与算法】二叉树的综合运用

目录 一&#xff0c;层序遍历算法 1-1&#xff0c;队列结构的设立 1-2&#xff0c;逻辑分析 二&#xff0c;判断单值二叉树 三&#xff0c;求二叉树的最大深度 一&#xff0c;层序遍历算法 二叉树的层序遍历是一层一层的从左到右遍历&#xff0c;现在问题是二叉树不支持随…

我们距离“裸眼3D自由”,还有多远?

还记得2018年&#xff0c;我曾熬夜好几天&#xff0c;就为了抢一张故宫博物院“清明上河图互动艺术展演”的门票。 后来&#xff0c;我也曾去过很多城市&#xff0c;看过不少策划精良的展览。那场“穿越北宋”的名画之旅&#xff0c;依然是我看过的&#xff0c;最具沉浸感的一场…

【Linux】kill 命令使用

经常用kill -9 XXX 。一直在kill&#xff0c;除了kill -9 -15 &#xff0c;还能做什么&#xff1f;今天咱们一起学习一下。 kill 命令用于删除执行中的程序或工作。 kill命令 -Linux手册页 命令选项及作用 执行令 man kill 执行命令结果 参数 -l 信号&#xff0c;若果…

力扣学习笔记——49. 字母异位词分组

49. 字母异位词分组 https://leetcode.cn/problems/group-anagrams/?envTypestudy-plan-v2&envIdtop-100-liked 给你一个字符串数组&#xff0c;请你将 字母异位词 组合在一起。可以按任意顺序返回结果列表。 字母异位词 是由重新排列源单词的所有字母得到的一个新单词。…

驱动开发5 阻塞IO实例、IO多路复用

1 阻塞IO 进程1 #include <stdlib.h> #include <stdio.h> #include <sys/types.h> #include <sys/stat.h> #include <sys/ioctl.h> #include <fcntl.h> #include <unistd.h> #include <string.h>int main(int argc, char co…

Luckyexcel 加载 springboot 后台返回的 excel 文件并显示

&#x1f451; 博主简介&#xff1a;知名开发工程师 &#x1f463; 出没地点&#xff1a;北京 &#x1f48a; 2023年目标&#xff1a;成为一个大佬 ——————————————————————————————————————————— 版权声明&#xff1a;本文为原创文…

Unity - 导出的FBX模型,无法将 vector4 保存在 uv 中(使用 Unity Mesh 保存即可)

文章目录 目的问题解决方案验证保存为 Unity Mesh 结果 - OK保存为 *.obj 文件结果 - not OK&#xff0c;但是可以 DIY importer注意References 目的 备忘&#xff0c;便于日后自己索引 问题 为了学习了解大厂项目的效果&#xff1a; 上周为了将 王者荣耀的 杨玉环 的某个皮肤…

关于nacos的配置获取失败及服务发现问题的排坑记录

nacos配置更新未能获取到导致启动报错 排查思路&#xff1a; 1、是否添加了nacos的启动pom依赖 参考&#xff1a; <dependency><groupId>com.alibaba.cloud</groupId><artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId><…

vue3中的route和router

因为我们在 setup 里面没有访问 this&#xff0c;所以我们不能再直接访问 this.$router 或 this.$route。作为替代&#xff0c;我们使用 useRouter 和useRoute函数,或者 Vue3 中提供了一个 getCurrentInstance 方法来获取当前 Vue 实例 <script setup>import { useRoute…

GEE图表——利用NOAA气象数据绘制气温预测图

简介 气象预测是通过气象数据和模型对未来某一时间和地点的天气情况进行预测。 具体步骤如下&#xff1a; 1. 数据采集&#xff1a;从气象观测站、卫星等获取气象数据&#xff0c;包括气压、水汽、风速、温度、降雨、云量等。 2. 数据清洗&#xff1a;对采集到的数据进行质…

学到一招 chrome 浏览器 debug 悬浮样式

前言 今天在想调试一个开源 UI 框架的某个table row的隔行换色的样式设置&#xff0c;发现这个颜色只有鼠标悬浮在row的时候才能拿到&#xff0c;但是想要拷贝 row 样式&#xff0c;鼠标必须离开悬浮区域&#xff0c;去chrome的debug控制台内才能拷贝&#xff0c;但是一离开悬…

Qt之设置QLineEdit只能输入浮点数

Qt提供了QDoubleValidator来进行浮点数校验,但是它同样存在限定范围无效的问题,详见:Qt之彻底解决QSpinBox限定范围无效的问题 因此我们要子类化QDoubleValidator,并重写其中的validate方法,最后调用QLineEdit的setValidator方法,并将这个子类当做参数传入。 QHDoubleVa…

怎么降低Linux内核驱动开发的风险?

降低Linux内核驱动开发的风险是一个重要的目标&#xff0c;因为内核驱动开发可能会对系统的稳定性和安全性产生重要影响。以下是一些降低风险的建议&#xff1a; 1. 深入了解Linux内核&#xff1a;在开始内核驱动开发之前&#xff0c;建议深入学习Linux内核的工作原理和架构&a…

[Unity]将所有 TGA、TIFF、PSD 和 BMP(可自定义)纹理转换为 PNG,以减小项目大小,而不会在 Unity 中造成任何质量损失

如何使用 只需在“项目”窗口中创建一个名为“编辑器”的文件夹&#xff0c;然后在其中添加此脚本即可。然后&#xff0c;打开窗口-Convert Textures to PNG&#xff0c;配置参数并点击“Convert to PNG&#xff01; ”。 就我而言&#xff0c;它已将某些 3D 资源的总文件大小…

交互式 Web 应用 0 基础入门

初探 Gradio&#xff1a;轻松构建交互式 Web 应用 文章目录 初探 Gradio&#xff1a;轻松构建交互式 Web 应用Why Gradio?安装 Gradio创建交互式界面1. gr.Interface2. gr.Blocks 强大的组件库输入输出组件控制组件布局组件 示例交互式数据可视化多组件同时&#xff08;嵌套&a…

vue3 源码解析(1)— reactive 响应式实现

前言 本文是 vue3 源码解析系列的第一篇文章&#xff0c;项目代码的整体实现是参考了 v3.2.10 版本&#xff0c;项目整体架构可以参考之前我写过的文章 rollup 实现多模块打包。话不多说&#xff0c;让我们通过一个简单例子开始这个系列的文章。 举个例子 <!DOCTYPE html…