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 Disable component in Baking System

Discussion in 'Entity Component System' started by DatCong, Jul 5, 2023.

  1. DatCong

    DatCong

    Joined:
    Nov 5, 2020
    Posts:
    77
    The method SetComponentEnabled in Baker is not suitable for me because it uses generic. Is there another way to set ComponentEnabled in baking system ?
     
  2. ThomasEgan

    ThomasEgan

    Joined:
    Dec 17, 2013
    Posts:
    20
    Not exactly sure what you mean about generic being a problem but you can disable components with a baking system like so:

    Code (CSharp):
    1. [WorldSystemFilter(WorldSystemFilterFlags.BakingSystem)]
    2. [UpdateInGroup(typeof(PostBakingSystemGroup))]
    3. public partial class DisableComponentBakingSystem : BakingSystem
    4. {
    5.     protected override void OnUpdate()
    6.     {
    7.         var query = SystemAPI.QueryBuilder().WithAll<EnableableComponent>()
    8.             .WithOptions(EntityQueryOptions.IncludePrefab | EntityQueryOptions.IncludeDisabledEntities)
    9.             .Build();
    10.  
    11.         var entities = query.ToEntityArray(Allocator.Temp);
    12.         foreach (var entity in entities)
    13.         {
    14.             SystemAPI.SetComponentEnabled<EnableableComponent>(entity, false);
    15.         }
    16.     }
    17. }
     
    DatCong likes this.