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

2023-10-27 12:01

本文主要是介绍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开发技巧。

这篇关于Unity DOTS系列之Filter Baking Output与Prefab In Baking核心分析的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/286108

相关文章

MySQL表锁、页面锁和行锁的作用及其优缺点对比分析

《MySQL表锁、页面锁和行锁的作用及其优缺点对比分析》MySQL中的表锁、页面锁和行锁各有特点,适用于不同的场景,表锁锁定整个表,适用于批量操作和MyISAM存储引擎,页面锁锁定数据页,适用于旧版本... 目录1. 表锁(Table Lock)2. 页面锁(Page Lock)3. 行锁(Row Lock

Java 8 Stream filter流式过滤器详解

《Java8Streamfilter流式过滤器详解》本文介绍了Java8的StreamAPI中的filter方法,展示了如何使用lambda表达式根据条件过滤流式数据,通过实际代码示例,展示了f... 目录引言 一.Java 8 Stream 的过滤器(filter)二.Java 8 的 filter、fi

Springboot中分析SQL性能的两种方式详解

《Springboot中分析SQL性能的两种方式详解》文章介绍了SQL性能分析的两种方式:MyBatis-Plus性能分析插件和p6spy框架,MyBatis-Plus插件配置简单,适用于开发和测试环... 目录SQL性能分析的两种方式:功能介绍实现方式:实现步骤:SQL性能分析的两种方式:功能介绍记录

最长公共子序列问题的深度分析与Java实现方式

《最长公共子序列问题的深度分析与Java实现方式》本文详细介绍了最长公共子序列(LCS)问题,包括其概念、暴力解法、动态规划解法,并提供了Java代码实现,暴力解法虽然简单,但在大数据处理中效率较低,... 目录最长公共子序列问题概述问题理解与示例分析暴力解法思路与示例代码动态规划解法DP 表的构建与意义动

C#使用DeepSeek API实现自然语言处理,文本分类和情感分析

《C#使用DeepSeekAPI实现自然语言处理,文本分类和情感分析》在C#中使用DeepSeekAPI可以实现多种功能,例如自然语言处理、文本分类、情感分析等,本文主要为大家介绍了具体实现步骤,... 目录准备工作文本生成文本分类问答系统代码生成翻译功能文本摘要文本校对图像描述生成总结在C#中使用Deep

Redis主从/哨兵机制原理分析

《Redis主从/哨兵机制原理分析》本文介绍了Redis的主从复制和哨兵机制,主从复制实现了数据的热备份和负载均衡,而哨兵机制可以监控Redis集群,实现自动故障转移,哨兵机制通过监控、下线、选举和故... 目录一、主从复制1.1 什么是主从复制1.2 主从复制的作用1.3 主从复制原理1.3.1 全量复制

Redis主从复制的原理分析

《Redis主从复制的原理分析》Redis主从复制通过将数据镜像到多个从节点,实现高可用性和扩展性,主从复制包括初次全量同步和增量同步两个阶段,为优化复制性能,可以采用AOF持久化、调整复制超时时间、... 目录Redis主从复制的原理主从复制概述配置主从复制数据同步过程复制一致性与延迟故障转移机制监控与维

Redis连接失败:客户端IP不在白名单中的问题分析与解决方案

《Redis连接失败:客户端IP不在白名单中的问题分析与解决方案》在现代分布式系统中,Redis作为一种高性能的内存数据库,被广泛应用于缓存、消息队列、会话存储等场景,然而,在实际使用过程中,我们可能... 目录一、问题背景二、错误分析1. 错误信息解读2. 根本原因三、解决方案1. 将客户端IP添加到Re

Redis主从复制实现原理分析

《Redis主从复制实现原理分析》Redis主从复制通过Sync和CommandPropagate阶段实现数据同步,2.8版本后引入Psync指令,根据复制偏移量进行全量或部分同步,优化了数据传输效率... 目录Redis主DodMIK从复制实现原理实现原理Psync: 2.8版本后总结Redis主从复制实

锐捷和腾达哪个好? 两个品牌路由器对比分析

《锐捷和腾达哪个好?两个品牌路由器对比分析》在选择路由器时,Tenda和锐捷都是备受关注的品牌,各自有独特的产品特点和市场定位,选择哪个品牌的路由器更合适,实际上取决于你的具体需求和使用场景,我们从... 在选购路由器时,锐捷和腾达都是市场上备受关注的品牌,但它们的定位和特点却有所不同。锐捷更偏向企业级和专