Unityを使ったゲーム開発において、テキスト表示は重要な要素の一つです。
しかし、一度に全てのテキストを表示すると、プレイヤーにとって読みづらいので
1文字ずつ表示するコードのメモです。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class TextDisplay : MonoBehaviour
{
public float displaySpeed = 0.1f; // 1文字の表示速度
private Text textComponent;
private string displayText;
void Start()
{
textComponent = GetComponent<Text>();
displayText = textComponent.text;
textComponent.text = ""; // テキストを初期化
StartCoroutine(DisplayText());
}
IEnumerator DisplayText()
{
foreach (char c in displayText)
{
textComponent.text += c;
yield return new WaitForSeconds(displaySpeed);
}
}
}