Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Question Need help with Accessing Child Entities during subscene baking

Discussion in 'Entity Component System' started by unityfreddy, Oct 4, 2023.

  1. unityfreddy

    unityfreddy

    Joined:
    Jul 21, 2018
    Posts:
    2
    Hi.
    I have gameobjects in a subscene that are using multiple materials.
    When the gameobjects are converted, for each material they are split into child entities parented to a parent entity. I'd like to add material overrides components to the child entities.

    I use a baker where I tag that entity, then using a baking system I can query the parent entity, but i can't access the child entities using SystemAPI.HasBuffer<Child>(entity).

    Code (CSharp):
    1. public class BuildingFloorAuthoring : MonoBehaviour
    2.     {
    3.         public int Floor = 0;
    4.  
    5.         private class BuildingFloorAuthoringBaker : Baker<BuildingFloorAuthoring>
    6.         {
    7.             public override void Bake(BuildingFloorAuthoring authoring)
    8.             {
    9.                 var entity = GetEntity(TransformUsageFlags.Dynamic);
    10.  
    11.                 AddComponent(entity, new Floor()
    12.                 {
    13.                     Value = authoring.Floor
    14.                 });
    15.  
    16.             }
    17.         }
    18.     }
    19.  
    20.     public struct Floor : IComponentData
    21.     {
    22.         public int Value;
    23.     }
    24.  
    25.     [WorldSystemFilter(WorldSystemFilterFlags.BakingSystem)]
    26.     partial struct AddMaterialOverrideToSubMeshesBakingSystem : ISystem
    27.     {
    28.         public void OnUpdate(ref SystemState state)
    29.         {
    30.             var queryFloorTag = SystemAPI.QueryBuilder()
    31.                 .WithAny<Floor>()
    32.                 .Build();
    33.            
    34.             var entityArray = queryFloorTag.ToEntityArray(Allocator.Temp);
    35.  
    36.             foreach (var entity in entityArray)
    37.             {
    38.                 Debug.Log("iteration");
    39.                 if (SystemAPI.HasBuffer<Child>(entity))
    40.                 {
    41.                     Debug.Log("found child");
    42.                 }
    43.             }
    44.  
    45.             entityArray.Dispose();
    46.         }
    47.     }
    The debug "iteration" runs fine and matches the number of tagged entities, but "found child" never prints.
    If I run the system outside of the baking then it works fine.
    What am I doing wrong? Thanks.
     
  2. DreamingImLatios

    DreamingImLatios

    Joined:
    Jun 3, 2017
    Posts:
    3,983
    Baked entities don't have a Child buffer. That's added at runtime.
     
    unityfreddy likes this.
  3. unityfreddy

    unityfreddy

    Joined:
    Jul 21, 2018
    Posts:
    2
    I see, I'll add the material overrides at runtime then. Thank you for taking the time to help!