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. Dismiss Notice

Question Getting components & dynamic buffers from entity created with EntityCommandBuffer.Instantiate

Discussion in 'Entity Component System' started by Cramonky, Jun 1, 2022.

  1. Cramonky

    Cramonky

    Joined:
    Apr 1, 2013
    Posts:
    181
    Hi,

    I'm having trouble figuring out how to get component and dynamic buffer data from an entity that was created with EntityCommandBuffer.Instantiate (if it is even possible). It seems like a simple thing but I can't find anything about it.

    Using ComponentDataFromEntity and BufferFromEntity don't work, but that makes sense because the entity hasn't actually been created yet.

    The GetComponent and GetBuffer SystemBase methods don't work either, I assume for the same reason.

    My specific use case involves instantiating prefabs that have many parts connected by physics joints. Since there isn't any handling of setting the position of the children entities, I have to do it myself:
    Code (CSharp):
    1. Entity newEntity = ecb.Instantiate(spawnData.EntityPrefab);
    2.  
    3. //Set position and rotation
    4. ecb.SetComponent(newEntity, new Translation() { Value = spawnData.SpawnPosition });
    5. ecb.SetComponent(newEntity, new Rotation() { Value = spawnData.SpawnRotation });
    6.  
    7. //Set position and rotation of children, if applicable
    8. DynamicBuffer<ConnectedChildBufferElementData> connectedChildren = GetBuffer<ConnectedChildBufferElementData>(newEntity);
    9.  
    10. for (int i = 0; i < connectedChildren.Length; i++)
    11. {
    12.     ConnectedChildBufferElementData connectedChild = connectedChildren[i];
    13.     Entity childEntity = connectedChild.ChildEntity;
    14.  
    15.     float3 position = spawnData.SpawnPosition + math.mul(spawnData.SpawnRotation, connectedChild.PositionOffset);
    16.     quaternion rotation = math.mul(spawnData.SpawnRotation, connectedChild.RotationOffset);
    17.  
    18.     ecb.SetComponent(childEntity, new Translation() { Value = position });
    19.     ecb.SetComponent(childEntity, new Rotation() { Value = rotation });
    20. }
     
  2. DreamingImLatios

    DreamingImLatios

    Joined:
    Jun 3, 2017
    Posts:
    3,983
    Is this data you have already added or set via the ECB or stuff that comes from the prefab? If the latter, read it from the prefab.
     
  3. Cramonky

    Cramonky

    Joined:
    Apr 1, 2013
    Posts:
    181
    This is data already on the prefab. That would work for some cases, but it wouldn't work for anything that references another entity, since I need to get the "children" of the entity I just instantiated, not the children of the prefab.
     
  4. yifanchu183

    yifanchu183

    Joined:
    Jul 4, 2019
    Posts:
    41
    same issue, i set the prefab before Instantiate. But i hope i can set it after Instantiate for parallel instantiate