コガネブログ

平日更新を目標に Unity や C#、Visual Studio、ReSharper などのゲーム開発アレコレを書いていきます

【Unity】uGUI の Text でリッチテキストを考慮して1文字ずつ表示できる「Unity-TextTyper」紹介

はじめに

「Unity-TextTyper」を Unity プロジェクトに導入することで
uGUI の Text でリッチテキストを考慮して1文字ずつ表示できるようになります

使用例

f:id:baba_s:20171222113317g:plain

使い方

f:id:baba_s:20171222113506p:plain

uGUI の Text に「TextTyper」コンポーネントをアタッチして、
下記のようにスクリプトを記述します

public TextTyper textTyper;

private void Awake()
{
    // 指定したテキストを1文字ずつ表示する
    textTyper.TypeText( "ピカチュウカイリューヤドラン" );
    
    // delay タグを使用すると文字送りの速度を変更できる
    textTyper.TypeText( "ピカチュウ<delay=0.05>カイリュー</delay>ヤドラン" );
    
    // 文字送りをスキップできるかどうか
    var isSkippable = textTyper.IsSkippable();
    
    // 文字送り中かどうか
    var isTyping = textTyper.IsTyping;
    
    // 文字送りをスキップ
    textTyper.Skip();
    
    // 文字送りが完了した時に呼び出されるコールバック
    textTyper.PrintCompleted.AddListener( OnPrintCompleted );
    
    // 1文字表示するたびに呼び出されるコールバック
    textTyper.CharacterPrinted.AddListener( OnCharacterPrinted );
}

private void OnPrintCompleted()
{
}

private void OnCharacterPrinted( string printedCharacter )
{
    // 文字送りのサウンドを鳴らすなど
}

関連記事