How to get all component values attached to an entity?

I need to get all components attached to an entity, so that i can copy them across to another entity in order to merge the entities

public unsafe static void Merge(this EntityManager em, Entity source, Entity target, bool useSourceOnConflict = false)
{
    var typeArray = em.GetComponentTypes(source, Allocator.Temp);
    var addTypeList = new NativeList<ComponentType>(Allocator.Temp);
    var conflictTypeList = new NativeList<ComponentType>(Allocator.Temp);
    for (int i = 0, len = typeArray.Length; i < len; i++)
    {
        var ct = typeArray[i];
        bool conflict = em.HasComponent(target, ct);
        if (conflict) conflictTypeList.Add(ct);
        else addTypeList.Add(ct);
    }

    var addTypes = new ComponentTypes(addTypeList.ToArray());
    em.AddComponents(target, addTypes);

    for (int i = 0; i < addTypeList.Length; i++)
    {
        var typeIndex = addTypeList[i].TypeIndex;
        var pData = em.GetComponentDataRawRO(source, typeIndex);
        var type = TypeManager.GetType(typeIndex);
        var size = UnsafeUtility.SizeOf(type);
        em.SetComponentDataRaw(target, typeIndex, pData, size);
    }

    if (useSourceOnConflict)
    {
        for (int i = 0; i < conflictTypeList.Length; i++)
        {
            var typeIndex = conflictTypeList[i].TypeIndex;
            var pData = em.GetComponentDataRawRO(source, typeIndex);
            var type = TypeManager.GetType(typeIndex);
            var size = UnsafeUtility.SizeOf(type);
            em.SetComponentDataRaw(target, typeIndex, pData, size);
        }
    }
}
2 Likes
var componentTypes = EntityManager.GetChunk(Entity).Archetype.GetComponentTypes();

If you want to get this in a job, you can use IJobChunk

public void Execute(ArchetypeChunk chunk, int index, int entityOffset)
{
    var componentTypes = chunk.Archetype.GetComponentTypes();
3 Likes

GetComponetnDataRawRO is internal in latest entities version :frowning:

6345453--705126--Screenshot 2020-09-24 at 18.37.48.png

it was always internal, if you need to access it you can create an assembly definition reference for the entities package and expose the internals for you own assembly

but that’s just for the types, is there any way to get the data?

how do you do that?

https://discussions.unity.com/t/809172

1 Like