首页 > 其他分享 >Unity DOTS系列之Filter Baking Output与Prefab In Baking核心分析

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

时间:2023-12-01 11:26:15浏览次数:34  
标签:DOTS Prefab Baking ecb entity public var prefab

最近DOTS发布了正式的版本, 我们来分享一下DOTS里面Baking核心机制,方便大家上手学习掌握Unity DOTS开发。今天给大家分享的Baking机制中的Filter Baking Output与Prefab 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 Baker
 var entityPrefab = GetEntity(authoring.Prefab, TransformUsageFlags.Dynamic);
 // Add the Entity reference to a component for instantiation later
 var entity = GetEntity(TransformUsageFlags.Dynamic);
 AddComponent(entity, new EntityPrefabComponent() {Value = entityPrefab});
 }
 }
    #endregion

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

#region InstantiateLoadedPrefabs
 public 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 loaded
 var 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 it
 foreach (var (prefab, entity) in
 SystemAPI.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, respectively
 ecb.RemoveComponent<RequestEntityPrefabLoaded>(entity);
 ecb.RemoveComponent<PrefabLoadResult>(entity);
 }
 ecb.Playback(state.EntityManager);
 ecb.Dispose();
 }
 }
    #endregion
实例化entity prefab可以使用EntityManager与entity command buffer。
#region InstantiateEmbeddedPrefabs
 public 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 reference
 foreach (var prefab in
 SystemAPI.Query<RefRO<EntityPrefabComponent>>())
 {
 // Instantiate the prefab Entity
 var 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.Playback
 ecb.AddComponent<ComponentA>(instance);
 }
 ecb.Playback(state.EntityManager);
 ecb.Dispose();
 }
 }
    #endregion
实例化EntityPrefabReference,可以使用如下代码:
 #region InstantiateLoadedPrefabs
 public 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 loaded
 var 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 it
 foreach (var (prefab, entity) in
 SystemAPI.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, respectively
 ecb.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 entities
 var prefabQuery = SystemAPI.QueryBuilder()
                .WithAll<BakedEntity>().WithOptions(EntityQueryOptions.IncludePrefab).Build();
#endregion
使用EntityManager与entity command buffer也可以销毁一个预制体节点,代码如下:
            #region DestroyPrefabs
 var ecb = new EntityCommandBuffer(Allocator.Temp);
 foreach (var (component, entity) in
 SystemAPI.Query<RefRO<RotationSpeed>>().WithEntityAccess())
 {
 if (component.ValueRO.RadiansPerSecond <= 0)
 {
 ecb.DestroyEntity(entity);
 }
 }
 ecb.Playback(state.EntityManager);
 ecb.Dispose();
            #endregion

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

标签:DOTS,Prefab,Baking,ecb,entity,public,var,prefab
From: https://www.cnblogs.com/bycw/p/17869288.html

相关文章

  • Unity DOTS Baking System与Baking World
    最近DOTS终于发布了正式的版本,我们来分享一下DOTS里面Baking阶段,BakingSystem,BakingWorld的关键概念,方便大家上手学习掌握UnityDOTS开发。Unity在Baking也是基于ECS模式开发设计的,所以Baking的时候也会有BakingSystem与BakingWorld,把Baking出来的数据放到BakingWorld里面......
  • Unity DOTS System与SystemGroup概述
    最近DOTS终于发布了正式的版本,我们来分享以下DOTS里面System关键概念,方便大家上手学习掌握UnityDOTS开发。System是迭代计算与处理World中的Entity实体的ComponentData数据的逻辑代码。System对应的代码是运行在mainthread上的。World里面所有的System通过SystemGroup来进行......
  • Unity 最新DOTS系列之《Baking与Baker的详解》
    UnityDOTSBaking与Baker详解UnityDOTSBaking与Baker详解最近DOTS终于发布了正式的版本,我们来分享一下DOTS里面Baking与Baker的关键概念,方便大家上手学习掌握UnityDOTS开发。UnityDOTS开发模式,为了让大家在”创作”游戏的时候使用原来组件方式来编辑游戏场景与资源,同......
  • Unity DOTS Component概述
    最近DOTS终于发布了正式的版本,我们来分享以下DOTS里面地几个关键概念,方便大家上手学习掌握UnityDOTS开发。UnityDOTS中Entity作为实体不直接去存放数据,而是将数据以一个个的组件为载体来存放起来。每个Entity会得到一些不同的ComponentData的组合,这些组合代表着不同的Entity......
  • Unity DOTS World Entity ArchType Component EntityManager System概述
    最近DOTS终于发布了正式的版本,我们来分享以下DOTS里面地几个关键概念,方便大家上手学习掌握UnityDOTS开发。UnityDOTS中所有的Entities都是被放到World世界中。每个Entity在它所在的World里面有唯一不同的ID号来区分。DOTS项目中可以同时有多个World。每个World有一个Entity......
  • Unity DOTS系列之System中如何使用SystemAPI.Query迭代数据
    最近DOTS发布了正式的版本,我们来分享一下System中如何基于SystemAPI.Query来迭代World中的数据,方便大家上手学习掌握UnityDOTS开发。SystemAPI.Query的使用System有两种,一种是Unmanaged的ISystem,一种是managed的SystemBase,这两种System都可以通过SystemAPI.Query来迭代与......
  • Unity DOTS中ECS核心架构详解
    最近DOTS终于发布了正式的版本, 我们来分享一下DOTS中ECS的几个关键概念与结构,方便大家上手学习掌握Unity DOTS开发。 ECS中的World  Unity DOTS ECS架构中所有的Entity都是被放到了World对象里面,每个Entity在World里面都有唯一的Id号。Unity DOTS 可以同时支持很多个......
  • Unity ECS最新DOTS环境搭建教程
    最近DOTS终于发布了正式的版本, 今天我们来基于Unity2023.1.6来搭建DOTS1.0.16的开发环境与注意事项。 1获取DOTS的在线文档   UnityDOTS的权威资料比较少,我们主要的都是基于DOTS的官方文档来进行学习和使用。UnityDOTS的官方文档的下载地址:https://docs.unity3d.c......
  • Unity DOTS系列之Struct Change核心机制分析
    最近DOTS发布了正式的版本,我们来分享一下DOTS里面StructChange机制,方便大家上手学习掌握UnityDOTS开发。基于ArchType与Chunk的Entity管理机制  我们回顾以下ECS的内存管理核心机制,基于ArchType+Chunk的Entity管理模式。每个Entity不直接存放数据,数据全部存放到Component......
  • Unity DOTS系列之Aspect核心机制分析
      最近DOTS发布了正式的版本,我们来分享一下DOTS里面Aspect机制,方便大家上手学习掌握UnityDOTS开发。Aspect 机制概述当我们使用ECS开发的时候,编写某个功能可能需要某个entity的一些组件,如果我们一个个组件的查询出来,可能参数会写很长。如果我们编写某个功能的时候,需要enti......