N秒おきにCUBEの表示をONOFFする。
task/async/await
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.Threading.Tasks;
public class Delay2 : MonoBehaviour {
void Start () {
}
async Task Update () {
await Vanish();//1secまって消す
if( gameObject.active == false) {
//1secまってつける
await Task.Delay (1000*1);
gameObject.SetActive (true);
}
}
async Task Vanish () {
await Task.Delay (1000*1);
gameObject.SetActive (false);
}
}
メモ:
久しぶりに使ったらハマったのでメモ
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.Threading.Tasks;
public class Delay : MonoBehaviour {
void Start () {
Func3(); //これは遅延する
}
async Task Update () {
//Func3(); //ここから読んだら遅延しない。
}
async void Func3() {
int cnt = 0;
await Task.Delay(1000);
Debug.Log(cnt.ToString());
cnt++;
await Task.Delay(1000);
Debug.Log(cnt.ToString());
cnt++;
await Task.Delay(1000);
Debug.Log(cnt.ToString());
}
}