Search Unity

TextMesh Pro How to change face color of all instances?

Discussion in 'UGUI & TextMesh Pro' started by ThomasSoto, Jan 4, 2021.

  1. ThomasSoto

    ThomasSoto

    Joined:
    Aug 7, 2013
    Posts:
    26
    Hi, I'm implementing a color palette switcher in my game where I use a single material for all the Text Mesh fields. Through a script I assign the colors from a ScriptableObject to the Material. The problem is that the text fields are not updating their colors. I believe the issue is that when the game starts each field creates an instance of the material.


    material.SetColor ("m_faceColor", (Color32) colors.fontColor);


    Is there any way to update all the instances of the material? Or to avoid the instances at all and have them share a single material?
     
  2. Stephan_B

    Stephan_B

    Joined:
    Feb 26, 2017
    Posts:
    6,596
    A long established convention in Unity is the accessing the material property of a renderer will return an instance of the material whereas accessing the sharedMaterial property will return the shared material. This is the same for TMP where accessing the fontMaterial will return and instance whereas accessing the fontSharedMaterial will return the shared material.

    Unless this color palette needs to by dynamic or user defined, I would recommend creating material presets for each variant. Doing so will not only allow you to pre-define all the colors but all other material properties as well. To control the visual appearance of individual text objects, you would simply assign to the fontSharedMaterial property one of those Material Presets. See the following video if you are not familiar with Material Presets.

    Using material presets which are persistent assets which will then potentially be shared between multiple text objects will also have the benefit of reducing the amount of draw calls whereas using instances will result in (1) draw call per instance.