An example of changing entity colours via URP/HDRP material property authoring would be nice
One of the most common things people want to do is change the colour of things - and while some of the samples here do that, they don't do it in a way that makes it easy to, say, randomise the colour of a spawned Entity.
It's not difficult to add a URPMaterialPropertyBaseColorAuthoring component to a prefab & authoring class then access it in a System via some float4 data, but if you're learning ECS it's not obvious how all that should fit together.
To wit, you'd need a Random object in the component data struct with something like the following in the Baker:
Random = Random.CreateFromIndex(authoring.RandomSeed)
Then in the System you could have something like:
public float4 GetRandomColour(RefRW<SpawnerComponentData> spawnerComponentData)
{
float r = spawnerComponentData.ValueRW.Random.NextFloat();
float g = spawnerComponentData.ValueRW.Random.NextFloat();
float b = spawnerComponentData.ValueRW.Random.NextFloat();
float a = 1f;
return new float4(r, g, b, a);
}
And this random colour could then be used in the System's OnUpdate method via something like:
state.EntityManager.SetComponentData(newEntity, new URPMaterialPropertyBaseColor() {
Value = GetRandomColour(spawnerComponentData)
});
I would have found an example like that useful - but instead I floundered around for a day and half reading docs & watching videos until I finally cracked it. While the solution now seems obvious, the lack of an example made it harder than it should have been to piece together, and bear in mind that only questions you know the answer to are 'easy' =D