関数6
ノベルちゃん風関数
使うとき
void Startや ボタンを押した時のプログラムに書く
StartCoroutine(シナリオ());
IEnumerator シナリオ()
{
yield return 待つ(2f);
yield return 移動する(A, new Vector3(1, 3, 0), 2f);
yield return 待つ(2f);
yield return テキスト表示("あいうえお");
}
関数
using UnityEngine;
using System.Collections;
public static class 関数6
{
public static IEnumerator 待つ(float 秒)
{
yield return new WaitForSeconds(秒);
}
public static IEnumerator 移動する(GameObject obj, Vector3 目的地, float 速度)
{
while (obj.transform.position != 目的地)
{
obj.transform.position = Vector3.MoveTowards(
obj.transform.position,
目的地,
速度 * Time.deltaTime
);
yield return null;
}
}
public static IEnumerator テキスト表示(GameObject obj, string 文)
{
var tmp = obj.GetComponent<TextMeshProUGUI>();
if (tmp != null)
{
tmp.text = 文;
yield return null;
yield break;
}
var text = obj.GetComponent<Text>();
if (text != null)
{
text.text = 文;
yield return null;
yield break;
}
Debug.LogWarning($"{obj.name} にテキストコンポーネントがありません");
yield return null;
}
}
オブジェクトにつけるもの
StartCoroutine を呼べるのは MonoBehaviour だけ。
オブジェクトがいる
using UnityEngine;
using UnityEngine.UI;
public class 関数6実行者 : MonoBehaviour
{
public static 関数6実行者 instance;
void Awake()
{
instance = this;
}
}

