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
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

TMPro update all materials colors

Discussion in 'UGUI & TextMesh Pro' started by BeginnerPolymath, Jun 9, 2020.

  1. BeginnerPolymath

    BeginnerPolymath

    Joined:
    Jul 31, 2015
    Posts:
    38
    I have a lot of text components and I'm trying to update their colors. I've created new material for the font and I'm changing the color with
    Code (CSharp):
    1. TextMaterial.SetColor(ShaderUtilities.ID_FaceColor, TextColors[ID]);
    And the color changes in the material, but visually it does not change in the texts themselves until you restart the application or manually change the color of the material. How do I update the entire drawing to change the color?

    Also the material is as if blocked, I can not change it for specific texts, but not for new ones.

    Upd:
    The problem is using a mask, but I don't know how to solve it. So far, I just turned off the mask.
     
    Last edited: Jun 9, 2020
  2. Stephan_B

    Stephan_B

    Unity Technologies

    Joined:
    Feb 26, 2017
    Posts:
    6,588
    Any reason you trying to change the color of the text using face color instead of Vertex Color or not using a Color Gradient Preset which can easily be shared between text objects?

    Changing the _FaceColor of the fontMaterial will result in an instance of the material being created. As such it will be unique per text object. Same if the UI Mask is used. Unless you need a UI Mask, use the RectMask2D which will not result in an instance material.

    Change the _FaceColor of the fontSharedMaterial will result in the face color being changed on all text objects that share this material. Ie. will not create an instance.
     
    Galse22 likes this.
  3. BeginnerPolymath

    BeginnerPolymath

    Joined:
    Jul 31, 2015
    Posts:
    38
    Oh, I didn't know about the Color Gradient Preset, I thought it was just gradients, I think the problem is solved.
     
  4. Stephan_B

    Stephan_B

    Unity Technologies

    Joined:
    Feb 26, 2017
    Posts:
    6,588
    Those are an asset that you can assign to text objects. These can be single color, 2 or 4 colors.

    There are examples of those included in the TMP Examples & Extras in the "TextMesh Pro/Examples & Extras/Resources/Color Gradient Presets" folder.
     
  5. BeginnerPolymath

    BeginnerPolymath

    Joined:
    Jul 31, 2015
    Posts:
    38
    Thanks problem is solved.
     
  6. BeginnerPolymath

    BeginnerPolymath

    Joined:
    Jul 31, 2015
    Posts:
    38
    Okay, I was in a hurry. When I change Color Gradient Presets from code, I don't update the color of the text. That is, the color changes, but not applied, you need somehow update the colors of all TextMeshPro components on the scene
     
  7. Stephan_B

    Stephan_B

    Unity Technologies

    Joined:
    Feb 26, 2017
    Posts:
    6,588
    You just need to create a Color Gradient Preset. Enable Gradient Preset and assign it to your text objects. Then all text objects to which it is assigned will have the same Color Gradient Preset.
     
  8. Stephan_B

    Stephan_B

    Unity Technologies

    Joined:
    Feb 26, 2017
    Posts:
    6,588
    BTW: Any reason why you are not changing the vertex color on the text objects or using a material preset?
     
  9. BeginnerPolymath

    BeginnerPolymath

    Joined:
    Jul 31, 2015
    Posts:
    38
    I'm sorry it's so confusing. The video shows that I assign color to the gradient, it does not change, but from the engine all applies.
     
  10. Stephan_B

    Stephan_B

    Unity Technologies

    Joined:
    Feb 26, 2017
    Posts:
    6,588
    I cannot see in the video if you have assigned a Color Gradient Preset to your text objects or if you are creating some instance.

    Are these text objects all using the same (shared) Color Gradient Preset asset?
     
  11. BeginnerPolymath

    BeginnerPolymath

    Joined:
    Jul 31, 2015
    Posts:
    38
    Yeah, they all use the same set of color gradients. I kind of solved the problem by adding the main parent off and on.

    Code (CSharp):
    1. public TMP_ColorGradient TextMaterial;
    2.  
    3. TextMaterial.bottomLeft = TextColors[ID];
    4. TextMaterial.bottomRight = TextColors[ID];
    5.  
    6. TextMaterial.topLeft = TextColors[ID];
    7. TextMaterial.topRight = TextColors[ID];
    8.  
    9. BookViwer.gameObject.SetActive(false);
    10. BookViwer.gameObject.SetActive(true);
     
  12. BeginnerPolymath

    BeginnerPolymath

    Joined:
    Jul 31, 2015
    Posts:
    38
    Color just wasn't used for drawing, although it was assigned. At restart or from the editor everything works.
     
  13. Stephan_B

    Stephan_B

    Unity Technologies

    Joined:
    Feb 26, 2017
    Posts:
    6,588
    You should not have to do anything special.

    An easy way to test this is as follows:
    (1) Create a new scene.
    (2) Add two UI - Text - TextMeshPro object into that scene.
    (3) Assign the same Color Gradient Preset to them.
    (4) Modify the Color Gradient Preset in the Inspector and the color changes will happen on both text objects.
     
  14. Stephan_B

    Stephan_B

    Unity Technologies

    Joined:
    Feb 26, 2017
    Posts:
    6,588
    Make sure the same Color Gradient Preset (persistent asset) is assigned to these text objects. In code, you can check if they all have the same instance ID to verify it is in fact the same and not an instance of a Color Gradient Preset.
     
  15. BeginnerPolymath

    BeginnerPolymath

    Joined:
    Jul 31, 2015
    Posts:
    38
    You're right, from the inspector, it works, but not from the code. Maybe it's influenced by the fact that I use 13 previews 2.1.0.
     
  16. Stephan_B

    Stephan_B

    Unity Technologies

    Joined:
    Feb 26, 2017
    Posts:
    6,588
    I am using the same version 2.1.0-preview.13 for testing and it works as expected on my end.

    Like I said, double check that when you are changing the color of this preset in code that it is changing it on the same instance ID / color gradient preset.
     
  17. Stephan_B

    Stephan_B

    Unity Technologies

    Joined:
    Feb 26, 2017
    Posts:
    6,588
    Since it is getting late for me (1:30 AM) I will follow up tomorrow and if you still have issues, I'll provide some code example / script that changes the color gradient preset assigned to multiple objects.
     
  18. BeginnerPolymath

    BeginnerPolymath

    Joined:
    Jul 31, 2015
    Posts:
    38
    Maybe this video is clearer.
     
  19. BeginnerPolymath

    BeginnerPolymath

    Joined:
    Jul 31, 2015
    Posts:
    38
    Anyway, I've got it working, it just looks like a crutch
     
  20. Galse22

    Galse22

    Joined:
    Jun 25, 2020
    Posts:
    2
    Yo thanks a bunch.
     
  21. MTCpersian

    MTCpersian

    Joined:
    Jun 18, 2020
    Posts:
    3
    I have the same issue, I also tried color gradient instead of face color but still text mesh pro's wont change unless I change color gradient in inspector or restart my app or restart the scene!
     
  22. Stephan_B

    Stephan_B

    Unity Technologies

    Joined:
    Feb 26, 2017
    Posts:
    6,588
    Any reason you are using the TMP_Text.faceColor instead of the TMP_Text.color which changes the vertex color?

    Changing the TMP_Text.faceColor will result in the creation of a material instance since the face color is a material property where it would otherwise change the face color of all text objects that share the same material.

    By contrast changing the TMP_Text.color changes the vertex color which is unique per text object.

    It is ok to change the "_FaceColor" but you have to be aware that you are creating an instance material behind the scene.

    If you want to change any material property of those materials, you can access the TMP_Text.sharedFontMaterial which will affect all objects using this material or access the TMP_Text.fontMaterial which will create an instance.