Unity2021から標準パッケージとなった”Visual Scripting(ビジュアルスクリプティング)”(旧Bolt)を使用して、コルーチン(coroutine)を使う方法を紹介します。
とりあえず
何も考えずに、コルーチンで使用する、以下のように”On Start”イベントで”Wait For Seconds”ユニットを使ってみると、

以下のようなエラーが出る。
InvalidOperationException: Port 'enter' on 'WaitForSecondsUnit#d6def...' can only be triggered in a coroutine.
“Wait For Seconds”ユニットはコルーチンの中でしか使えないよという感じのエラーメッセージ
では、どのようにやるかというと。
その1 StartイベントやUpdateイベントで使用する方法
1-1.”On Start”ユニットを選択して、”Graph Inspector”の”Coroutine”のチェックを付ける


すると、”On Start”ユニットの右上に”⇉”マークが付き、これがコルーチンの印となる。

これだけで、実行すると、”Wait For Seconds”ユニットなどのコルーチン用のユニットが動作するようになる。
1-2.完成図の例としてはこんな感じ。

その2 カスタムイベントで使用する方法
1-1.”Custom Event Arguments”ユニットを選択して、”Graph Inspector”の”Coroutine”のチェックを付ける


すると、”Custom Event Arguments”ユニットの右上に”⇉”マークが付き、これがコルーチンの印となる。

1-2.完成図の例としてはこんな感じ。

コメント