0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

Update内で処理を待つ

Last updated at Posted at 2020-05-18

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());
	}
}

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?