Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

TextMeshPro set_outlineColor error in Editor

Discussion in 'UGUI & TextMesh Pro' started by LHAppri, Nov 2, 2018.

  1. LHAppri

    LHAppri

    Joined:
    Aug 18, 2017
    Posts:
    20
    I have a prefab containing a TextMesh Pro object which has a colored outline. There is a script on the main prefab and changing settings on this script change various color options, including the TextMesh Pro color and outlinecolor. Because I want to see immediately what it looks like, the script uses the [ExecuteInEditMode] attribute.
    However, this spams the console with errors

    Instantiating material due to calling renderer.material during edit mode. This will leak materials into the scene. You most likely want to use renderer.sharedMaterial instead.
    UnityEngine.Renderer:get_material()
    TMPro.TextMeshPro:SetOutlineColor(Color32) (at Packages/com.unity.textmeshpro/Scripts/Runtime/TMPro_Private.cs:841)
    TMPro.TMP_Text:set_outlineColor(Color32) (at Packages/com.unity.textmeshpro/Scripts/Runtime/TMP_Text.cs:360)


    Any ideas?
     
  2. MSplitz-PsychoK

    MSplitz-PsychoK

    Joined:
    May 16, 2015
    Posts:
    1,278
    It's generally not a great idea to modify the ".material" property of a renderer at edit time.

    The quick solution to fix this error is to use ".sharedMaterial" instead of ".material" when editing material properties. Be careful, doing this will apply your changes to the material permanently, both in play mode and edit mode!

    Why?
    When you edit material settings on a renderer using the ".material" property, Unity will automatically make a copy of your material, apply your changes to the copy, and set your renderer to use that copy. This is to prevent other renderers that use the same material from receiving the same changes.

    When these extra material copies are created during run mode, they are stored in an internal list and unloaded when you exit run mode. When these extra material copies are created in edit mode, they are not properly tracked and never get deleted, and will continue to eat up your RAM as "leaked memory" until your operating system eventually detects that it isn't being used.
     
  3. LHAppri

    LHAppri

    Joined:
    Aug 18, 2017
    Posts:
    20
    I know using .material is not a good idea ... that's why the error exists. The problem is that the TextMeshPro files are now part of Unity built-in packages and not something I can change.
     
  4. Stephan_B

    Stephan_B

    Joined:
    Feb 26, 2017
    Posts:
    6,595
    These warnings are not specific to TMP but to the creation of material instances in the editor.

    Instead of using the properties of the text object to change the outline (for example) create your own (single) instance of the .sharedMaterial and then modified the material properties directly on it. See the following post as well as this one.

    By managing your own instance of the material, you should be able to avoid the warnings.
     
  5. MSplitz-PsychoK

    MSplitz-PsychoK

    Joined:
    May 16, 2015
    Posts:
    1,278
    You can avoid the errors by modifying the material directly. Here are 2 methods, I recommend the first.

    This method will work for sure, but will modify the original material, so the change will be permanent and affect all objects that use the material:
    textMeshObject.fontSharedMaterial.SetColor("_OutlineColor", *your color*);


    This method will only affect the one object, but may not be saved properly (I'm really not sure how it's saved at all). It might cause a silent memory leak, and the change might go away when you reload the scene:
    textMeshObject.fontMaterial.SetColor("_OutlineColor", *your color*);



    I actually understand now that OP's issue is that compiled TextMeshPro code modifies a .material property when you try to modify the .outlineColor property of the TextMeshPro object.
     
  6. Stephan_B

    Stephan_B

    Joined:
    Feb 26, 2017
    Posts:
    6,595
    To modify the material properties, the following should be used instead of .SetFloat with string. Use the ShaderUtilities with ID for whatever property you wish to modify.

    Code (csharp):
    1.  
    2. m_TextMeshPro.fontSharedMaterial.SetFloat("_GlowPower", 0.5f);
    3.  
    4. // Instead of using a string to access the material property, you could use the ShaderUtilities class I provide
    5. m_TextMeshPro.fontSharedMaterial.SetFloat(ShaderUtilities.ID_GlowPower, 0.5f);
    6.  
    7. // Since some of the material properties can affect the mesh (size) you would need to update the padding values.
    8. m_TextMeshPro.UpdateMeshPadding();
    9.  
     
  7. LHAppri

    LHAppri

    Joined:
    Aug 18, 2017
    Posts:
    20
    Thanks, I'll give those a try.