LoginSignup
5
5

More than 5 years have passed since last update.

Unity テキスト送り? 会話送り?(Textを1文字ずつ表示する)

Last updated at Posted at 2018-12-27

以下のようなテキスト送りを実装しました。

ほぼこの方の書き方。
http://pafu-of-duck.hatenablog.com/entry/2017/06/14/101139

ただ、
1.テキストエリアをプッシュして進めたい
2.Update関数はできすだけ無くして、Main関数っぽいもので管理したい(その方が管理が楽だと思っています)

という思いがあるので強引に修正しています(いい書き方あれば教えてください)。

TextController.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class TextController : MonoBehaviour {

    public string[] sentences; // 文章を格納する
    [SerializeField] Text uiText;   // uiTextへの参照

    [SerializeField][Range(0.001f, 0.3f)]
    float intervalForCharDisplay = 0.05f;   // 1文字の表示にかける時間

    private int currentSentenceNum = 0; //現在表示している文章番号
    private string currentSentence = string.Empty;  // 現在の文字列
    private float timeUntilDisplay = 0;     // 表示にかかる時間
    private float timeBeganDisplay = 1;         // 文字列の表示を開始した時間
    private int lastUpdateCharCount = -1;       // 表示中の文字数


    void Start () {
        SetNextSentence ();
    }


    public void TextUpdate(bool _IsPush) {
        // 文章の表示完了 / 未完了
        if (IsDisplayComplete ()) {
            //最後の文章ではない & ボタンが押された
            if (currentSentenceNum < sentences.Length && _IsPush) {
                SetNextSentence ();
            }
            else if(currentSentenceNum >= sentences.Length){
                currentSentenceNum = 0;
            }
        } else {
            if (_IsPush) {
                timeUntilDisplay = 0; //※1
            }
        }

        //表示される文字数を計算
        int displayCharCount = (int)(Mathf.Clamp01((Time.time - timeBeganDisplay) / timeUntilDisplay) * currentSentence.Length);
        //表示される文字数が表示している文字数と違う
        if (displayCharCount != lastUpdateCharCount) {
            uiText.text = currentSentence.Substring (0, displayCharCount);
            //表示している文字数の更新
            lastUpdateCharCount = displayCharCount;
        }
    }

    // 次の文章をセットする
    void SetNextSentence(){
        currentSentence = sentences [currentSentenceNum];
        timeUntilDisplay = currentSentence.Length * intervalForCharDisplay;
        timeBeganDisplay = Time.time;
        currentSentenceNum++;
        lastUpdateCharCount = 0;
    }

    bool IsDisplayComplete(){
        return Time.time > timeBeganDisplay + timeUntilDisplay; //※2
    }
}

こちらであらゆる管理を行う

GameMainSystem.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class GameMainSystem : MonoBehaviour {
    [SerializeField]
    TextController textController;

    bool IsTextPush = false;
    void Update(){
        textController.TextUpdate(IsTextPush);
        IsTextPush = false;
    }
    public void PushText(){
        this.IsTextPush = true;
    }
}
5
5
0

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
5
5