Search Unity

  1. We are migrating the Unity Forums to Unity Discussions. On July 12, the Unity Forums will become read-only. On July 15, Unity Discussions will become read-only until July 18, when the new design and the migrated forum contents will go live. Read our full announcement for more information and let us know if you have any questions.

Question Setting shader property through code leads to black text

Discussion in 'UGUI & TextMesh Pro' started by Fufeer, Jan 20, 2023.

  1. Fufeer

    Fufeer

    Joined:
    Jan 2, 2020
    Posts:
    7
    Hi! First of all, my apologies for the not very descriptive title, but it is hard for me to frame what my question is, or where I am failing to understand some concepts.

    I have modified the TextMeshPro Bitmap shader to add a new Color property that I called _OutlineColor. The shader can now add an outline to the text in a pixel-perfect style, with the color of the property _OutlineColor.

    However, I do not want all of the text elements with the same material to have the same _OutlineColor, so my belief (and please point me in a different direction if I am wrong) is that I should access the material instance in order to set this variable for each of the TMP elements in my scene.

    Code (CSharp):
    1. public Color outlineColor;
    2.  
    3.     private void OnValidate()
    4.     {
    5.         var text = GetComponent<TMP_Text>();
    6.         var material = text.fontMaterial;
    7.         material.SetColor("_OutlineColor", outlineColor);
    8.         text.fontMaterial = material;
    9.     }
    Doing so seems to break the material completely, and instead it displays the text and the outline in a black color. However, applying this change to the fontSharedMaterial does indeed work, but it will apply this color to all of the elements that have the same font.

    Has anybody had this problem before? I am going in a wrong direction here?