baobao
12/7/2018 - 3:30 AM

ランダムテキストアニメーション

using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using UnityEngine.UI;
using System.Linq;
using DG.Tweening;

public class RandomTextAnimation : MonoBehaviour
{
    [SerializeField]
    private Text _text;

    private void Awake()
    {
        _text.enabled = false;
    }

    public void WaveCount(int cnt, int total)
    {
        string result = string.Format("WAVE   {0} / {1}", cnt, total);
        _text.enabled = true;
        _text.DOKill();
        _text.color = Color.white;
        StartCoroutine(RandomTextAnimation(result, 1, ()=>{
            _text.DOFade(0, 0.6f).SetEase(Ease.OutSine).OnComplete(() => {
                _text.enabled = false;
            }).SetDelay(1f);
        }));
    }

    private IEnumerator RandomTextAnimation (string result, int speed = 1, System.Action onComplete = null)
    {
        int resultCharCnt = result.Length;
        int cnt = 0;
        for(int i = 0; i < resultCharCnt; i++)
        {
            _text.text = result.Substring(0, cnt) + string.Join(",", GetRandomTexts(resultCharCnt - cnt).ToArray());
            cnt += speed;
            cnt = Mathf.Clamp(cnt, 0, resultCharCnt);
            yield return null;
        }

        _text.text = result;
        onComplete.SafeInvoke();
    }

    private static string[] RAND_TEXTS = new string[]{
        "A", "B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z","1","2","3","4","5","6","7","8","9","0","#","$"
        ,"!","%","&","(",")","¥"
    };

    private IEnumerable<string> GetRandomTexts (int cnt)
    {
        return RAND_TEXTS.Shuffle().Take(cnt);
    }
}