関数4
関数4
コルーチンの進化したもの
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;
public static class 関数4
{
public static IEnumerator 待つ(float 秒数, Action 処理)
{
yield return new WaitForSeconds(秒数);
処理?.Invoke();
}
public static IEnumerator 順番に(params IEnumerator[] 処理一覧)
{
foreach (var 処理 in 処理一覧)
{
yield return 処理;
}
}
}
使い方
void Update()
{
if (Input.GetKeyDown(KeyCode.Space))
{
StartCoroutine(順番に(
待つ(2f, () => Debug.Log("2秒後")),
待つ(3f, () => Debug.Log("さらに3秒後")),
待つ(1f, () => Debug.Log("さらに1秒後"))
));
}
}
説明
yield return 処理; はこの処理が終わるまで次に進まないという意味。
同時ではなく順番に実行する。
yield return new WaitForSeconds(2f);
→ 2秒待つ
yield return StartCoroutine(別のコルーチン);
→ 別のコルーチンが終わるまで待つ

