- Home /
How to update TMP Pro Sprite Shader using script/C#?
I am using a custom Shader for my TMP Fallback sprites which are emojis. I want to change a value on the shader dynamically based on user input. If I pause my game in the editor and change the value in the UI it works, but in c# the change is never displayed. Here is what I have tried:
var subMesh = GetComponentInChildren<TMP_SubMeshUI>();
subMesh.material.SetFloat("_GrayscaleAmount", 0f);
I've also tried fallbackMaterial, creating a whole new material and setting that, subMesh.SetMaterialDirty()
and subMesh.RefreshMaterial()
and text.ForceMeshUpdate
on the parent TextMeshProUGUI
object.
The shader values work great when I update them in the UI, it only seems to be via script that they don't get re-rendered.
Any ideas? Thank you!
Answer by Infenix · Feb 22, 2021 at 02:22 PM
You should try to use subMesh.sharedMaterial instead of subMesh.material
Thanks, that did partially work. but the icon is used in 2+ places and this updates the shader anywhere that icon is used. I need to adjust the shader only for the given icon/sprite.
Didn't see your answer. For this, you can use a trick in the awake method (or wherever you want) and do submesh.material = new Material(subMesh.sharedMaterial) soyou create a new instance of it, isolated from the rest, that you should be able use as you want without impacting other objects
@Infenix My code is subMesh.material = new Material(subMesh.sharedMaterial); subMesh.material.SetFloat("_GrayscaleAmount", 0f);
but unfortunately it still does not update. If i use that same code on the sharedMaterial it works so it's not an issue with the shader itself.