前提・実現したいこと
Unity初心者です。Unityで会話シーンを作ろうとしています。
1文字ずつ表示して、表示途中にクリックしたら最後まで一気に表示するというものを作るにはどのようなものが必要になり、どのようなスクリプトを書けば出来ますか?
会話シーン用のスクリプト(1文字ずつ表示することは出来た)
C#
1using UnityEngine; 2using System.Collections; 3using UnityEngine.UI; 4 5public class Dialogue : MonoBehaviour { 6 7 //読み込んだテキストを出力するUIテキスト 8 [SerializeField] 9 private Text dataText; 10 //読む込むテキストが書き込まれている.txtファイル 11 private string loadText; 12 //改行で分割して配列に入れる 13 private string[] splitText; 14 //表示中のテキスト番号 15 private int textNum; 16 int TextLEN; 17 int textlength; 18 [SerializeField] float novelSpeed; 19 20 void Start () { 21 loadText = (Resources.Load("Dialogue", typeof(TextAsset)) as TextAsset).text; 22 splitText = loadText.Split(char.Parse("\n")); 23 textNum = 1; 24 } 25 26 void Update () { 27 if(Input.GetButtonDown("Fire1")) { 28 StartCoroutine("Novel"); 29 } 30 } 31 IEnumerator Novel(){ 32 //Resourcesフォルダに配置したテキストファイルの内容を表示 33 int TextLEN = splitText[textNum].Length; 34 Debug.Log("今の文字列"+splitText[textNum]+"は"+TextLEN+"文字です"); 35 for (textlength = 0; textlength<TextLEN; ++textlength){ 36 dataText.text = splitText[textNum].Substring(0, textlength); 37 yield return new WaitForSeconds(novelSpeed); 38 } 39 //ここから最後まで表示終わった処理 40 textNum+=2; 41 StopCoroutine("Novel"); 42 } 43 } 44
よろしくお願いします。
質問頂きありがとうございます。
Novel()のfor内に
if(Input.GetMouseButtonDown(0)){
textlength=textLEN;
}
を挿入してみてはどうでしょうか?
> bochan2さん
「yield return new WaitForSeconds(novelSpeed);」で待機しているので、例えば「1秒に1文字」表示させているのであれば、その1秒分反応が遅れるので、あまり良くないかなと思います。
yield return new WaitForSeconds(novelSpeed);より前のfor内にif以下を入れると、1文字ずつ文字が表示される前に全て表示されてしまいます。逆に yield return new WaitForSeconds(novelSpeed);より後にif以下を入れると1文字ずつ表示はされるのですが、クリックしてもforで回してるので、すべて表示されませんでした。
回答1件
あなたの回答
tips
プレビュー