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 [1.0.0-exp.8] Baker remove component and destroy entity

Discussion in 'Entity Component System' started by optimise, Sep 28, 2022.

  1. optimise

    optimise

    Joined:
    Jan 22, 2014
    Posts:
    2,029
    At 1.0 release, how can I remove redundant components in Baker since there's no remove component and destroy entity API anymore?
     
    Last edited: Sep 28, 2022
    charleshendry likes this.
  2. tertle

    tertle

    Joined:
    Jan 25, 2011
    Posts:
    3,626
    It's a 2 part process. Bake then execute on baked data.
    You need to do this in the second part via an ISystem (or SystemBase)

    Code (CSharp):
    1. [WorldSystemFilter(WorldSystemFilterFlags.BakingSystem)]
    2. internal partial struct YourSystem : ISystem
    There's also an attribute, [TemporaryBakingType], that can be used to pass data between Baker and your Systems that will be removed at the end and not make runtime

    So you can create a
    Code (CSharp):
    1.  
    2. [TemporaryBakingType]
    3. struct NoTranslation: IComponentData {}
    4.  
    5. public class NoTranslationAuthoring : MonoBehaviour {}
    6.  
    7. public class NoTranslationBaker : Baker<NoTranslationAuthoring>
    8. {
    9.     public override void Bake(NoTranslationAuthoring authoring)
    10.     {
    11.         AddComponent<NoTranslation>();
    12.     }
    13. }
    14.  
    15. [WorldSystemFilter(WorldSystemFilterFlags.BakingSystem)]
    16. public partial struct NoTranslationSystem : ISystem {
    17.     public void OnUpdate(ref SystemState state)
    18.     {
    19.         var ecb = new EntityCommandBuffer(Allocator.Temp);
    20.         foreach (var (_, entity) in SystemAPI.Query<NoTranslation>().WithEntityAccess())
    21.         {
    22.             ecb.RemoveComponent<Translation>(entity);
    23.         }
    24.         ecb.Playback(state.EntityManager);
    25.     }
    26.  
    27.     // ...
    28. }
    * Code written in browser, may not compile
     
  3. charleshendry

    charleshendry

    Joined:
    Jan 7, 2018
    Posts:
    95
    I'm using the code you have above, but using it to remove the Parent components that Unity adds. The system runs, but the foreach section doesn't, even though I can see Entities with the RemoveParent BakingType component I added.

    It looks like instead of this, I'd also be able to add a BakingOnlyEntity component to the parent entity (which is just an empty GameObject in the subscene used for grouping), but that component doesn't seem to exist in my Entities package.