Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Question How to get all component values attached to an entity?

Discussion in 'Entity Component System' started by Stroustrup, Sep 23, 2020.

  1. Stroustrup

    Stroustrup

    Joined:
    May 18, 2020
    Posts:
    142
    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
     
  2. Lieene-Guo

    Lieene-Guo

    Joined:
    Aug 20, 2013
    Posts:
    547
    Code (CSharp):
    1.  
    2. public unsafe static void Merge(this EntityManager em, Entity source, Entity target, bool useSourceOnConflict = false)
    3. {
    4.     var typeArray = em.GetComponentTypes(source, Allocator.Temp);
    5.     var addTypeList = new NativeList<ComponentType>(Allocator.Temp);
    6.     var conflictTypeList = new NativeList<ComponentType>(Allocator.Temp);
    7.     for (int i = 0, len = typeArray.Length; i < len; i++)
    8.     {
    9.         var ct = typeArray[i];
    10.         bool conflict = em.HasComponent(target, ct);
    11.         if (conflict) conflictTypeList.Add(ct);
    12.         else addTypeList.Add(ct);
    13.     }
    14.  
    15.     var addTypes = new ComponentTypes(addTypeList.ToArray());
    16.     em.AddComponents(target, addTypes);
    17.  
    18.     for (int i = 0; i < addTypeList.Length; i++)
    19.     {
    20.         var typeIndex = addTypeList[i].TypeIndex;
    21.         var pData = em.GetComponentDataRawRO(source, typeIndex);
    22.         var type = TypeManager.GetType(typeIndex);
    23.         var size = UnsafeUtility.SizeOf(type);
    24.         em.SetComponentDataRaw(target, typeIndex, pData, size);
    25.     }
    26.  
    27.     if (useSourceOnConflict)
    28.     {
    29.         for (int i = 0; i < conflictTypeList.Length; i++)
    30.         {
    31.             var typeIndex = conflictTypeList[i].TypeIndex;
    32.             var pData = em.GetComponentDataRawRO(source, typeIndex);
    33.             var type = TypeManager.GetType(typeIndex);
    34.             var size = UnsafeUtility.SizeOf(type);
    35.             em.SetComponentDataRaw(target, typeIndex, pData, size);
    36.         }
    37.     }
    38. }
    39.  
     
    Stroustrup and apkdev like this.
  3. tertle

    tertle

    Joined:
    Jan 25, 2011
    Posts:
    3,753
    Code (CSharp):
    1. var componentTypes = EntityManager.GetChunk(Entity).Archetype.GetComponentTypes();
    If you want to get this in a job, you can use IJobChunk

    Code (CSharp):
    1. public void Execute(ArchetypeChunk chunk, int index, int entityOffset)
    2. {
    3.     var componentTypes = chunk.Archetype.GetComponentTypes();
     
    Last edited: Sep 24, 2020
    PhilSA, apkdev and Lieene-Guo like this.
  4. Stroustrup

    Stroustrup

    Joined:
    May 18, 2020
    Posts:
    142
    GetComponetnDataRawRO is internal in latest entities version :(
     

    Attached Files:

  5. brunocoimbra

    brunocoimbra

    Joined:
    Sep 2, 2015
    Posts:
    679
    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
     
  6. Stroustrup

    Stroustrup

    Joined:
    May 18, 2020
    Posts:
    142
    but that's just for the types, is there any way to get the data?
     
  7. Stroustrup

    Stroustrup

    Joined:
    May 18, 2020
    Posts:
    142
    how do you do that?
     
  8. Lieene-Guo

    Lieene-Guo

    Joined:
    Aug 20, 2013
    Posts:
    547