We may decide to execute a function not right now, but at a certain time later. That’s called “scheduling a call”. The Timers class helps you to do so with a clean and short syntax without having to worry about enumerators.
Install the latest stable release using the Unity Package Manager by adding the following line to yourmanifest.jsonfile located within your project's Packages directory, or by adding the Git URL to the Package Manager Window inside of Unity.
The module is availble on the OpenUPM package registry, you can install the latest stable release using the OpenUPM Package manager's Command Line Tool using the following command.
We may decide to execute a function not right now, but at a certain time later. That’s called “scheduling a call”. Note that the timers are using unscaled time.
There are two methods for it:
setTimeout allows to run a function once after the interval of time.
setInterval allows to run a function regularly with the interval between the runs.
Examples
public class MyClass {
private void Awake () {
Timers.SetTimeout(1000, () => {
Debug.Log("A second has passed!");
});
Timers.SetTimeout(1000, Notifiy);
Timers.SetInterval(2500, Notifiy);
}
private void Notify () {
Debug.Log("Notify!!");
}
}