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

相关文章

怎样通过分析GC日志来定位Java进程的内存问题

《怎样通过分析GC日志来定位Java进程的内存问题》:本文主要介绍怎样通过分析GC日志来定位Java进程的内存问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录一、GC 日志基础配置1. 启用详细 GC 日志2. 不同收集器的日志格式二、关键指标与分析维度1.

MySQL中的表连接原理分析

《MySQL中的表连接原理分析》:本文主要介绍MySQL中的表连接原理分析,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录1、背景2、环境3、表连接原理【1】驱动表和被驱动表【2】内连接【3】外连接【4编程】嵌套循环连接【5】join buffer4、总结1、背景

深度解析Spring Boot拦截器Interceptor与过滤器Filter的区别与实战指南

《深度解析SpringBoot拦截器Interceptor与过滤器Filter的区别与实战指南》本文深度解析SpringBoot中拦截器与过滤器的区别,涵盖执行顺序、依赖关系、异常处理等核心差异,并... 目录Spring Boot拦截器(Interceptor)与过滤器(Filter)深度解析:区别、实现

python中Hash使用场景分析

《python中Hash使用场景分析》Python的hash()函数用于获取对象哈希值,常用于字典和集合,不可变类型可哈希,可变类型不可,常见算法包括除法、乘法、平方取中和随机数哈希,各有优缺点,需根... 目录python中的 Hash除法哈希算法乘法哈希算法平方取中法随机数哈希算法小结在Python中,

Java Stream的distinct去重原理分析

《JavaStream的distinct去重原理分析》Javastream中的distinct方法用于去除流中的重复元素,它返回一个包含过滤后唯一元素的新流,该方法会根据元素的hashcode和eq... 目录一、distinct 的基础用法与核心特性二、distinct 的底层实现原理1. 顺序流中的去重

详解MySQL中DISTINCT去重的核心注意事项

《详解MySQL中DISTINCT去重的核心注意事项》为了实现查询不重复的数据,MySQL提供了DISTINCT关键字,它的主要作用就是对数据表中一个或多个字段重复的数据进行过滤,只返回其中的一条数据... 目录DISTINCT 六大注意事项1. 作用范围:所有 SELECT 字段2. NULL 值的特殊处

关于MyISAM和InnoDB对比分析

《关于MyISAM和InnoDB对比分析》:本文主要介绍关于MyISAM和InnoDB对比分析,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录开篇:从交通规则看存储引擎选择理解存储引擎的基本概念技术原理对比1. 事务支持:ACID的守护者2. 锁机制:并发控制的艺

Python包管理工具核心指令uvx举例详细解析

《Python包管理工具核心指令uvx举例详细解析》:本文主要介绍Python包管理工具核心指令uvx的相关资料,uvx是uv工具链中用于临时运行Python命令行工具的高效执行器,依托Rust实... 目录一、uvx 的定位与核心功能二、uvx 的典型应用场景三、uvx 与传统工具对比四、uvx 的技术实

MyBatis Plus 中 update_time 字段自动填充失效的原因分析及解决方案(最新整理)

《MyBatisPlus中update_time字段自动填充失效的原因分析及解决方案(最新整理)》在使用MyBatisPlus时,通常我们会在数据库表中设置create_time和update... 目录前言一、问题现象二、原因分析三、总结:常见原因与解决方法对照表四、推荐写法前言在使用 MyBATis

Python主动抛出异常的各种用法和场景分析

《Python主动抛出异常的各种用法和场景分析》在Python中,我们不仅可以捕获和处理异常,还可以主动抛出异常,也就是以类的方式自定义错误的类型和提示信息,这在编程中非常有用,下面我将详细解释主动抛... 目录一、为什么要主动抛出异常?二、基本语法:raise关键字基本示例三、raise的多种用法1. 抛