Hello! I have a RTS game project. The task is to create an entity in runtime from a prefab, which is defined in the SO. There is a house model with separate meshes for doors. There is a code that displays one material out of 10 in the scene and one sub mesh, how to display all the materials and sub meshes? Should I create an entity for each sub mesh? How to display additional door meshes with the correct local position? Unity 6000.043, Entities 1.3.10. (Update: I edited code a little).
private Entity ConvertGameObjectToEntityPrefab(GameObject prefab)
{
MeshFilter meshFilter = prefab.GetComponent<MeshFilter>();
MeshRenderer meshRenderer = prefab.GetComponent<MeshRenderer>();
Entity entityPrefab = EntityManager.CreateEntity(
ComponentType.ReadWrite<BuildingComponent>(),
ComponentType.ReadWrite<Prefab>(),
ComponentType.ReadWrite<LocalTransform>()
);
Material[] materials = meshRenderer.sharedMaterials;
Mesh mesh = meshFilter.sharedMesh;
int subMeshCount = mesh.subMeshCount;
var renderMeshArray = new RenderMeshArray(
materials,
new[] { mesh } // Still one mesh, but with multiple submeshes
);
var renderMeshDescription = new RenderMeshDescription(
shadowCastingMode: ShadowCastingMode.On,
receiveShadows: true
);
RenderMeshUtility.AddComponents(
entityPrefab,
EntityManager,
renderMeshDescription,
renderMeshArray,
MaterialMeshInfo.FromRenderMeshArrayIndices(0, 0)
);
EntityManager.SetComponentData(entityPrefab, LocalTransform.FromPositionRotationScale(
float3.zero,
quaternion.identity,
1f
));
return entityPrefab;
}