LoginSignup
24
23

More than 5 years have passed since last update.

NGUIのUILabelを一文字ずつ書き出す

Posted at

初投稿。気が向いた時にネタあげていきます。
主にコピペ用の簡単ハックを書く予定です。

NGUIのUILabelを一文字ずつ出力するサンプル

TextTest.cs
using UnityEngine;
using System;
using System.Collections;
using System.Text;
using System.Text.RegularExpressions;

public class TextTest : MonoBehaviour {

    void Start () {
        string text = "ほげ[FF0000]ほげ[-]ほげ";
        //string text = GetComponent<UILabel>().text;
        StartCoroutine(textWriter(text));
    }

    IEnumerator textWriter(string text){

        int len = 0;
        StringBuilder sb = new StringBuilder();
        UILabel label = GetComponent<UILabel>();
        label.text = sb.ToString();

        Regex r = new Regex(@"^\[[0-9A-F]{6}\]$");

        yield return null;

        while(len < text.Length){
            // Font Colorの評価
            string txt = text.Substring( len , 1 );
            if (txt == "["){
                if (len+3 < text.Length){
                    txt = text.Substring( len , 3 );
                    if (txt == "[-]"){
                        sb.Append( txt );
                        len += 3;
                    } else if (len+8 < text.Length){
                        txt = text.Substring( len , 8 );
                        if (r.IsMatch(txt)){
                            sb.Append( txt );
                            len += 8;
                        }   
                    }
                }
                if (len >= text.Length) break;
            }
            sb.Append( text.Substring( len , 1 ) );
            label.text = sb.ToString();
            len++;
            //Debug.Log(len);
            yield return null;
        }
        yield return null;
    }
}
24
23
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
24
23