回答編集履歴
2
誤字の修正
test
CHANGED
@@ -2,7 +2,7 @@
|
|
2
2
|
|
3
3
|
コルーチン内部の実行を5秒遅らせている部分ですが、5秒待っている間にもUpdate()メソッドは毎フレーム実行され続けるわけですからStartCoroutine()が100回以上呼ばれることになります。途中で```Debug.Log(textNumber);```を出力すれば分かりますが、5秒後に自分の環境ではtextNumber→113になってました。当然配列インデックスがオーバーします。
|
4
4
|
|
5
|
-
応急処置としては、コルーチンが実行されるのを待っているか判別するブール変数を用意して、待っている間はテキスト表示を行わないという方法があります。
|
5
|
+
応急処置としては、コルーチンが実行されるのを待っているか判別するブール変数を用意して、待っている間はテキスト表示処理を行わないという方法があります。
|
6
6
|
```C#
|
7
7
|
using System;
|
8
8
|
using System.Collections;
|
1
ソースコードを追加
test
CHANGED
@@ -2,4 +2,97 @@
|
|
2
2
|
|
3
3
|
コルーチン内部の実行を5秒遅らせている部分ですが、5秒待っている間にもUpdate()メソッドは毎フレーム実行され続けるわけですからStartCoroutine()が100回以上呼ばれることになります。途中で```Debug.Log(textNumber);```を出力すれば分かりますが、5秒後に自分の環境ではtextNumber→113になってました。当然配列インデックスがオーバーします。
|
4
4
|
|
5
|
+
応急処置としては、コルーチンが実行されるのを待っているか判別するブール変数を用意して、待っている間はテキスト表示を行わないという方法があります。
|
6
|
+
```C#
|
7
|
+
using System;
|
8
|
+
using System.Collections;
|
9
|
+
using System.Collections.Generic;
|
10
|
+
using UnityEngine;
|
11
|
+
using UnityEngine.UI;
|
12
|
+
|
13
|
+
public class セリフ用2 : MonoBehaviour
|
14
|
+
{
|
15
|
+
public string[] texts;//Unity上で入力するstringの配列
|
16
|
+
int textNumber;//何番目のtexts[]を表示させるか
|
5
|
-
|
17
|
+
string displayText;//表示させるstring
|
18
|
+
int textCharNumber;//何文字目をdisplayTextに追加するか
|
19
|
+
int displayTextSpeed; //全体のフレームレートを落とす変数
|
20
|
+
bool click;
|
21
|
+
bool textStop; //テキスト表示を始めるか
|
22
|
+
bool waitCoroutine; //コルーチンを待っているか
|
23
|
+
void Start()
|
24
|
+
{
|
25
|
+
|
26
|
+
}
|
27
|
+
private void Update()
|
28
|
+
{
|
29
|
+
transform.position = Vector3.one;
|
30
|
+
|
31
|
+
// コルーチンの起動
|
32
|
+
|
33
|
+
|
34
|
+
if (textStop == false && waitCoroutine == false) //テキストを表示させるif文
|
35
|
+
{
|
36
|
+
displayTextSpeed++;
|
37
|
+
if (displayTextSpeed % 5 == 0)//5回に一回プログラムを実行するif文
|
38
|
+
{
|
39
|
+
if (textCharNumber != texts[textNumber].Length)//もしtext[textNumber]の文字列の文字が最後の文字じゃなければ
|
40
|
+
{
|
41
|
+
displayText = displayText + texts[textNumber][textCharNumber];//displayTextに文字を追加していく
|
42
|
+
textCharNumber = textCharNumber + 1;//次の文字にする
|
43
|
+
}
|
44
|
+
else//もしtext[textNumber]の文字列の文字が最後の文字だったら
|
45
|
+
{
|
46
|
+
if (textNumber != texts.Length - 1)//もしtexts[]が最後のセリフじゃないときは
|
47
|
+
{
|
48
|
+
waitCoroutine = true;
|
49
|
+
StartCoroutine(DelayCoroutine(5, () =>
|
50
|
+
{
|
51
|
+
// n秒後にここの処理が実行される
|
52
|
+
|
53
|
+
|
54
|
+
displayText = "";//表示させる文字列を消す
|
55
|
+
textCharNumber = 0;//文字の番号を最初にする
|
56
|
+
textNumber = textNumber + 1;//次のセリフにする
|
57
|
+
transform.position = Vector3.zero;
|
58
|
+
|
59
|
+
}));
|
60
|
+
}
|
61
|
+
else //もしtexts[]が最後のセリフになったら
|
62
|
+
{
|
63
|
+
waitCoroutine = true;
|
64
|
+
StartCoroutine(DelayCoroutine(5, () =>
|
65
|
+
{
|
66
|
+
// n秒後にここの処理が実行される
|
67
|
+
|
68
|
+
|
69
|
+
displayText = ""; //表示させる文字列も消す
|
70
|
+
textCharNumber = 0; //文字の番号を最初にする
|
71
|
+
textStop = true; //セリフ表示を止める
|
72
|
+
transform.position = Vector3.zero;
|
73
|
+
}));
|
74
|
+
}
|
75
|
+
}
|
76
|
+
|
77
|
+
this.GetComponent<Text>().text = displayText;//画面上にdisplayTextを表示
|
78
|
+
click = false;//クリックされた判定を解除
|
79
|
+
}
|
80
|
+
if (Input.GetMouseButton(0))//マウスをクリックしたら
|
81
|
+
{
|
82
|
+
click = true; //クリックされた判定にする
|
83
|
+
}
|
84
|
+
}
|
85
|
+
}
|
86
|
+
|
87
|
+
// Update is called once per frame
|
88
|
+
|
89
|
+
// Update is called once per frame
|
90
|
+
|
91
|
+
private IEnumerator DelayCoroutine(float seconds, Action action)
|
92
|
+
{
|
93
|
+
yield return new WaitForSeconds(seconds);
|
94
|
+
action?.Invoke();
|
95
|
+
waitCoroutine = false;
|
96
|
+
}
|
97
|
+
}
|
98
|
+
```
|