Search Unity

IConvertGameObjectToEntity children gameobjects.

Discussion in 'Entity Component System' started by daserra, Sep 25, 2019.

  1. daserra

    daserra

    Joined:
    Dec 7, 2016
    Posts:
    14
    Hello!

    I have the follow structure in my enemy GO.

    Enemy
    - Head (child)

    I attatched to my enemy GO my EnemyAuthoring script where I setup the enemy components and then I attatch the Convert To Entity.
    Everything works fine for the enemy.
    The thing is, I want to also have my HeadAuthoring script attatched to the head GO which is a children of Enemy.
    It seems that when we attatch the COnvert TO Entity to some object it also converts the children to entities. The problem is that in my HeadAuthoring which implements IConvertGameObjectToEntity the convert method is not being execute, not even if I also attatch the Convert To Entity in the Head GO.

    How I can have my Authoring works independently for children objects ?

    THanks in advance!
     
  2. eizenhorn

    eizenhorn

    Joined:
    Oct 17, 2016
    Posts:
    2,685
    Show code
     
  3. daserra

    daserra

    Joined:
    Dec 7, 2016
    Posts:
    14
    Ok, I currently did something that is working but I'm afraid that is not the correct way for doing it.

    Code (CSharp):
    1.  [RequiresEntityConversion]
    2.     public class PlayerAuthoring : MonoBehaviour, IConvertGameObjectToEntity {
    3.         public Emitter emitter;
    4.  
    5.         public void Convert (Entity entity, EntityManager dstManager, GameObjectConversionSystem conversionSystem) {
    6.  
    7.             var playerInput = new PlayerInput { };
    8.             dstManager.AddComponentData (entity, playerInput);
    9.             for(int i = 0; i < this.gameObject.transform.childCount; i++){
    10.                 if(this.gameObject.transform.GetChild(i).GetComponent<SortAuthoring>() != null){
    11.                        GameObjectConversionUtility.ConvertGameObjectHierarchy(this.gameObject.transform.GetChild(i).gameObject, World.Active);
    12.                 }
    13.             }
    14.         }
    15.     }
    In my child object I have a SomeAuthoring script that now is being execute
     
  4. eizenhorn

    eizenhorn

    Joined:
    Oct 17, 2016
    Posts:
    2,685
    You shouldn't do that, if you have ConvertToEntity on root it will convert whole hierarchy authorings. Show your HeadAuthoring
     
  5. daserra

    daserra

    Joined:
    Dec 7, 2016
    Posts:
    14
    Yes it seems that the solution I used reset the GO and it loses all mono scripts so doesn't really works.

    So I have a Sorting component that I will add to the child where I can add some parameters. Here is the child Authoring

    Code (CSharp):
    1.  public class SortAuthoring : MonoBehaviour, IConvertGameObjectToEntity {
    2.  
    3.         public Sorting sorting;
    4.    
    5.         public void Convert (Entity entity, EntityManager dstManager, GameObjectConversionSystem conversionSystem) {
    6.             dstManager.AddComponentData (entity, sorting);
    7.         }
    8.     }
    The SortingAuthoring is attatched to the child object
     
  6. thelebaron

    thelebaron

    Joined:
    Jun 2, 2013
    Posts:
    857
    Is ConvertToEntity being used as Inject on the root gameobject? If so this might be the cause of your issues.
     
  7. daserra

    daserra

    Joined:
    Dec 7, 2016
    Posts:
    14
    Indeed, If I set Convert and Destroy it works. What give me another problem considering I'm trying to use a hybrid solution. In my child Object I have also a Sprite Renderer and a Mono Script. If I convert and destroy the child GO will lose the other properties as Sprite Renderer and My Mono Script.

    I tried to do the follow but it didn't works.

    Code (CSharp):
    1.  public class SortAuthoring : MonoBehaviour, IConvertGameObjectToEntity {
    2.  
    3.         public SpriteRenderer spriteRenderer;
    4.         public Sorting sorting;
    5.  
    6.         public void Convert (Entity entity, EntityManager dstManager, GameObjectConversionSystem conversionSystem) {
    7.  
    8.             Debug.Log ("Sorting Authority");
    9.             dstManager.AddComponentData (entity, sorting);
    10.             dstManager.AddComponentObject(entity, spriteRenderer);
    11.         }
    12.     }
    There is a way to keep the Sprite Renderer and My monoscript attatched to it?
     
  8. thelebaron

    thelebaron

    Joined:
    Jun 2, 2013
    Posts:
    857
    For now(not sure if this will change in the future as the roadmap or answers surrounding it havent really been explained) you will just need to manually create an entity for it and assign the component data yourself.
    so something like

    Code (CSharp):
    1.  
    2.         public GameObject child;
    3.         public Sorting sorting;
    4.  
    5.  
    6. public void Convert (Entity entity, EntityManager dstManager, GameObjectConversionSystem conversionSystem) {
    7.             var childentity = dstManager.CreateEntity();
    8.             dstManager.AddComponentData (childentity, sorting);
    9.             dstManager.AddComponentObject(childentity, child.GetComponent<SpriteRenderer>());
    10.         }
    Any stuff that you relied on that gets added automatically also needs to be added manually(translation, rotation, localtoworld etc).
    I would just add that SortAuthoring or variation of it to the root object this way.
     
  9. daserra

    daserra

    Joined:
    Dec 7, 2016
    Posts:
    14

    I did that, if I use GetComponentObject for SpriteRenderer it returns me true. However I can't see the sprite itself in the game scene. Basically the entity has the SpriteRenderer with a sprite to it but for some reason I can't see the sprite in game view. Any guess?

    Thanks
     
  10. YurySedyakin

    YurySedyakin

    Joined:
    Jul 25, 2018
    Posts:
    63
  11. eizenhorn

    eizenhorn

    Joined:
    Oct 17, 2016
    Posts:
    2,685
    Sorry for late response, I’’m sick a bit, and was in a bed. ConvertToEntity runs only on parent GO.
    Code (CSharp):
    1. void Awake()
    2.         {
    3.             if (World.Active != null)
    4.             {
    5.                 // Root ConvertToEntity is responsible for converting the whole hierarchy
    6.                 if (transform.parent != null && transform.parent.GetComponentInParent<ConvertToEntity>() != null)
    7.                     return;
    8.                
    9.             ...
    10.  
    11.         }
    This is why your child Convert To Entity not work. Cause in Create And Inject it's not going through hierarchy recursive like in Convert And Destroy mode.
     
  12. thelebaron

    thelebaron

    Joined:
    Jun 2, 2013
    Posts:
    857
    You're right, this doesnt appear to work for subscenes but I couldn't find in that thread what the alternative would be(for pure ecs entity creation, I can see how to create an entity from a gameobject component conversion though). Can you point me to the alternative?
     
  13. YurySedyakin

    YurySedyakin

    Joined:
    Jul 25, 2018
    Posts:
    63
    My understanding is that you either declare referenced prefabs (or, as we did in DotsUI, assets), or you call EntityManager.CreateAdditionalEntity - this way the conversion system knows the mapping from unity objects to your entities and includes entities in the serialization process. I might be oversimplifying but that's the explanation I have. Someone like @5argon could've explained better.