I’m trying to set a Sprite Renderers color using MaterialPropertyBlocks.
Can’t get it to work though. I don’t want to set the color property on the sprite, since I’m only wanting to change the final color in Edit mode for some visualization.
I thought this might work:
[ExecuteAlways]
[RequireComponent(typeof(SpriteRenderer))]
public class SpriteRendererViz : MonoBehaviour {
#if UNITY_EDITOR
[System.NonSerialized] private MaterialPropertyBlock mpb = default;
private void OnWillRenderObject() {
if (Application.isPlaying) {
return;
}
SpriteRenderer renderer = GetComponent<SpriteRenderer>();
if (mpb == null) {
mpb = new MaterialPropertyBlock();
}
renderer.GetPropertyBlock(mpb);
mpb.SetColor("_BaseColor", Color.green);
mpb.SetColor("_Color", Color.green);
mpb.SetColor("_RendererColor", Color.green);
mpb.SetColor("_FillColor", Color.green);
renderer.SetPropertyBlock(mpb);
}
#endif
}
But nothing changes.
The code does execute correctly in edit mode. Just no color changes are applied.
Using URP 8.2.0 2D Renderer
Is it even possible to change the color of a SpriteRenderer using MaterialPropertyBlocks?