Help us understand the problem. What is going on with this article?

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

More than 5 years have passed since last update.

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

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;
    }
}
mbotsu
転職歴: 調理師 -> WEBエンジニア -> ゲームエンジニア -> R&D 現在はAI/IoT/xRと浅く広く活動
https://twitter.com/mb_otsu
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
No comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
ユーザーは見つかりませんでした