追記
概要
ざっくり解説
演出を3つの処理に分解する
必要な文字数分'-'を追加していく処理
文字列の左から1文字ずつシャッフルを増やしていく処理
文字列の左から1文字ずつ正しい文字を表示していく処理
これらをコルーチンでタイミングをずらして実行開始する
スクリプト
ShuffleText.csusing System.Collections;
using UnityEngine;
using UnityEngine.UI;
[RequireComponent(typeof(Text))]
public class SuffleText : MonoBehaviour {
public string RandomCharacters = "ABCDEFGHIJKLMNOPQRSTUVWXY!#$%&'()0=~|Z1234567890";
public string EmptyCharacter = "-";
public float delay = 0.2f;
public float timeOut = 0.02f;
private Text textField;
private float timeElapsed;
private string text;
private int length = 0;
private int shuffleLength = 0;
private int displayLength = 0;
void Start () {
textField = gameObject.GetComponent<Text> ();
Shuffle();
}
public void Shuffle () {
text = textField.text;
length = text.Length;
shuffleLength = 0;
displayLength = 0;
textField.text = "";
StartCoroutine ("Starter");
}
void Update() {
if ( displayLength <= length ) {
timeElapsed += Time.deltaTime;
if (timeElapsed >= timeOut) {
if (textField.text.Length < length) {
textField.text += EmptyCharacter;
}
if(shuffleLength > 0){
textField.text = GenerateRandomText (shuffleLength)
+ textField.text.Substring (shuffleLength );
if(shuffleLength < textField.text.Length){
shuffleLength++;
}
}
if(displayLength > 0){
textField.text = text.Substring(0, displayLength)
+ textField.text.Substring (displayLength);
displayLength++;
}
timeElapsed = 0.0f;
}
}
}
private IEnumerator Starter()
{
yield return new WaitForSeconds (delay);
shuffleLength = 1;
yield return new WaitForSeconds (delay);
displayLength = 1;
yield return false;
}
private string GenerateRandomText( int length )
{
var stringBuilder = new System.Text.StringBuilder( length );
var random = new System.Random();
int position = 0;
char c;
for ( int i = 0; i < length; i++ ){
position = random.Next( RandomCharacters.Length );
c = RandomCharacters[position];
stringBuilder.Append( c );
}
return stringBuilder.ToString();
}
}