関数5
関数5
同時に実行するプログラム
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;
public static class 関数5
{
public static IEnumerator 同時に(params IEnumerator[] 処理一覧)
{
List<Coroutine> 実行中 = new List<Coroutine>();
MonoBehaviour 実行者 = 関数5実行者.インスタンス;
foreach (var 処理 in 処理一覧)
{
実行中.Add(実行者.StartCoroutine(処理));
}
foreach (var c in 実行中)
{
yield return c;
}
}
}
関数実行者
public class 関数5実行者 : MonoBehaviour
{
public static 関数5実行者 インスタンス;
void Awake()
{
インスタンス = this;
}
}
Unity のシーンに空オブジェクトを置いて
関数5実行者 をアタッチするだけでOK。
重要:static クラスから StartCoroutine は呼べない
だから コルーチン実行専用の隠しオブジェクトを作る必要がある。
使い方
void Update()
{
if (Input.GetKeyDown(KeyCode.Space))
{
StartCoroutine(同時に(
点滅(敵),
移動(敵, Vector3.forward, 1f)
));
}
}
public static IEnumerator 点滅(GameObject A, float 速度 = 0.2f)
{
var r = A.GetComponent<Renderer>();
while (true)
{
r.enabled = !r.enabled;
yield return new WaitForSeconds(速度);
}
}
public static IEnumerator 移動(GameObject A, Vector3 方向, float 速度 = 1f)
{
while (true)
{
A.transform.position += 方向 * 速度 * Time.deltaTime;
yield return null;
}
}

