As title says - I’m programmatically setting up TextMeshPro/TextMeshProUGUI material properties.
For TextMeshProUGUI component, there is no problem.
But for TextMeshPro component, it creates a new instance of material every time I set up new outline thickness, and that sometimes loses other changes I made to material before it.
I’ve compared code in TMPro_Private.cs and TMPro_UGUI_Private.cs, and even though comment for both functions is “This function will create an instance of the Font Material.”, it looks like version in TMPro_UGUI_Private is more ‘advanced’ because it tries to use shared material before creating a new material.
Could functions like SetOutlineThickness(), SetFaceColor(), SetOutlineColor() in TMPro_Private.cs be changed to use shared material too?
In Unity, accessing the material property of a MeshRenderer creates an instance of that material. Accessing the sharedMaterial property does not.
The same is true for the component which uses the MeshRenderer. As such access the fontMaterial will create an instance whereas accessing the fontSharedMaterial will not.
In the case of the which uses a CanvasRenderer the behavior is different because of the CanvasRenderer. This is not more sophisticated or advanced. It is simply the result of the MeshRenderer having its own behavior in this regards in addition to sorting compared to the CanvasRenderer and Canvas system.
That behavior would be contrary to how materials behave with the MeshRenderer. This would also result on all text objects that share this material changing.
To deal with visual style changes, use a material preset if you wish to affect all text objects. Or create an instance that you then assign to all those text objects thus not modifying the shared material.
1 Like