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.

Question Use a generated Mesh for a baked entity prefab

Discussion in 'Entity Component System' started by Salmakis, Nov 29, 2022.

  1. Salmakis

    Salmakis

    Joined:
    May 14, 2018
    Posts:
    15
    Hey, the new baking workflow is nice :)
    But i wanted to do to following:
    bake an entity prefab with a mesh that is generated by the start of the game once, for all entities for that type then.

    i wanted to do it like so:

    Code (CSharp):
    1.    
    2. public class LivingControllerSingletonAuthoring : SingletonBehaviour<LivingControllerSingletonAuthoring> {
    3.     public GameObject livingPrefab;
    4. }
    5.  
    6. public class LivingControllerSingletonBaker : Baker<LivingControllerSingletonAuthoring> {
    7.  
    8.     public override void Bake(LivingControllerSingletonAuthoring authoring)
    9.     {
    10.       authoring.livingPrefab.GetComponent<MeshFilter>().mesh =    EntityMeshManager.GetEntityMesh(EntityMeshManager.EntityType.Person);
    11.  
    12.  
    13.       AddComponent<LivingControllerData>(new LivingControllerData
    14.          {
    15.             livingPrefabEntity = GetEntity(authoring.livingPrefab)
    16.          });
    17.        }
    18.    }
    19.  
    20.    public struct LivingControllerData : IComponentData {
    21.        public Entity livingPrefabEntity;
    22.    }
    23.  
    when i do:
    authoring.livingPrefab.GetComponent<MeshFilter>().mesh = EntityMeshManager.GetEntityMesh(EntityMeshManager.EntityType.Person);

    then the mesh is changed on the prefab, haha of course, i forgot that.

    then i tried to use GameObject.Instantiate to create a temp instance of the prefab, change the mesh, and use that instance then as prefab for the entityPrefab, and destroy it afterwards
    that created alot of GameObjects.

    i feels like the whole idea of seting a generated mesh in the baking process seems to be the wrong approach, but when and where could i intercept to run my mesh generator (wich is managed) to create the mesh i want to use for the entities?
     
  2. Mayumichi

    Mayumichi

    Joined:
    Mar 12, 2017
    Posts:
    29
    heu3becteh likes this.