Search Unity

Get Entity in a converted parent, from its child (and converted) GameObjects

Discussion in 'Entity Component System' started by alexandre-fiset, Nov 5, 2019.

  1. alexandre-fiset

    alexandre-fiset

    Joined:
    Mar 19, 2012
    Posts:
    715
    Lets say that I have this basic setup in the inspector:
    • TestScreen
      • Canvas
        • Image1
        • Image2
    ...and that I have a ConversionSystem that converts the following authorings to entities:
    • TestScreenAuthoring (on TestScreen GO)
    • ImageAuthoring (on Image1 GO)
    • ImageAuthoring (on Image2 GO)
    ...and I would like the TestScreen to store Image1Entity and Image2Entity in its component data. My first reflex was to do the following procedure:
    • ImageConversion creates the entities for my images from their authoring components
    • TestScreen creates the screen entity, and get the image entities with GetPrimaryEntity from their injected GameObject.
    The problem is that the image entities never get created when TestScreen is there, even if have used UpdateAfter(typeof(ImageConversion).

    So the question is: Is there a way to get an entity from another conversion, through GetPrimaryEntity or something similar? Or I have to always make sure that two conversions are not child nor parent from another conversion?

    This all seems quite limitative in my opinion, especially when dealing with UI which are often a composition of multiple entities.
     
  2. GilCat

    GilCat

    Joined:
    Sep 21, 2013
    Posts:
    676
    You could add GameObjectEntity to all your children and the when conversion get the Entity from there.
    I'm not sure if GameObjectEntity is going to be deprecated in the future or not but that is how I used to approach that problem.
     
  3. riskparitygawd

    riskparitygawd

    Joined:
    Sep 22, 2018
    Posts:
    21
    I use a MonoBehavioube but you may be able to use just the authoring component with a reference to the child transform. After conversion of the parent use ConvertToEntity on the child and the authoring component gets picked up during conversion. In order to link the children to the parent you'll either need to assign the entity reference to a property in Authoring after conversion or find them via other means.
    Code (CSharp):
    1. ConvertToEntity.ConvertHierarchy(child.gameObject);
     
  4. DreamingImLatios

    DreamingImLatios

    Joined:
    Jun 3, 2017
    Posts:
    4,264
    Your conversion system should not be creating new entities, but rather using the created entities made by the GameObjectConversionMappingSystem. Don't use GameObjectEntity.
     
    GilCat likes this.
  5. thelebaron

    thelebaron

    Joined:
    Jun 2, 2013
    Posts:
    857
    If I were to tackle this, (as I don't know if there yet an automatic way to convert a hierarchy if you want to use monobehaviours)

    I would make the two new entities manually in TestScreenAuthoring with conversionSystem.CreateAdditionalEntity() (using the the gameobject which TestScreenAuthoring is attached to) and then with references to the two ImageScreenAuthoring scripts on the root authoring, manually add any component objects to their respective entities.
     
  6. BackgroundMover

    BackgroundMover

    Joined:
    May 9, 2015
    Posts:
    224
    Here's how I got it to work

    Code (CSharp):
    1.  
    2. //https://forum.unity.com/threads/what-is-the-right-way-to-fine-tune-gameobject-conversion.767654/#post-5116592
    3.  
    4. [UpdateInGroup(typeof(GameObjectAfterConversionGroup))]
    5. public class Converter : GameObjectConversionSystem {
    6.  
    7.     protected override void OnUpdate() {
    8.  
    9.         //Only loop through entities in the hybrid world, thanks to DreamingImLatios
    10.         Entities.ForEach((TestScreenAuthor testScreenAuthor) => {
    11.  
    12.             //Get the entity that was created for this monobehaviour
    13.             var testScreenEntity = GetPrimaryEntity(testScreenAuthor);
    14.  
    15.             //Get the ComponentData
    16.             var testScreenComponentData = DstEntityManager.GetComponentData<TestScreen>(testScreenEntity);
    17.  
    18.             //Get the entities for the Image monobehaviours pointed to by the TestScreenAuthor
    19.             var Image1Entity = GetPrimaryEntity(testScreenAuthor.Image1Ref);
    20.             var Image2Entity = GetPrimaryEntity(testScreenAuthor.Image2Ref);
    21.  
    22.             //Set the data on the component
    23.             testScreenComponentData.Image1 = Image1Entity;
    24.             testScreenComponentData.Image2 = Image2Entity;
    25.  
    26.             //Set the component data back on the entity
    27.             DstEntityManager.SetComponentData<TestScreen>(testScreenEntity, testScreenComponentData);
    28.  
    29.         });
    30.     }
    31. }
    Edit: I thought [UpdateInGroup(typeof(GameObjectAfterConversionGroup))] means that component generation has finished, but now I'm not sure.
     

    Attached Files:

    Last edited: Nov 6, 2019
  7. GilCat

    GilCat

    Joined:
    Sep 21, 2013
    Posts:
    676
    What if one wants to reference another gameobject that is not part of the conversion hierarchy but also has the ConvertToEntity?