using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using UnityEngine;
public static class TextScrambler {
///
/// Transition from one text to another over time
///
///
/// text to transition from
/// text to transition to
/// time in seconds to transition
/// assign the output to your text. example: (result)=>{myText.text = result;}
public static void Scramble(this MonoBehaviour monoBehaviour, string initial, string final, float time, Action assignFunction)
{
monoBehaviour.StartCoroutine(ScrambleCoroutine(initial, final, time, assignFunction));
}
private static IEnumerator ScrambleCoroutine(string initial, string final, float time, Action assignFunction)
{
string bigger = initial.Length > final.Length ? initial : final;
var waitTime = new WaitForSeconds(time / bigger.Length);
List positions = new List(bigger.Length);
for (int i = 0; i < bigger.Length; i++)
{
positions.Add(i);
}
StringBuilder stringBuilderInitial = new StringBuilder(initial);
while(positions.Count > 0)
{
int posIdx = UnityEngine.Random.Range(0, positions.Count);
int pos = positions[posIdx];
if(pos < stringBuilderInitial.Length)
{
if(pos < final.Length)
{
stringBuilderInitial[pos] = final[pos];
}
else
{
stringBuilderInitial.Remove(pos, 1);
if(posIdx < positions.Count - 1)
{
for (int i = posIdx + 1; i < positions.Count; i++)
{
positions[i]--;
}
}
}
}
else
{
stringBuilderInitial.Append(' ', pos + 1 - stringBuilderInitial.Length);
stringBuilderInitial[pos] = final[pos];
}
positions.RemoveAt(posIdx);
assignFunction(stringBuilderInitial.ToString());
yield return waitTime;
}
}
}