This is the second part of the multi-part series on Simple timer in Unity. You can find part 1 here.
A timer is an essential game feature. Therefore, game developers use it at some point in their careers. We learnt how to create a timer using Invoke() and Coroutine()in the last part. Continuing the series, in this part, we will create a timer with Update() and AsyncAwait.
Timer with Update()
The Update() method is a predefined method given by MonoBehaviour and runs in each game frame automatically. The Update() method will run until the GameObject attached to the script is active.
For the timer with Update() method, we need a float variable, which will be increased with Time.deltaTime in each frame. Its parsed integer value will be used to indicate the increase in the timer.
In the Update() method, _totalTime will hold the total time to which the timer will run and _timerIsOn will handle the starting and ending of the timer update.
Timer increment in update() method
As we mentioned above, Update() runs in each frame change in the game, so the increment of the timer value should be done in each frame.
Here, we have created a timer increment. Now, we need to set the value of the timer before starting it. Create a method to start the timer and update the required value.
After calling this method, the timer will start and end when it reaches the total time you provided in the StartTimer() method above. In this test case, we have called the timer in Start() method, which will be executed once the GameObject the script is attached to is enabled.
Stopping the timer in update() method
Lastly, we need to create a method to stop the running timer.
To know when the timer is completed, we can trigger an Action event when the timer is completed and set it to null when it stops. If you don't want to use the Action event, you can directly call the next method that will run when the timer is completed.
This is how you can create a timer with the Update() method in Unity.
Full code for TimerWithUpdate()
The full code for the TimerWithUpdate() is below:
Can we use a float variable instead of an integer to represent the timer? Yes, but with float variables, you'll get a timer with decimals which doesn't give a good look on screen. Also, if you need an exact second from the timer, you will need to convert it to an integer. So, using an integer would prevent any hassle beforehand.
Timer with AsyncAwait
Async Await Support is a free plugin in the Unity asset store. This plugin makes your normal method an Async method, so it can run asynchronously in the game. It works similarly to the Coroutine() method but without using the StartCoroutine() to run it. We can apply a timer with this method easily. We can also apply try-catch to the whole code within the method, which we couldn't do in Coroutine().
Importing the plugin from the Unity asset store
First, import the Async Await Support plugin from the Unity asset store. Add the plugin to your Unity asset store account and fetch it to your project using PackageManager from the Window section. Choose My Assets option in Packages and choose the Async Await Support package to install and import it to the project.
Ensure that the account you are signed in to is the same one to which you have added the Async Await Support plugin. If you haven't added the plugin to the assets of your Unity account, add it from the Asset store.
After importing the Async Await Support plugin from the server, you can use it in your project.
As we did in the previous part of the blog, we need to declare the variables that will be used for handling the timer. So, let's declare them first.
Timer Increment in AsyncAwait
With AsyncAwait you don't need to increase the timer in each frame like we did in Update() method above. We need to set the timer with a 1-second time interval which will increment the timer by 1-second until it reaches the total time provided in the totalTime.
Here, async is added before the return type void. It makes the StartTimer() method an async method, which runs in a different thread than the normal code running thread. You can easily apply await to wait for the given time interval in the method. We will apply a 1-second real-time interval. After the timer is up, OnTimeOut event is triggered to inform that the timer is up.
You might have seen some familiarity in the code above. It is almost similar to the timer increment process usingCoroutine. The only differences areIEnumerator is replaced with async and yield return is replaced with await.
Now, we just need to reset and start the timer for a given time, and it will work fine. In our case, we trigger an Action event once the timer is up. You can assign any reference to it.
Now, call the StartTimer() method and provide the total time and callback method. We will call this in Start() method for this code.
We have provided a total of 10 seconds to be run in the timer, and when it's up, the TimeOut method will be called. You can invoke any action whenever the timer is up in the TimeOut() function.
Stopping the Timer in AsyncAwait
For stopping any loop or method which uses AsyncAwait, we need to use CancellationToken . By assigning the CancellationToken created by CancellationSourceToken object, we can easily manage the cancellation of the running timer with AsyncAwait.
CancellationToken is a token created by the CancellationSourceToken object assigned to a task. It helps in cancelling the corresponding tasks running in multiple threads.
Create a global CancellationSourceToken reference. We will need it to cancel its thread from a different method.
Create a method to cancel the _cancellationTokenSource if it has been initiated. Apply try-catch the method to handle the case where the CancellationSourceToken object might have already been disposed, so the full cancellation process can't be executed.
The AsyncAwait applied method doesn't stop by itself when its attached GameObject is disabled or destroyed. So, call for the StopTime() method when the timer is needed to be stopped. You can always decide when to stop the timer.
Now, initialize the CancellationSourceToken and assign its token to a local CancellationToken for its easy use. Before initializing the CancellationSourceToken, don't forget to cancel it if it has already been initialized.
This is how you can create a simple timer using the Async Await plugin.
Full code for TimerWithAsyncAwait
The full code for the TimerWithAsyncAwait is below:
we have finished creating a simple timer in two ways; update() and AsyncAwait in this article. Now you know how to make a simple timer in Unity in four ways: Invoke, Coroutine, Update and AsyncAwait.
Hope you found this blog series helpful. Comment below if you have any queries.