Convert And Inject GameObject doesn't convert childs

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”

3 Likes

Because of the
Library/PackageCache/com.unity.entities@0.8.0-preview.8/Unity.Entities.Hybrid/ConvertToEntity.cs

void ConvertToEntitySystem::Convert()
{
    ...
    var remove = parent != null && parent.GetComponentInParent<ConvertToEntity>() != null;
    ....
}

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

        static void AddRecurse(EntityManager manager, Transform transform, HashSet<Transform> toBeDetached, List<Transform> toBeInjected)
        {
            ...

            if (IsConvertAndInject(transform.gameObject))
            {
                toBeDetached.Add(transform);
                toBeInjected.Add(transform);
            }
            else // << Why is it under else branch? Looks like a bug, causing inconsistent behaviour
            {
                foreach (Transform child in transform)
                    AddRecurse(manager, child, toBeDetached, toBeInjected);
            }
        }

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.

2 Likes

That has more sense now then!
If it is intentional and not a bug then its good

thanks a lot for your response

1 Like

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

        static void AddRecurse(EntityManager manager, Transform transform, HashSet<Transform> toBeDetached, List<Transform> toBeInjected)
        {
            ...
            if (IsConvertAndInject(transform.gameObject))
            {
                toBeDetached.Add(transform);
                toBeInjected.Add(transform);
            }
            // << Just remove the else branch
            
                foreach (Transform child in transform)
                    AddRecurse(manager, child, toBeDetached, toBeInjected);
            
        }

and that would allow you to convert Injected hierarchies

1 Like