Search Unity

TextMesh Pro Changing gradient color through script?

Discussion in 'UGUI & TextMesh Pro' started by AnAngelineer, Mar 1, 2019.

  1. AnAngelineer

    AnAngelineer

    Joined:
    Nov 4, 2015
    Posts:
    34
    I tried to change the gradient color through script :

    Code (CSharp):
    1.         flavorText_A.SetText("defense!");
    2.         flavorText_B.SetText("defense!");
    3.  
    4.         Color colorTop = new Color(194, 232, 212);
    5.         Color colorBottom = new Color(95, 152, 201);
    6.  
    7.         flavorText_B.colorGradient = new VertexGradient(colorTop, colorTop, colorBottom, colorBottom);
    8.  
    But this doesn't work : I always get white text and my newly created gradient is nowhere to be seen. Note that if I put another gradient through the graphic interface beforehand, this code destroys it and replaced it with the "failed white text" (as expected).
    It does appear that a new gradient is created, but somehow the colors always default to white.

    Anyone knows how to solve this? Thanks
     
    Last edited: Mar 1, 2019
    jcameron47 likes this.
  2. Stephan_B

    Stephan_B

    Joined:
    Feb 26, 2017
    Posts:
    6,595
    The issue is simply related to Color using floats where the r, g, b and a values should be from 0.0f - 1.0f.
     
    Nimi142 and AnAngelineer like this.
  3. AnAngelineer

    AnAngelineer

    Joined:
    Nov 4, 2015
    Posts:
    34
    Oh! I realize now that I should have seen it in the documentation to begin with. It sure explains why it ended up changing to white.

    Thanks. It works as intended now.
     
    Stephan_B likes this.
  4. Kheremos

    Kheremos

    Joined:
    Dec 13, 2017
    Posts:
    17
    Apologies for adding to an old post, but I've been attempting methods to change a TMP_Text element's gradient via script, and I'm not succeeding...

    I'll try to be brief.

    Situation One:
    I have two gradient assets of type TPM_Color Gradient.
    I'd like some text to switch between the two when I toggle.
    However, TMP_text only has a reference to type VertexGradient.
    I couldn't figure out how to hot swap the assets, which is what I'd like to do.

    Situation Two:
    The following code doesn't change the TMP_Text element's gradient.
    Code (CSharp):
    1. alternateColorText.enableVertexGradient = true;
    2. Color tempcolor = new Color(1f, 0f, 1f);
    3. alternateColorText.colorGradient = new TMPro.VertexGradient(tempcolor, tempcolor, tempcolor, tempcolor);
    The first line does work, showing me that the reference is working.

    Situation Three:
    I changed the references for the external gradients to VertexGradient.
    This prevents me from dragging the asset, of course.
    When I adjust the colors in the SerializedField VertexGradient and assign to the colorGradient property of the TMP_text, it still doesn't update.

    I have scoured Google searching for an answer, this topic is one of the few that shows up. So again, sorry for necro posting, but it seemed appropriate.
     
    jcameron47 likes this.
  5. MarkHelsinki

    MarkHelsinki

    Joined:
    Jul 14, 2021
    Posts:
    23
    Sorry to necro, but as you were struggling with this and I was looking to achieve something similar, I thought I'd update it with an answer.

    TextMeshPro has a new feature called GradientPresets. They are scriptable objects, so if you want to use them, you don't instantiate Vector4s in code, rather, you create a field to receive the gradient in your script, and then just swap out the presets when you need to change them at runtime:

    Code (CSharp):
    1. [SerializeField] private TMP_ColorGradient redGradient;
    To create your scriptable object, go to a preferred folder in your Project window and click Create--TextMeshPro--Color Gradient

    Then you can drag this SO gradient into your serialized field in the Inspector.

    To assign your preset in code, simply use:

    Code (CSharp):
    1. textExample.colorGradientPreset = redGradient;
    To set up your colors, either select the SO and change in the Inspector or if you prefer to see the result on a TextMesh component, simply assign it in the Inspector to a text field: ColorGradient--ColorPreset. Your preset should be visible when you choose to assign but if it isn't, just drag and drop it into the field.

    Remember, this is not an instance, so any changes you make on an instance with be saved into your preset and will affect all other instances.
     
    Last edited: Nov 3, 2022
    Ideator and Ryzeson like this.