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 );
}
}
}
}