Tips Tuesday: coding without using co-routines (DOTween part 2)

Second episode of advanced Unity tips. Before moving on to another tool/asset available for free, I figured I dedicate a second episode to DOTween, and in particular Sequence in DOTween.

Short recap: DOTween allow you to do manipulate an object’s transform, easily and free of charge. It is the one asset that is in every single one of my projects: commercial or otherwise.

This second tip I thought was interesting because, beside using it myself, I know of at least 2 veterans Unity professional developers that use this trick (so it’s not just me…).

Everything here could be achieved by co-routines, but I find using DOTween Sequences easier and more concise, and it is now the primary reason I use DOTween so much.

DOTween sequence allow you to invoke callback methods - you guessed it - in a sequence. You can even append things like interval. In fact you can even change the order of callbacks around and stuff, but that’s more advanced stuff.

Recall the standard unity method:

Destroy(GameObject,float);

that lets you destroy an object after a duration. Except with DOTween sequence, you can do the same with just about any method.

To use it, just create a Sequence object using the DOTween.Sequence() method, and call AppendCallback() method so you can append any thing to it (including intervals using AppendInterval()).

Let’s say you want to do the following:

  1. Call a method called FadeIn()
  2. Call a method called SaveToFile()
  3. wait 1 seconds
  4. call a clean up method called Cleanup()
  5. wait 2 seconds
  6. Quit the game

You’d just do:

DOTween.Sequence()
  .AppendCallback(FadeIn)
  .AppendCallback(SaveToDatabase)
  .AppendInterval(1)
  .AppendCallback(Cleanup)
  .AppendInterval(2)
  .AppendCallback(Application.Quit);

And that’s it. that one statement would run at the end of the current frame - not need to do anything or call some other methods to start it.

1 Like