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

Create parent-child at runtime

Discussion in 'Entity Component System' started by andywatts, Mar 8, 2022.

  1. andywatts

    andywatts

    Joined:
    Sep 19, 2015
    Posts:
    107
    I can create a parent and child entities at runtime using the below. Works great.
    Code (CSharp):
    1.     protected override void OnUpdate()
    2.     {
    3.         if (!runOnce)
    4.         {
    5.             var parentPrefab = GetSingleton<ParentPrefab>().Value;
    6.             var parent = EntityManager.Instantiate(parentPrefab);
    7.          
    8.             var cubePrefab = GetSingleton<CubePrefab>().Value;
    9.             var cube = EntityManager.Instantiate(cubePrefab);
    10.          
    11.             EntityManager.AddComponentData(cube, new Parent {Value = cube});
    12.             EntityManager.AddComponentData(cube, new LocalToParent {Value = new float4x4(1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1)});
    13.             var children = EntityManager.AddBuffer<Child>(parent);
    14.             children.Add(new Child {Value = cube});
    15.  
    16.             var q = quaternion.EulerXYZ(0, 45 * Mathf.Deg2Rad, 0);
    17.             SetComponent(parent, new Rotation {Value = q});
    18.          
    19.             runOnce = true;
    20.         }
    21.     }
     
  2. andywatts

    andywatts

    Joined:
    Sep 19, 2015
    Posts:
    107
    Works great until adding Rotation to parent.. then LocalToParentSystem throws a stackoverflow.
     
  3. DreamingImLatios

    DreamingImLatios

    Joined:
    Jun 3, 2017
    Posts:
    3,983
    Don't Add the Child buffer. Let Unity handle that.
     
  4. andywatts

    andywatts

    Joined:
    Sep 19, 2015
    Posts:
    107
    Thanks @DreamingImLatios
    Also needed LocalToWorld for ParentSystem's query.

    Here's a working version for posterity.


    Code (CSharp):
    1.     protected override void OnUpdate()
    2.     {
    3.         if (!runOnce)
    4.         {
    5.             var parentPrefab = GetSingleton<ParentPrefab>().Value;
    6.             var parent = EntityManager.Instantiate(parentPrefab);
    7.            
    8.             var childPrefab = GetSingleton<ChildPrefab>().Value;
    9.             var child = EntityManager.Instantiate(childPrefab);
    10.            
    11.             // Add Parent and friends for ParentSystem's updateNewParents
    12.             EntityManager.AddComponentData(child, new Parent {Value = parent});
    13.             EntityManager.AddComponentData(child, new LocalToParent {Value = new float4x4(1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1)});
    14.             EntityManager.AddComponentData(child, new LocalToWorld() {Value = new float4x4(1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1)});
    15.            
    16.            
    17.             // Set Rotation on parent
    18.             var q = quaternion.EulerXYZ(0, 45 * Mathf.Deg2Rad, 0);
    19.             SetComponent(parent, new Rotation {Value = q});
    20.            
    21.             runOnce = true;
    22.         }
    23.     }
     
    Baggers_ likes this.