Search Unity

TextMesh Pro Outline Color Not Updating Properly

Discussion in 'UGUI & TextMesh Pro' started by TinyTitanAndrew, Jun 5, 2018.

  1. TinyTitanAndrew

    TinyTitanAndrew

    Joined:
    Sep 11, 2017
    Posts:
    9
    When using TMP, I can successfully change the outline color in a few ways. Unfortunately, I'm not able to change it for the behaviour I'm looking for.

    Here is some code:

    Code (CSharp):
    1.  
    2. for (int i = 0; i < TextMeshes.Count; i++)
    3.             {
    4.                 var TMesh = TextMeshes[i];
    5.              
    6.                 if (TMesh.name.Contains("_NoOutline"))
    7.                 {
    8.                     TMesh.font = Game.Resources.GetAsset<TMP_FontAsset>(AB.fonts, NoOutlineFontName);
    9.                 }
    10.                 else if (TMesh.name.Contains("_Outline"))
    11.                 {
    12.                     var PrevColor = TMesh.outlineColor;
    13.                     Debug.LogError("Label: " + TMesh.name + "  PrevColor = " + PrevColor);
    14.  
    15.                     TMesh.font = Game.Resources.GetAsset<TMP_FontAsset>(AB.fonts, OutlineFontName);
    16.                     Game.RunNextFrame(() => TMesh.outlineColor = PrevColor);
    17.  
    18.                     Debug.LogError("Label: " + TMesh.name + "  TMesh.outlineColor = " + TMesh.outlineColor);
    19.                 }
    20.             }
    21.  
    TextMeshes is assigned by grabbing all of the TextMeshProUGUI components from my canvas, it goes through a for loop and creates TMesh within the for loop.

    The intended behaviour is this:
    Grab the outline colour of the current font -> Change font -> Change new font's outline colour to the old one.

    This is to keep the same outline colour as my game is using several different outline colours for different text elements. The odd thing is that instead of doing this
    Code (CSharp):
    1. TMesh.outlineColor = PrevColor
    I do this
    Code (CSharp):
    1. TMesh.outlineColor = Color.blue
    it will change perfectly fine.

    I have tried with TMesh as a var, Color and Color32, none of which make it work. All that happens is the outlineColor will remain the same color as the font's outline color when set up in the scene.
     
  2. Stephan_B

    Stephan_B

    Joined:
    Feb 26, 2017
    Posts:
    6,595
    Properties like faceColor, outlineColor and outlineWidth are provided for convenience. These are actually properties of the shader / material used by the text object which are _FaceColor, _OutlineColor and _OutlineWidth.

    As per the summary / intellisense on those properties, accessing them results in an instance of the material currently assigned to the text object being created. This behavior is consistent with established Unity conventions where accessing the material properties of an object results in an instance of that material being created. By contrast, accessing the sharedMaterial of an object will result in changes being reflected on all objects that share this material.

    In other words, when accessing the outlineColor, the assumption is that you only want to change the outlineColor of that object and not of all other objects. This is why an instance of the material is being created.

    You have to keep in mind that each instance material also results in a draw call per material so in cases where you absolutely need each object to have their own unique material properties, you use instances but in cases where several objects share the same visual appearance, using material presets is much more efficient as all these object would batch with each other thus reducing draw calls.

    In the case of your code, you are likely changing intermixing / setting these properties on the wrong instance of the material.

    So if several text objects will share the same font and visual appearance, create a material preset for each of these visual variances per font asset and then simply assign the correct material preset to these objects when needed. You will need to create a set of material presets for each font assets which were created from different source font files (.ttf, otf, .ttc, etc.).

    There are several posts covering this information on the TextMesh Pro user forum and here on the Unity forum. There are also several videos on my YouTube channel covering the Font Asset Creation and Material Presets.