Search Unity

  1. Unity 2019.2 is now released.
    Dismiss Notice

Getting 1 chunk per entity with SharedComponentData

Discussion in 'Data Oriented Technology Stack' started by Mike37, Jul 26, 2019.

  1. Mike37

    Mike37

    Joined:
    Oct 21, 2018
    Posts:
    18
    I'm using Unity 2019.1.11f1 with Entities 0.0.12-preview.33. I have some code roughly like the following:

    Code (CSharp):
    1. // in Start(), initialize some variables
    2. manager = World.Active.EntityManager;
    3. enemyArchetype = manager.CreateArchetype(typeof(RenderMesh), /* more components... */);
    4. renderMesh = new RenderMesh
    5. {
    6.     material = EnemyMaterial,
    7.     mesh = EnemyMesh
    8. };
    9.  
    10.    
    11. // in Update()  
    12. if (Input.GetKeyDown(KeyCode.Space))
    13. {
    14.     Entity enemy = manager.CreateEntity(enemyArchetype);
    15.     manager.SetSharedComponentData(enemy, renderMesh);
    16. }
    Every time I press spacebar, it creates a new entity and a new chunk (so 10 entities would mean 10 chunks). I'm guessing this is because when it creates the entity, the RenderMesh isn't yet set and therefore doesn't match the other entities, and it doesn't condense them once I give it the same RenderMesh.

    Is this intended behaviour? How can I ensure the enemies are added to an existing chunk?
     
  2. DreamingImLatios

    DreamingImLatios

    Joined:
    Jun 3, 2017
    Posts:
    357
    Is the source of the material from a GameObject renderer? If so, it might be cloning the material every time you fetch it.
     
  3. Mike37

    Mike37

    Joined:
    Oct 21, 2018
    Posts:
    18
    The material is set from the editor. I have a public field in my class, like this:

    Code (CSharp):
    1. public class EnemySpawner : MonoBehaviour
    2. {
    3.     public Material EnemyMaterial;
    4.     public Mesh EnemyMesh;
    5.  
    6.     private RenderMesh renderMesh;
    7.     // more stuff...
    8.  
    9.     void Start()
    10.     {
    11.         renderMesh = new RenderMesh
    12.         {
    13.             material = EnemyMaterial,
    14.             mesh = EnemyMesh
    15.         };
    16.  
    17.         // more code...
    18.     }
    19. }
     
  4. DreamingImLatios

    DreamingImLatios

    Joined:
    Jun 3, 2017
    Posts:
    357
    Not sure. Only other thing I can think of is that you got a dynamic buffer with a huge capacity in the archetype. Instead of instantiating from an archetype, could you use the archetype to create a prefab and then instantiate the prefab? I'd be curious if that still created unique chunks. How are you determining the chunk count?
     
  5. Mike37

    Mike37

    Joined:
    Oct 21, 2018
    Posts:
    18
    The Entity Debugger shows the number of chunks.

    It seems if I do manager.Instantiate(someOtherEnemy), then it gets added to an existing chunk. I could use that as a workaround, but it's inconvenient because I need an enemy to clone.
     
    sngdan likes this.
  6. Mike37

    Mike37

    Joined:
    Oct 21, 2018
    Posts:
    18
    Does anyone know if this will be fixed in a future version?
     
  7. Mike37

    Mike37

    Joined:
    Oct 21, 2018
    Posts:
    18
    Just an update: you still get a single entity per chunk if you instantiate from a prefab. I'm on Entities 0.1.1 now.

    So if you use:
    Code (CSharp):
    1. enemyArchetype = manager.CreateArchetype(typeof(RenderMesh), typeof(Prefab), /* more components... */);
    2. enemyPrefab = manager.CreateEntity(enemyArchetype);
    3. manager.SetSharedComponentData(enemyPrefab, /* set RenderMesh here */);
    4.  
    5. // Some time later...
    6. manager.Instantiate(enemyPrefab);
    It won't help.