Skip to content

CountdownTimer

Ed edited this page Aug 20, 2020 · 3 revisions

A utility class to keep track of a countdown and receive a callback when the remaining minutes or seconds changed.

Executing a countdown is a very common task in game development. Though it's not a complex task, you can see yourself repeating the same code over and oveer again if you have multiple countdowns. This class encapsulates those lines to avoid this situation.

The most common use case for the CountdownTimer class is creating a countdown to be displayed in the UI. The CountdownTimer uses a callback to inform when the minutes or seconds of the timer changed so you can update your labels accordingly at those specific moments, instead of doing it every frame, which helps generating less garbage for the GC.


Example of usage

Included with the Common Utils there's this component: SimpleCountdownDisplayController which is ready to use.

Take a look at the code for an example of usage. To test this component, attach it to a Unity.UI.Text game object and set the desired total time (in seconds) in the inspector.

Clone this wiki locally