Skip to content

Instantly share code, notes, and snippets.

@korbul
Last active March 22, 2025 13:27
Show Gist options
  • Select an option

  • Save korbul/b8fc9dcbc97e7f015955be066cd254b9 to your computer and use it in GitHub Desktop.

Select an option

Save korbul/b8fc9dcbc97e7f015955be066cd254b9 to your computer and use it in GitHub Desktop.

Revisions

  1. korbul revised this gist Oct 30, 2017. 1 changed file with 8 additions and 0 deletions.
    8 changes: 8 additions & 0 deletions TextScrambler.cs
    Original file line number Diff line number Diff line change
    @@ -6,6 +6,14 @@

    public static class TextScrambler {

    /// <summary>
    /// Transition from one text to another over time
    /// </summary>
    /// <param name="monoBehaviour"></param>
    /// <param name="initial">text to transition from</param>
    /// <param name="final">text to transition to</param>
    /// <param name="time">time in seconds to transition</param>
    /// <param name="assignFunction">assign the output to your text. example: (result)=>{myText.text = result;}</param>
    public static void Scramble(this MonoBehaviour monoBehaviour, string initial, string final, float time, Action<string> assignFunction)
    {
    monoBehaviour.StartCoroutine(ScrambleCoroutine(initial, final, time, assignFunction));
  2. korbul revised this gist Oct 30, 2017. No changes.
  3. korbul revised this gist Oct 30, 2017. No changes.
  4. korbul revised this gist Oct 30, 2017. No changes.
  5. korbul revised this gist Oct 30, 2017. No changes.
  6. korbul revised this gist Oct 30, 2017. No changes.
  7. korbul created this gist Oct 30, 2017.
    57 changes: 57 additions & 0 deletions TextScrambler.cs
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,57 @@
    using System;
    using System.Collections;
    using System.Collections.Generic;
    using System.Text;
    using UnityEngine;

    public static class TextScrambler {

    public static void Scramble(this MonoBehaviour monoBehaviour, string initial, string final, float time, Action<string> assignFunction)
    {
    monoBehaviour.StartCoroutine(ScrambleCoroutine(initial, final, time, assignFunction));
    }

    private static IEnumerator ScrambleCoroutine(string initial, string final, float time, Action<string> assignFunction)
    {
    string bigger = initial.Length > final.Length ? initial : final;
    var waitTime = new WaitForSeconds(time / bigger.Length);
    List<int> positions = new List<int>(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;
    }
    }
    }