質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.47%
C#

C#はマルチパラダイムプログラミング言語の1つで、命令形・宣言型・関数型・ジェネリック型・コンポーネント指向・オブジェクティブ指向のプログラミング開発すべてに対応しています。

Unity

Unityは、Unity Technologiesが開発・販売している、IDEを内蔵するゲームエンジンです。主にC#を用いたプログラミングでコンテンツの開発が可能です。

Q&A

解決済

1回答

289閲覧

コルーチンの挙動でわからないことがあります

tsubu

総合スコア15

C#

C#はマルチパラダイムプログラミング言語の1つで、命令形・宣言型・関数型・ジェネリック型・コンポーネント指向・オブジェクティブ指向のプログラミング開発すべてに対応しています。

Unity

Unityは、Unity Technologiesが開発・販売している、IDEを内蔵するゲームエンジンです。主にC#を用いたプログラミングでコンテンツの開発が可能です。

0グッド

1クリップ

投稿2020/05/10 10:15

編集2020/05/10 12:12

閲覧ありがとうございます。

表題の通りなんですが、現在テストでいろいろな関数とかを試していて、
Excelのデータを読み込むことに挑戦しています。

そこで、以下のスクリプトを実行後、適当なInputを行なったときに、
endOfTalkがtrueにならない限り、Story()を先頭からスタートさせたいと思っています。

messageを1回更新したいんですが、いざ入力してみると連続で2回更新されてしまいます。
コルーチンの使い方もあまり詳しくないのですが、このコードだと何がまずいのでしょうか。

ご教授の程、よろしくお願いいたします。

C#

1using System.Collections; 2using System.Collections.Generic; 3using System.Linq; 4using UnityEngine; 5using UnityEngine.UI; 6 7public class TestStory : MonoBehaviour 8{ 9 [SerializeField] TestExcel testExcel; 10 [SerializeField] Text massage;//本文 11 [SerializeField] Text yBranchMassage;//肯定 12 [SerializeField] Text nBranchMassage;//否定 13 [SerializeField] int iD = 0;//文章を検索するときに使うID 14 public List<Sentence> sentences;//=new List<Sentence>(); 15 16 void Start() 17 { 18 sentences.AddRange(testExcel.sentence); 19 StartCoroutine("Story"); 20 } 21 22 IEnumerator Story() 23 { 24 print(iD); 25 massage.text = sentences[iD].message + ":" + sentences[iD].endOfTalk; 26 27 yield return new WaitUntil(() => Input.anyKeyDown); 28 29 iD++; 30 31 if (sentences[iD].endOfTalk) 32 { 33 print("止まった"); 34 yield break; 35 } 36 else 37 { 38 StartCoroutine("Story"); 39 } 40 } 41}

追記
以下のコードだと1回ずつ更新されましたが、なぜ上記だとそうなるのかわかりません。
時間で更新するのではなく、何らかの入力後に更新したいです。
よろしくおねがいします。

c#

1 2 IEnumerator Story() 3 { 4 /*/while文を試しました 5 while (!sentences[iD].endOfTalk) 6 { 7 print(iD); 8 massage.text = sentences[iD].message + ":" + sentences[iD].endOfTalk; 9 10 //yield return new WaitUntil(() => Input.anyKeyDown);//うまくいかない 11 yield return new WaitForSeconds(3);//うまくいった 12 13 iD++; 14 } 15 //*/ 16 17 //* 18 print(iD); 19 massage.text = sentences[iD].message + ":" + sentences[iD].endOfTalk; 20 21 //yield return new WaitUntil(() => Input.anyKeyDown); 22 yield return new WaitForSeconds(3);//ここを変更。うまくいった。 23 24 iD++; 25 26 if (sentences[iD].endOfTalk) 27 { 28 print("止まった"); 29 yield break; 30 } 31 else 32 { 33 StartCoroutine("Story"); 34 } 35 //*/ 36 }

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

自己解決

追記の一定時間待つ処理で正常に動いたので、Input.anyKeyDownが悪いと思い、
Input.マウスDown(0)でやってみたが変わらず。
boolを追加し、yieldの直前でfalseにするように変更したところ、
希望した動きになった。

C#

1using System.Collections; 2using System.Collections.Generic; 3using System.Linq; 4using UnityEngine; 5using UnityEngine.UI; 6 7public class TestStory : MonoBehaviour 8{ 9 [SerializeField] TestExcel testExcel; 10 [SerializeField] Text massage;//本文 11 [SerializeField] Text yBranchMassage;//肯定 12 [SerializeField] Text nBranchMassage;//否定 13 [SerializeField] int iD = 0;//文章を検索するときに使うID 14 public List<Sentence> sentences;//=new List<Sentence>(); 15 16 void Start() 17 { 18 sentences.AddRange(testExcel.sentence); 19 StartCoroutine("Story"); 20 } 21 22 bool touch;//boolを追加しました。 23 public void Touch() 24 { 25 touch = true; 26 } 27 28 IEnumerator Story() 29 { 30 //*/ 31 while (!sentences[iD].endOfTalk) 32 { 33 print(iD); 34 massage.text = sentences[iD].message + ":" + sentences[iD].endOfTalk; 35 touch = false; 36 yield return new WaitUntil(() => touch); 37 38 iD++; 39 } 40 //*/ 41 42 /* 43 print(iD); 44 massage.text = sentences[iD].message + ":" + sentences[iD].endOfTalk; 45 touch = false; 46 yield return new WaitUntil(() =>touch); 47 iD++; 48 49 if (sentences[iD].endOfTalk) 50 { 51 print("止まった"); 52 yield break; 53 } 54 else 55 { 56 StartCoroutine("Story"); 57 } 58 //*/ 59 } 60}

Input.anyKeyDownがどのような悪さをしていたのかわかりませんが、とりあえず動いてよかったです。
whileも使ったことないので、この際に試してみようと思います。
ありがとうございました。

投稿2020/05/10 14:55

tsubu

総合スコア15

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.47%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問