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

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:

meshRenderer = new RenderMesh();
GameObject shipPrefab = Resources.Load<GameObject>("Prefab/ShipPrefabGameObject");

shipEntityManager = World.DefaultGameObjectInjectionWorld.EntityManager;
shipEntityPrefab = shipEntityManager.CreateEntity(typeof(Prefab),
       typeof(RenderMesh), typeof(RenderBounds),
       typeof(LocalToWorld), typeof(ShipEntityComponent));

meshRenderer.mesh = shipPrefab.GetComponentInChildren<MeshFilter>().sharedMesh;
meshRenderer.material = shipPrefab.GetComponentInChildren<MeshRenderer>().sharedMaterial;

shipEntityManager.SetSharedComponentData(shipEntityPrefab, meshRenderer);
shipEntityManager.SetComponentData(shipEntityPrefab, new RenderBounds { Value = meshRenderer.mesh.bounds.ToAABB() });

NativeArray<Entity> shipsEntityArray = new NativeArray<Entity>(ships.Count, Allocator.Temp);
shipEntityManager.Instantiate(shipEntityPrefab, shipsEntityArray);

It would be awesome to hear some thoughts and see some code.
Thanks in advance.
Slarti.

Would that help?

public static void ConvertGameObjectsToEntitiesField(GameObjectConversionSystem conversionSystem, GameObject[] gameObjects, out Entity[] entities)

For changing the color you can simply change a material parameter that your shader exposes.

See: Per-instance material params support in Entities 0.2!

1 Like

just read through this. Looks good. Will give it a try. @Joachim_Ante_1 I hope we can change Entities Prefabs on the fly.

1 Like