Animating UI via scripts in Unity/C#

Let’s see how to use C# coroutines to animate UI canvas elements!

Mina Pêcheux
CodeX
Published in
8 min readNov 10, 2021

--

What about Unity animations?

When you work on your UI, you might be tempted to use “normal” Unity animations to update the various parameters of your elements. And it’s a valid and user-friendly of doing it.

For example, it allows you to quickly setup a basic fade-to-black scene transition; I’ve talked about this in more detail in a previous tutorial (available in text or video format), if you’re interested in learning more :)

But!

Despite being a great way of prototyping UI animations, this technique is not the most efficient.

The problem is that Unity’s animator engine was originally crafted and tuned for character rigs animation — so they sort of expect continuous movement that keep on updating ever so-slightly the positions of a whole bunch of bones in a complex skeleton. And this is pretty different from what you’re doing when you’re just interpolating some colour alpha between 1 and 0, right?

--

--

Mina Pêcheux
CodeX
Writer for

I’m a freelance full-stack web & game developer. I’m passionate about topics like CGI, music, data science and more! Find me at: https://minapecheux.com :)