Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

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?