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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Convert And Inject GameObject doesn't convert childs

Discussion in 'Entity Component System' started by Extrys, Mar 5, 2020.

  1. Extrys

    Extrys

    Joined:
    Oct 25, 2017
    Posts:
    329
    Convert And Inject Game Object doesnt convert childs right now
    but when you use "Convert And Destroy" all the hyerarchy is converted,
    it should be the same behaviour for "Convert And Inject GameObject"
     
    Xisor, kro11 and Enzi like this.
  2. Xisor

    Xisor

    Joined:
    Jan 30, 2016
    Posts:
    8
    Because of the
    ``` Library/PackageCache/com.unity.entities@0.8.0-preview.8/Unity.Entities.Hybrid/ConvertToEntity.cs ```
    Code (CSharp):
    1. void ConvertToEntitySystem::Convert()
    2. {
    3.     ...
    4.     var remove = parent != null && parent.GetComponentInParent<ConvertToEntity>() != null;
    5.     ....
    6. }
    children entity is removed from the original list, I assume its done because the parent should handle the hierarchy conversion.

    But then when traversing parent hierarchy
    Code (CSharp):
    1.         static void AddRecurse(EntityManager manager, Transform transform, HashSet<Transform> toBeDetached, List<Transform> toBeInjected)
    2.         {
    3.             ...
    4.  
    5.             if (IsConvertAndInject(transform.gameObject))
    6.             {
    7.                 toBeDetached.Add(transform);
    8.                 toBeInjected.Add(transform);
    9.             }
    10.             else // << Why is it under else branch? Looks like a bug, causing inconsistent behaviour
    11.             {
    12.                 foreach (Transform child in transform)
    13.                     AddRecurse(manager, child, toBeDetached, toBeInjected);
    14.             }
    15.         }
    it just skips the hierarchy if the mode is `ConvertAndInjectGameObject`, which looks like a bug. I fixed it locally adding hierarchy unconditionally, however its not a proper fix, because adding a `ConvertToEntity` component to children with Inject mode would also break the hierarchy in the end, causing children to be detached from parents. To fix this you also need to filter them from `toBeDetached` hashSet. Or you can just hack it the same way I did, remember to not use `ConvertToEntity` components in your children and wait for a proper fix.
     
    Last edited: Apr 4, 2020
    Extrys and starikcetin like this.
  3. Extrys

    Extrys

    Joined:
    Oct 25, 2017
    Posts:
    329
    That has more sense now then!
    If it is intentional and not a bug then its good

    thanks a lot for your response
     
    romeo-carx-tech likes this.
  4. Xisor

    Xisor

    Joined:
    Jan 30, 2016
    Posts:
    8
    I think intent was to convert hierarchies with a single parent conversion policy I guess, but implementation of this intent is wrong.
    You can hack it locally changing `Library/PackageCache/com.unity.entities@0.8.0-preview.8/Unity.Entities.Hybrid/ConvertToEntity.cs` to
    Code (CSharp):
    1.         static void AddRecurse(EntityManager manager, Transform transform, HashSet<Transform> toBeDetached, List<Transform> toBeInjected)
    2.         {
    3.             ...
    4.             if (IsConvertAndInject(transform.gameObject))
    5.             {
    6.                 toBeDetached.Add(transform);
    7.                 toBeInjected.Add(transform);
    8.             }
    9.             // << Just remove the else branch
    10.            
    11.                 foreach (Transform child in transform)
    12.                     AddRecurse(manager, child, toBeDetached, toBeInjected);
    13.            
    14.         }
    and that would allow you to convert Injected hierarchies
     
    anarkiastd likes this.