Search Unity

A question - How to change colour or prefab of an Entity when something is happening to it?

Discussion in 'Entity Component System' started by Slarti-42, Feb 29, 2020.

  1. Slarti-42

    Slarti-42

    Joined:
    Jul 1, 2019
    Posts:
    49
    Hi folks,

    I hope this question helps others as well.

    I was wondering in regards to this. Some thoughts.
    Is possible to change the shared component (renderMesh, Render Bounds, Prefab)? If yes, does it change for all the Entities?

    Would it make any sense to destroy that Entity, and make a new one (new chunk with Entities that have this condition)? Or is should I tag it and change with the first question the prefab...?

    What would be the code like, please? In my code I do following to create my Entities:


    Code (CSharp):
    1. meshRenderer = new RenderMesh();
    2. GameObject shipPrefab = Resources.Load<GameObject>("Prefab/ShipPrefabGameObject");
    3.  
    4. shipEntityManager = World.DefaultGameObjectInjectionWorld.EntityManager;
    5. shipEntityPrefab = shipEntityManager.CreateEntity(typeof(Prefab),
    6.        typeof(RenderMesh), typeof(RenderBounds),
    7.        typeof(LocalToWorld), typeof(ShipEntityComponent));
    8.  
    9. meshRenderer.mesh = shipPrefab.GetComponentInChildren<MeshFilter>().sharedMesh;
    10. meshRenderer.material = shipPrefab.GetComponentInChildren<MeshRenderer>().sharedMaterial;
    11.  
    12. shipEntityManager.SetSharedComponentData(shipEntityPrefab, meshRenderer);
    13. shipEntityManager.SetComponentData(shipEntityPrefab, new RenderBounds { Value = meshRenderer.mesh.bounds.ToAABB() });
    14.  
    15. NativeArray<Entity> shipsEntityArray = new NativeArray<Entity>(ships.Count, Allocator.Temp);
    16. shipEntityManager.Instantiate(shipEntityPrefab, shipsEntityArray);
    17.  
    It would be awesome to hear some thoughts and see some code.
    Thanks in advance.
    Slarti.
     
    Last edited: Mar 2, 2020
  2. Slarti-42

    Slarti-42

    Joined:
    Jul 1, 2019
    Posts:
    49
    Would that help?

    Code (CSharp):
    1. public static void ConvertGameObjectsToEntitiesField(GameObjectConversionSystem conversionSystem, GameObject[] gameObjects, out Entity[] entities)
    2.  
     
  3. siggigg

    siggigg

    Joined:
    Apr 11, 2018
    Posts:
    247
    Slarti-42 likes this.
  4. Slarti-42

    Slarti-42

    Joined:
    Jul 1, 2019
    Posts:
    49
    just read through this. Looks good. Will give it a try. @Joachim_Ante I hope we can change Entities Prefabs on the fly.
     
    siggigg likes this.