私もUniRxについて経験が浅いので自信がないのですが、EnemyManager
中のFromCoroutine
の第2引数publishEveryYield
がfalseなら、コルーチンが完了してからSubscribe
に記述された敵行動終了時の処理が実行されるのではないかと思います。
publishEveryYield
はデフォルトでfalseなので、敵行動終了時の処理はご質問者さんの現状のコードで正しく実行されるような気がするのですが、それなのに次の敵が変なタイミングで行動を開始してしまうとなると、ご提示のコードでは省略されている、各敵の全行動完了を待機するロジックに何かトラブルがあるのではないでしょうかね...?
コード中の各段階で細かくLogを出力させてみたり、デバッガで動きを追ってみる必要があるかもしれません。
※試しにその全行動完了待機の部分をToYieldInstruction
で作ったYieldInstructionをyield return
で返して待機させるようにして、Subscribe
で記述していた敵行動終了時の処理はDo
でやるようにしたところ、とりあえずちゃんとした順序で動いているように見えました。
Enemy
C#
1using System.Collections;
2using UniRx;
3using UnityEngine;
4
5public class Enemy : MonoBehaviour
6{
7 public IEnumerator ActionAI()
8 {
9 yield return Observable.FromCoroutine(_ => this.ExecuteMoveAI())
10 .Do(_ => Debug.LogFormat("ActionAI : {0} 移動完了", this.name))
11 .ToYieldInstruction();
12
13 yield return Observable.FromCoroutine(_ => this.ExecuteAttackAI())
14 .Do(_ => Debug.LogFormat("ActionAI : {0} 攻撃完了", this.name))
15 .ToYieldInstruction();
16 }
17
18 private IEnumerator ExecuteMoveAI()
19 {
20 Debug.LogFormat("ExecuteMoveAI : {0} 移動開始", this.name);
21
22 yield return new WaitForSeconds(1.0f);
23
24 Debug.LogFormat("ExecuteMoveAI : {0} 移動終了", this.name);
25 }
26
27 private IEnumerator ExecuteAttackAI()
28 {
29 Debug.LogFormat("ExecuteAttackAI : {0} 攻撃開始", this.name);
30
31 yield return new WaitForSeconds(1.0f);
32
33 Debug.LogFormat("ExecuteAttackAI : {0} 攻撃終了", this.name);
34 }
35}
EnemyManager
C#
1using System.Collections;
2using UniRx;
3using UnityEngine;
4
5public class EnemyManager : MonoBehaviour
6{
7 public Enemy[] enemies;
8
9 private IEnumerator Start()
10 {
11 Debug.Log("Start : 敵集団行動開始");
12
13 yield return new WaitForSeconds(1.0f);
14
15 var enemyCount = this.enemies.Length;
16
17 for (var i = 0; i < enemyCount; i++)
18 {
19 var enemy = this.enemies[i];
20
21 yield return Observable.FromCoroutine(_ => enemy.ActionAI())
22 .Do(_ => Debug.LogFormat("Do : {0} 全行動終了、次の敵の行動に移る", enemy.name))
23 .ToYieldInstruction();
24
25 Debug.Log("Start : 次の敵の行動に移る");
26
27 yield return new WaitForSeconds(1.0f);
28 }
29
30 Debug.Log("Start : 敵集団行動終了");
31 }
32}
コンソールに出力された結果
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2018/06/28 01:41