「Unity/Csharp/Coroutine」の版間の差分
提供: 初心者エンジニアの簡易メモ
(ページの作成:「==コールチンとは== 条件になるまで待機するもの ==数秒後まで待機== <pre> using System.Collections; private void Start() { StartCoroutine(Delay...」) |
(→入力待機) |
||
(同じ利用者による、間の8版が非表示) | |||
行1: | 行1: | ||
==コールチンとは== | ==コールチンとは== | ||
− | 条件になるまで待機するもの | + | *条件になるまで待機するもの |
+ | *類似なものとして、UniTaskがある | ||
+ | |||
+ | [[unity/UniTask]] [ショートカット] | ||
==数秒後まで待機== | ==数秒後まで待機== | ||
行16: | 行19: | ||
==入力待機== | ==入力待機== | ||
+ | エンターキーを入力するまで待つ | ||
<pre> | <pre> | ||
using System.Collections; | using System.Collections; | ||
行22: | 行26: | ||
} | } | ||
IEnumerator WaitInput () { | IEnumerator WaitInput () { | ||
− | yield return new WaitUntil(() => Input.GetKeyDown(KeyCode. | + | yield return new WaitUntil(() => Input.GetKeyDown(KeyCode. Return)); |
+ | // yield return new WaitWhile(() => !Input.GetKeyDown(KeyCode.Return)); // WaitWhileの場合は、条件に当てはまらないときに、先にすすめる | ||
+ | // ここに処理を追加 | ||
} | } | ||
</pre> | </pre> | ||
参考:https://tofgame.hatenablog.com/entry/2019/04/10/141614 | 参考:https://tofgame.hatenablog.com/entry/2019/04/10/141614 | ||
+ | ==コールチンのキャンセル== | ||
+ | <pre> | ||
+ | using System.Collections; | ||
+ | Coroutine coroutine; | ||
+ | private void Start() | ||
+ | { | ||
+ | coroutine = StartCoroutine(WaitInput()); | ||
+ | } | ||
+ | private void Update() | ||
+ | { | ||
+ | if (Input.GetKeyDown(KeyCode.Space)) | ||
+ | { | ||
+ | StopCoroutine(coroutine); | ||
+ | } | ||
+ | } | ||
+ | </pre> | ||
+ | |||
+ | ==WaitUntilは1フレーム消費する== | ||
+ | 参考:https://qiita.com/kema/items/0c80483d828d101b0693 |
2022年12月5日 (月) 04:25時点における最新版
コールチンとは
- 条件になるまで待機するもの
- 類似なものとして、UniTaskがある
unity/UniTask [ショートカット]
数秒後まで待機
using System.Collections; private void Start() { StartCoroutine(DelayMethod1(5.0f, 123)); } IEnumerator DelayMethod1(float delay, int hoge) { yield return new WaitForSeconds(delay); // ここに処理を追加 }
unity/Csharp/Invoke [ショートカット]
入力待機
エンターキーを入力するまで待つ
using System.Collections; private void Start() { StartCoroutine(WaitInput()); } IEnumerator WaitInput () { yield return new WaitUntil(() => Input.GetKeyDown(KeyCode. Return)); // yield return new WaitWhile(() => !Input.GetKeyDown(KeyCode.Return)); // WaitWhileの場合は、条件に当てはまらないときに、先にすすめる // ここに処理を追加 }
参考:https://tofgame.hatenablog.com/entry/2019/04/10/141614
コールチンのキャンセル
using System.Collections; Coroutine coroutine; private void Start() { coroutine = StartCoroutine(WaitInput()); } private void Update() { if (Input.GetKeyDown(KeyCode.Space)) { StopCoroutine(coroutine); } }