週刊SleepNel新聞

SleepNel所属のぽうひろが日々の個人開発で気になったことを綴ります。

1文字ずつ進むセリフテキストの実装例

みなさん、こんにちは。ぽうひろです。

ストーリーを入れなきゃなということで、よくあるセリフが一文字ずつでてくるセリフウィンドウを実装してみました。
こんな感じで、uGUIのTextを含んだパネルを用意します。
f:id:pouhiroshi:20160705081916p:plain

f:id:pouhiroshi:20160705082051p:plain

例によって時間制御に便利なUnityTimersを利用しています。
sleepnel.hatenablog.com

using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using EJaw.UnityTimers;
using UnityEngine.UI;

public class SerifControl : MonoBehaviour {

	public GameObject serif1;
	public GameObject serif2;

	private int currentSerifSeq;

	// Use this for initialization
	void Start () {
		InitSerif ();

	}
	
	// Update is called once per frame
	void Update () {
	
	}

	public void InitSerif(){
		serif1.GetComponentInChildren<Text> ().text = "";
		serif2.GetComponentInChildren<Text> ().text = "";
		PlaySeq ();
	}

	public void PlaySeq(){
		Timer timer = new Timer ();
		string serif = "フッフッフッ\nわはははははは!";
		int length = serif.Length;
		timer.Start (0.3f * length, 0.3f);
		int nowPos = 0;
		timer.Elapsed += delegate(float obj) {
			if(serif.isSelf){
				serif1.GetComponentInChildren<Text>().text = serif.Substring(0,nowPos);
			}else{
				serif2.GetComponentInChildren<Text>().text = serif.Substring(0,nowPos);
			}
			nowPos++;
		};

	}
}

何をやっているかと申しますと、UnityTimersのElapsed(単位時間あたりに処理を行う)を利用して、
セリフテキストを1文字ずつSubstringを使って増やしています。
UnityTimers便利だわあ〜。。。(結局それ)

結果はこちら

Unityでセリフ1文字ずつ表示させるサンプル

これから、セリフを考えねばですね。。。(そこ

ではでは!