Need help modifying existing fading test script...

These scripts fade text in letter by letter.
Can someone modify them to also fade out letter by letter with a public float (after X amount letters begin fading out)?

using UnityEngine;
using System.Collections;
using UnityEngine.UI;

[RequireComponent(typeof(Text))]
public class TextFader : MonoBehaviour {

    public bool IgnoreWhiteSpaces = true;
    public bool StartOnEnable = true;
    public float CharFadeDuration = 0.1f;

    CharLimiter CharLimiter;
    CharFader CharFader;
    Text text;

    int currentLetterIndex;
    float currentCharFadeTime;

    void OnEnable() {

        if( CharLimiter == null )
            CharLimiter = gameObject.AddComponent<CharLimiter>();

        if( CharFader == null )
            CharFader = gameObject.AddComponent<CharFader>();

        CharLimiter.enabled = true;
        CharFader.enabled = true;

        text = GetComponent<Text>();

        if( StartOnEnable )
        {
            PerformAnimation();
        }
    }

    void OnDisable() {
        CharLimiter.enabled = false;
        CharFader.enabled = false;
    }

    public void PerformAnimation() {
        currentLetterIndex = 0;
        currentCharFadeTime = 0.0f;
    }

    void Update() {

        if( IgnoreWhiteSpaces )
        {
            var str = text.text;

            if( currentLetterIndex >= str.Length )
                return;

            var currentChar = str[currentLetterIndex];
            if( currentChar == ' ' )
            {
                currentLetterIndex++;
                Update();
                return;
            }
        }

        CharLimiter.NumberOfLetters = currentLetterIndex + 1;

        currentCharFadeTime += Time.deltaTime;
        float progress = currentCharFadeTime / CharFadeDuration;
   
        if( progress >= 1.0f )
        {
            CharFader.SetCharAlpha( currentLetterIndex, 255 );

            currentLetterIndex++;
            currentCharFadeTime = 0.0f;

            if( currentLetterIndex >= text.text.Length )
            {
                enabled = false;
            }
        }
        else
        {
            byte alpha = (byte)(progress * 255);
            CharFader.SetCharAlpha( currentLetterIndex, alpha );

        }
    }
}
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
using System.Collections.Generic;

public class CharLimiter : BaseMeshEffect, IMeshModifier {

    [SerializeField]
    int m_NumberOfLetters = -1;

    public int NumberOfLetters {
        get {
            return m_NumberOfLetters;
        }
        set {
           
            if( value == m_NumberOfLetters )
                return;
           
            m_NumberOfLetters = value;
            graphic.SetVerticesDirty();
        }
    }

    static List<UIVertex> tmpVertices = new List<UIVertex>();
    static UIVertex[] tmpVerticesQuad = new UIVertex[4];

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

        if( m_NumberOfLetters == -1 )
            return;

        int vertCount = toFill.currentVertCount;

        for( int i = 0; i < vertCount; i++ )
        {
            if( tmpVertices.Count < (i+1) )
            {
                tmpVertices.Add( new UIVertex() );
            }
            UIVertex vert = tmpVertices[i];
            toFill.PopulateUIVertex( ref vert, i );
            tmpVertices[i] = vert;
        }

        int numberOfVertices = m_NumberOfLetters * 4;
        toFill.Clear();

        for( int i = 0; i < numberOfVertices && i < tmpVertices.Count; i++ )
        {
            int tempVertsIndex = i & 3;
            tmpVerticesQuad[tempVertsIndex] = tmpVertices[i];
            if( tempVertsIndex == 3 )
            {
                toFill.AddUIVertexQuad( tmpVerticesQuad );
            }
        }
    }
}
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
using System.Collections.Generic;

public class CharFader : BaseMeshEffect, IMeshModifier {

    static List<UIVertex> tmpVertices = new List<UIVertex>();
    static UIVertex[] tmpVerticesQuad = new UIVertex[4];

    int charIndex = -1;
    byte alpha;

    public void SetCharAlpha(int charIndex, byte alpha) {
        this.charIndex = charIndex;
        this.alpha = alpha;
        graphic.SetVerticesDirty();
    }

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

        if( charIndex == -1 )
            return;

        int vertCount = toFill.currentVertCount;

        for( int i = 0; i < vertCount; i++ )
        {
            if( tmpVertices.Count < (i+1) )
            {
                tmpVertices.Add( new UIVertex() );
            }
            UIVertex vert = tmpVertices[i];
            toFill.PopulateUIVertex( ref vert, i );
            tmpVertices[i] = vert;
        }

        toFill.Clear();

        for( int i = 0; i < vertCount; i++ )
        {
            int tempVertsIndex = i & 3;
            tmpVerticesQuad[tempVertsIndex] = tmpVertices[i];


            int letterIndex = i / 4;

            if( charIndex == letterIndex )
            {
                var color = tmpVerticesQuad[tempVertsIndex].color;
                color.a = alpha;
                tmpVerticesQuad[tempVertsIndex].color = color;
            }


            if( tempVertsIndex == 3 )
            {
                toFill.AddUIVertexQuad( tmpVerticesQuad );
            }
        }
    }
}

No, but you certainly can! Try step by step doing what you want, and if something is not working the way you want, use lots of Debug.Log() statements to track it down.

In fact, to understand how the above code even behave right now, I recommend liberally sprinkling Debug.Log() statements through your code to display information in realtime.

Doing this should help you answer these types of questions:

  • is this code even running? which parts are running? how often does it run?
  • what are the values of the variables involved? Are they initialized?

Knowing this information will help you reason about the behavior you are seeing so you can reason about how to change it to suit your needs.

1 Like