Search Unity

Getting a Material Into An Entity

Discussion in 'Data Oriented Technology Stack' started by Filter_Feeder, Jul 2, 2021.

  1. Filter_Feeder

    Filter_Feeder

    Joined:
    Oct 19, 2017
    Posts:
    22
    Sorry if this question is a bit too basic for an entire forum thread, it's hard to find good answers to my specific case.

    I have a system which creates and stores entities into a native array, and then updates on them. I'm doing this in "pure ECS", specifying all components manually and not using the conversion function, which is fine for me.

    I'm trying to add a material to the entities I'm creating. I obviously can't reference a material through the unity editor, so I have to somehow get this information into the system using other methods. I could create the entities in a class that inherits from MonoBehaviour and simply reference the material, but I'm then stuck with another problem -I need to have the native array stored or somehow accesible through the system which updates on them.
     
  2. francois85

    francois85

    Joined:
    Aug 11, 2015
    Posts:
    1,634
    I'm also curious how ppl handle this, here is what I would have done, probably not the best way.

    Code (CSharp):
    1.          
    2.              Entities
    3.                 .ForEach(( Entity e, RenderMesh renderMesh,) =>
    4.                 {
    5.                     _material = Resources.Load("MyMaterialName", typeof(Material)) as Material;
    6.                  
    7.                     EntityManager.SetSharedComponentData(e, new RenderMesh
    8.                     {
    9.                         material = _material,
    10.                         mesh = renderMesh.mesh,
    11.                         castShadows = renderMesh.castShadows,
    12.                         receiveShadows = false
    13.                     });
    14.                 }).WithStructuralChanges().WithoutBurst().Run();
     
  3. Filter_Feeder

    Filter_Feeder

    Joined:
    Oct 19, 2017
    Posts:
    22
    Thanks a lot! I managed to get it to work but finding the standard mesh sphere was a bit trickier. I could have used "create primitive" and then access the GameObject from there, but ended up using a prefab and loading it from the resources instead.

    Regardless, it would certainly improve my understanding of ECS if someone would have an idea of how to move a NativeArray from a MonoBehaviour to a System.

    Code (CSharp):
    1. unitMaterial =  Resources.Load("EntityMaterial", typeof(Material)) as Material;
    2.         gameObjectPrefab = Resources.Load("SphereObject", typeof(GameObject)) as GameObject;
    3.         unitMesh = gameObjectPrefab.GetComponent<MeshFilter>().sharedMesh;
     
  4. francois85

    francois85

    Joined:
    Aug 11, 2015
    Posts:
    1,634
    I imagine you would use the EntireManager in your monobehavior to create an entity with a IBufferComponet and then in your system get a nativeArray from the buffer. Not sure if this is best/fastest or possible.
     
unityunity