Search Unity

Question Debugging Runtime generation with prefab hierarchy

Discussion in 'Graphics for ECS' started by sstrong, Mar 22, 2023.

  1. sstrong

    sstrong

    Joined:
    Oct 16, 2013
    Posts:
    2,252
    Previously at runtime I used ConvertGameObjectHierarchy. See here.

    For ECS 1.0 in U2022.2.1 I'm attempting to create an Entity (with child entities) to replicate what I had previously.

    Code (CSharp):
    1.  
    2. public List<Entity> asteriodPrefabEntityList;..
    3.  
    4. asteriodPrefabEntityList = new List<Entity>();
    5.  
    6. // Create the parent entity components
    7. Entity prefabEntity = entityManager.CreateEntity();
    8.  
    9. entityManager.AddComponent(prefabEntity, typeof(LocalTransform));
    10. entityManager.AddComponent(prefabEntity, typeof(Asteroid));
    11. entityManager.AddComponent(prefabEntity, typeof(PostTransformScale));
    12.  
    13. MeshRenderer mRen;
    14. MeshFilter mFilter;
    15.  
    16. Transform prefabRootTransform = obstaclePrefabs[pfIdx];
    17.  
    18. // Is there a mesh renderer and filter on the prefab root gameobject?
    19. if (prefabRootTransform.TryGetComponent(out mRen) && mRen.TryGetComponent(out mFilter))
    20. {
    21.     // Create a RenderMeshDescription with named parameters.
    22.     var desc = new RenderMeshDescription(
    23.         shadowCastingMode: mRen.shadowCastingMode,
    24.         receiveShadows: mRen.receiveShadows);
    25.  
    26.     // Create an array of mesh and material required for runtime rendering.
    27.     var renderMeshArray = new RenderMeshArray(mRen.sharedMaterials, new Mesh[] { mFilter.sharedMesh });
    28.  
    29.     RenderMeshUtility.AddComponents
    30.     (
    31.         entity: prefabEntity,
    32.         entityManager: entityManager,
    33.         renderMeshDescription: desc,
    34.         renderMeshArray: renderMeshArray,
    35.         materialMeshInfo: MaterialMeshInfo.FromRenderMeshArrayIndices(0, 0)
    36.     );
    37. }
    38.  
    39. // Find non-recursive child transform (we could use GetComponentsInChildren but will add more complexity)
    40. foreach (Transform childTfrm in prefabRootTransform)
    41. {
    42.     // Is there a mesh renderer and filter on the prefab's child transform?
    43.     if (childTfrm.TryGetComponent(out mRen) && childTfrm.TryGetComponent(out mFilter))
    44.     {
    45.         Entity childEntity = entityManager.CreateEntity();
    46.         entityManager.AddComponentData(childEntity, new LocalTransform() { Position = childTfrm.localPosition, Rotation = childTfrm.localRotation, Scale = 1 });
    47.         entityManager.AddComponentData(childEntity, new Parent() { Value = prefabEntity });
    48.  
    49.         // Create a RenderMeshDescription with named parameters.
    50.         var desc = new RenderMeshDescription(
    51.             shadowCastingMode: mRen.shadowCastingMode,
    52.             receiveShadows: mRen.receiveShadows);
    53.  
    54.         // Create an array of mesh and material required for runtime rendering.
    55.         var renderMeshArray = new RenderMeshArray(mRen.sharedMaterials, new Mesh[] { mFilter.sharedMesh });
    56.  
    57.         RenderMeshUtility.AddComponents
    58.         (
    59.             entity: childEntity,
    60.             entityManager: entityManager,
    61.             renderMeshDescription: desc,
    62.             renderMeshArray: renderMeshArray,
    63.             materialMeshInfo: MaterialMeshInfo.FromRenderMeshArrayIndices(0, 0)
    64.         );
    65.     }
    66. }
    67.  
    68. asteriodPrefabEntityList.Add(prefabEntity);
    69.  
    70. ...
    71. // Instantiate entities
    72. prefabEntity = asteriodPrefabEntityList[i];
    73. Entity entity = entityManager.Instantiate(prefabEntity);
    74. // SetComponentData...
    If I have a mesh filter and renderer on the root transform of the prefab I'm converting it all works fine. However, if the prefab has an empty gameobject, at the root, and one or more children with a mesh, the entities don't render.

    If I have a mesh on the root and the children, it also renders in the scene. My initial entities that get created from the prefab seem ok (but only if I add the mesh to the root gameobject of the prefab).

    upload_2023-3-22_10-9-5.png

    upload_2023-3-22_10-11-8.png

    How can I debug this issue? What should I be looking at?