Search Unity

Resolved Enable / disable TextMeshPro material property via script

Discussion in 'Scripting' started by esteiner44, May 10, 2022.

  1. esteiner44

    esteiner44

    Joined:
    Feb 5, 2022
    Posts:
    8
    Hello everyone,

    I would like to configure and activate/deactivate the 'Underlay' option of a text written with TextMeshPro but I don't know how to access that property.

    I have been looking for information and I think I need to change the shader of the component, but I am not sure if that is what I need and I am not sure how to do it because I am a newbie in this, Can someone help me a little?

    PS: Sorry for my english

    Best regards
     
  2. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,187
    What do you mean by Underlay?
     
  3. esteiner44

    esteiner44

    Joined:
    Feb 5, 2022
    Posts:
    8
    I'm sorry I didn't explain myself well. I attach an image with the property that I would like to activate.

    Best regards
     

    Attached Files:

  4. karliss_coldwild

    karliss_coldwild

    Joined:
    Oct 1, 2020
    Posts:
    599
    That's not a property of text component that's a property of material. You should start with reading unity docs or some tutorials of how to change material properties. After you understand the concept you might have to read the corresponding shader code to find out the exact name of properties that you want to change since the actual names in code are not identical with human readable labels shown in editor.
     
  5. esteiner44

    esteiner44

    Joined:
    Feb 5, 2022
    Posts:
    8
    I already got it. Thanks for the directions.

    To enable and disable the property I have used this:
    Code (CSharp):
    1. //Enable
    2. transform.GetChild(selected).GetComponent<TextMeshProUGUI>().fontSharedMaterial.EnableKeyword("UNDERLAY_ON");
    3. //Disable
    4. transform.GetChild(selected).GetComponent<TextMeshProUGUI>().fontSharedMaterial.DisableKeyword("UNDERLAY_ON");
    Unfortunately, I realized that this is not a good option for me because there are several elements on the screen that use the same material, so I have thought of duplicating the font and changing that (I already said I was a beginner :)).

    In case it helps someone, I also leave the line that I think should be used to change the properties:
    Code (CSharp):
    1. transform.GetChild(selected).GetComponent<TextMeshProUGUI>().fontSharedMaterial.SetFloat("NAME_OF_PROPERTY", 0f);
    To see the name of the properties I have only selected the shader in the TextMeshPro folders and the options appear in the inspector.

    Best regards
     
    karliss_coldwild likes this.
  6. kdgalla

    kdgalla

    Joined:
    Mar 15, 2013
    Posts:
    4,627
    When you change the properties of a material, Unity will automatically create a separate instance of the material, so that the one object will have the changed material, but other objects will still have the original material with its original properties.