TobiasWehrum / unity-utilities

A collection of Unity3D scripts I've been sharing between projects - open source, fully commented and with examples.
MIT License
274 stars 27 forks source link

Phaser-like countdowns #7

Open oxysoft opened 8 years ago

oxysoft commented 8 years ago

One thing I absolutely loved during the time I used Phaser for HTML5 game development is the way you did countdowns and timers.

If we could do timers and countdowns like this, it would be insanely useful

new Countdown(5f, () => {
    Destroy(gameObject); // run this code after 5 seconds
});

I love this method of doing things because the code that runs after the countdown is grouped together with its declaration. Super clean and useful. And, it makes it really easy to do things like this

new Countdown(0.5f, () => {
    Debug.Log("foo");
    new Countdown(0.5f, () => {
        Debug.Log("bar!");
    }
});

Prints foo after half a second, then bar after another half a second.

Of course, you should still be able to keep these countdown objects and cancel them at any time, or restart, etc. We're just changing the way you declare the code you wish to run on completion or progress.