How to: text gradient fill?

I want to make similar effect we had in DF GUI, where text color can be gradient. I’m using custom material with build-in UI/Text shader, changing it to something like this:

half4 frag (v2f i) : COLOR
{
half4 col = i.color;
col.a *= tex2D(_MainTex, i.texcoord).a;
col = col * _Color * tex2D(_MainTex, i.texcoord);
clip (col.a - 0.01);
return col;
}

where in “_MainTex” I assign gradient texture.

I expect color multiply by gradient, but all I get is grey text all the time.

1 Like

Solved: There is component UI->Outline, and also community-written UI->Gradient FIll now for the texts;

Where could one find this?

there[ Unity3D gradient effect - Pastebin.com](http:// Unity3D gradient effect - Pastebin.com) right?

3 Likes

That’s beautiful! Thank you.

Very nice. Thanks.

Vertex Color Gradient & Animation
This video provides a short overview of TextMesh Pro as well as showing the latest implementation of support for Vertex Color Gradients in TextMesh Pro in Unity 4.6 & UI.

Note that you will be able to animate the (4) vertex colors independently of each other using Unity’s Animation component.

1 Like

I’ve made a new version: Fixed an error when the text becomes smaller than 0 - Pastebin.com. Fixed an error when the item is shrunk to invisibility.

i created extended gradient. see [Scripts] Useful 4.6 Scripts Collection - Unity Engine - Unity Discussions

1 Like

I’ve just realized that I can’t animate color values on this Gradient. Do you have any suggestions for that? I suppose I could create a script “bridge” to animate them…

I created the script bridge to animate the color, but I had to do something else too. The UI apparently doesn’t update if it doesn’t know it has to, so even if you change the value of the gradient colors by script, you still need to sort of “poke” the UI to make them actually change. I did this by setting the text.color to a random value (since it gets overwritten by the Gradient component anyway).

I thought I should post this for anyone else who may encounter the same issue.

I found nice solution but honestly i will create PRO (paid) pack of my extension for few $ mainly aiming on animation support (because most of my extension doesnt support animation or support very dirty)…

Just to tease a little bit as this is just a first implementation :slight_smile:

In this video you can see the Vertex Color Gradient on the text combined with the bevel, soft shadow and the new Animated Overlay FX.


In this short GIF animation you can see how we are combining vertex color gradient, outline, bevel, soft shadows, overlay FX and bump mapping on the face of the TextMesh Pro text object.

The font is Impact SDF with some bevel, soft shadow, gradient vertex color and some bump mapping and of course an Animated Overlay EnvMap.

I have problem when update Unity 5.2.0f3 “BaseVertexEffect Change to BaseMeshEffect”

I had the same problem. I updated the script from information I found here: http://forum.unity3d.com/threads/basevertexeffect-change-to-basemesheffect.338455/

New script working in 5.2: http://pastebin.com/LR1u9Lz8

I had the same issue, which the new script resolves, however I struggled to get a local vertical gradient (for multi line text elements). To fix I used these lines:

   case GradientDir.Vertical:
                        if(i % 2 == 0)
                            uiVertex.color *= (i % 3 == 0 || (i - 0) % 3 == 0) ? vertex1 : vertex2;
                        else
                            uiVertex.color *= (i % 3 == 0 || (i - 0) % 3 == 0) ? vertex2 : vertex1;
                        break;

“Assets/Script/UI/Effects/Gradient.cs(7,14): error CS0534: Gradient' does not implement inherited abstract member UnityEngine.UI.BaseMeshEffect.ModifyMesh(UnityEngine.UI.VertexHelper)'”

I copied the code from post #15 and get this error. It dosen’t seem to register the VertexHelper inside of the function. Or something. Ideas?

Looking at their source code diff at: https://bitbucket.org/Unity-Technologies/ui/diff/UnityEngine.UI/UI/Core/VertexModifiers/BaseMeshEffect.cs?diff1=c13e8914c7bee39c5348ffce0998b25b751c99bd&diff2=2f094af18832&at=5.2

it seems like they added a new abstract method:

public abstract void ModifyMesh(VertexHelper vh);

However, I’m not sure how to implement it since this bit here:

using (VertexHelpervertexHelper2 = newVertexHelper())
{
vertexHelper2.AddUIVertexTriangleStream(list);
vertexHelper2.FillMesh(mesh);
}

is expecting a mesh. Will spend sometime understanding the systems and trying to fix it!

    public override void ModifyMesh( VertexHelper vh )
    {
        if( !IsActive() )
            return;

        List<UIVertex> list = new List<UIVertex>();
        vh.GetUIVertexStream( list );

        ModifyVertices( list );

        vh.Clear();
        vh.AddUIVertexTriangleStream( list );
    }

Should work, not sure if it’s the best way to implement, but currently works for me in 5.3

Integrated post #19 and added a basic check to account for situations in which the Text’s string is “”.

using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using UnityEngine.UI;
[AddComponentMenu("UI/Effects/Gradient")]
public class Gradient : BaseMeshEffect
{
    public enum Type
    {
        Vertical,
        Horizontal
    }
    [SerializeField]
    public Type GradientType = Type.Vertical;
    [SerializeField]
    [Range(-1.5f, 1.5f)]
    public float Offset = 0f;
    [SerializeField]
    private Color32 StartColor = Color.white;
    [SerializeField]
    private Color32 EndColor = Color.black;
    public override void ModifyMesh( VertexHelper vh )
    {
        if( !IsActive() )
            return;
        List<UIVertex> list = new List<UIVertex>();
        vh.GetUIVertexStream( list );
        ModifyVertices( list );
        vh.Clear();
        vh.AddUIVertexTriangleStream( list );
    }
    public void ModifyVertices(List<UIVertex> _vertexList)
    {
        if (!IsActive())
            return;
        int nCount = _vertexList.Count;
        switch (GradientType)
        {
            case Type.Vertical:
                {
                    if(_vertexList.Count != 0){
                        float fBottomY = _vertexList[0].position.y;
                        float fTopY = _vertexList[0].position.y;
                        float fYPos = 0f;
   
                        for (int i = nCount - 1; i >= 1; --i)
                        {
                            fYPos = _vertexList[i].position.y;
                            if (fYPos > fTopY)
                                fTopY = fYPos;
                            else if (fYPos < fBottomY)
                                fBottomY = fYPos;
                        }
   
                        float fUIElementHeight = 1f / (fTopY - fBottomY);
                        for (int i = nCount - 1; i >= 0; --i)
                        {
                            UIVertex uiVertex = _vertexList[i];
                            uiVertex.color = Color32.Lerp(EndColor, StartColor, (uiVertex.position.y - fBottomY) * fUIElementHeight - Offset);
                            _vertexList[i] = uiVertex;
                        }
                    }
                }
                break;
            case Type.Horizontal:
                {
                    float fLeftX = _vertexList[0].position.x;
                    float fRightX = _vertexList[0].position.x;
                    float fXPos = 0f;
                    for (int i = nCount - 1; i >= 1; --i)
                    {
                        fXPos = _vertexList[i].position.x;
                        if (fXPos > fRightX)
                            fRightX = fXPos;
                        else if (fXPos < fLeftX)
                            fLeftX = fXPos;
                    }
                    float fUIElementWidth = 1f / (fRightX - fLeftX);
                    for (int i = nCount - 1; i >= 0; --i)
                    {
                        UIVertex uiVertex = _vertexList[i];
                        uiVertex.color = Color32.Lerp(StartColor, EndColor, (uiVertex.position.x - fLeftX) * fUIElementWidth - Offset);
                        _vertexList[i] = uiVertex;
                    }
                }
                break;
            default: break;
        }
    }
}
3 Likes