Search Unity

Resolved IConvertGameObjectToEntity (Convert -> Bake) How do I access EntityManager now?

Discussion in 'Entity Component System' started by Dapeos, Dec 17, 2022.

  1. Dapeos

    Dapeos

    Joined:
    Jun 27, 2013
    Posts:
    28
    Question 1:
    I just converted 50 IConvertGameObjectToEntity authoring scripts to Bakers in my project.

    However I had two convert methods that created additional entities in their convert. But since I no longer have access to the Entity Manager how do I do this now?
    EntityManager.CreateArchetype
    EntityManager.CreateEntity

    Question 2:
    I also had a GameObjectConversionSystem that ran for just about every authoring script. Are these depreciated as well? If so where should all of this code that needs to run immediately after entity creation go now?
    [UpdateInGroup(typeof(GameObjectAfterConversionGroup))]
    public partial class VehicleConversionSystem : GameObjectConversionSystem

    Thanks
    Marc
     
  2. eizenhorn

    eizenhorn

    Joined:
    Oct 17, 2016
    Posts:
    2,685
    You have access to EntityManager in
    Baking System
    (more info below)

    With baking there is another place for systems which process entities in baking cycle. You can have Baking systems Before baking process, Inside baking process and After baking process.
    It should be
    ISystem
    or
    SystemBase
    which is updating in baking cycle - using
    [WorldSystemFilter(WorldSystemFilterFlags.BakingSystem)]
    and depends on where you want it to run:
    Before baking -
    [UpdateInGroup(typeof(PreBakingSystemGroup))]
    (but remember all the entities will be removed in World before baking process execution)
    After baking -
    [UpdateInGroup(typeof(PostBakingSystemGroup))]

    And without specifying group if you need to inject system in baking process.
     
    Krajca and Dapeos like this.
  3. Dapeos

    Dapeos

    Joined:
    Jun 27, 2013
    Posts:
    28
    Thanks Eizenhorn for the quick reply!
     
  4. lclemens

    lclemens

    Joined:
    Feb 15, 2020
    Posts:
    761
    You can access EntityManager inside of a Baker via _State.World.EntityManager.
     
  5. Luxxuor

    Luxxuor

    Joined:
    Jul 18, 2019
    Posts:
    89
    You should not use the EntityManager in a baker, you can use this instead: Method CreateAdditionalEntity | Entities | 1.0.0-pre.15 (unity3d.com)
    If you add components using the EM in a Baker, changes will not be tracked correctly and you will get issues with the baking.
     
    lclemens likes this.
  6. eizenhorn

    eizenhorn

    Joined:
    Oct 17, 2016
    Posts:
    2,685
    And you shouldn’t do that, and should use Baker methods for proper dependencies or BakingSystem, otherwise you breaks baking dependency tracking and incremental baking.
     
    Krajca and lclemens like this.