SpriteRenderer with MaterialPropertyBlock losing custom _MainTex when disabled/enabled

Hello,

Some context, I have a cartoon animation using characters made from sprite. In order to customise the cartoon at runtime, I’m using MaterialPropertyBlocks to change the _Color color and _MainTex texture with a texture that is generated at runtime.

I noticed that when the GameObject containing the SpriteRenderer is disabled then enabled again the object renders only with the _Color attribute from the MaterialPropertyBlock but the _MainTex seems to have reverted back to the original sprite texture.

I have written a script that reassign the MaterialPropertyBlock in OnEnable() of every customized GameObject and it does the trick but I’d like to understand why the _MainTex value of the material is reverted back to the default when disabling/enabling a GameObject. (I’m using the Sprite-default material btw)

Thanks, have a nice day.

Sprite Renderer components internally use a MaterialPropertyBlock to set the sprite texture. When the Sprite Renderer is enabled, it re-assigns the sprite texture, overriding whatever you had set before. Hence why you need to re-assign OnEnable().

@bgolus Alright, that makes sense. Thank you for that info.
I’m wondering if that is documented somewhere or if we have to guess that in some way.
Also, I suppose that the Sprite Renderer get the already set MaterialPropertyBlock and just re-assigns the _MainTex, that would explain why the _Color I set remains when disabling/enabling the GameObject. Or maybe SetPropertyBlock does not override every parameter when the new block only has the _MainTex property present ? (or maybe it has to do with the [PerRendererData] set for the _MainTex and not for _Color in the sprite-default shader ? I’m a bit confused by all that ^^; )

That material properties were specifically called out in shaders using that in the past. Today it just hides the property to the inspector. Sprite shaders are the last remaining vestige of that. Sprite color is set via vertex color, not the material color.

1 Like