tips/자주쓰는 C# 스크립트

유니티, 일정한 시간 딜레이와 반복 Invoke

디지털 수공업자 2020. 12. 18. 13:27
반응형

일정한 시간 후에 함수 호출하거나 일정한 시간 후에 일정한 시간마다 반복 호출.
Coroutine보다 퍼포먼스가 좋고, Update보다 사용하게 편리하다.

using UnityEngine ;
using System.Collections.Generic ;

public class Example : MonoBehavior {

	float delayTime = 1f ;
	float repeatTime = 1f ;


	Invoke("StartSomething", delayTime) ;
	// 1초 후에 StartSomething을 호출

	InvokeRepeating("RepeatSomething", delayTime, repeatTime) ;
	// 1초 후에 RpeatSomething을 처음 호출하고, 이후 1초마다 호출.

}

void StartSomething() {
	
	// 1초 후에 할 일.
	
}

void RepeatSomething() {

	// 1초 후에 매 1초 마다 할 일.

}

void Update() {

	if (Input.GetButton("Fire1")) CancelInvoke() ;
	// 모든 Invoke 취소.

	if (Input.GetButton("Fire2")) CancelInvoke("StartSomething") ;
	// StartSomething을 호출하는 Invoke 취소.

}


Dictionary<T> : https://boxwitch.tistory.com/157
List<T> : https://boxwitch.tistory.com/160

반응형