How best to work with UI elements that "come and go".

So I have some standard buttons and text in my UI, but I also have flashy images that I want to pop up and animate when certain conditions are met.

My plan is just to have multiple images that are start off deactivated and are activated when the conditions are met. I am having trouble figuring out how to deactivate them after the animation is done.

I worked with the Animation class and could use the clip.length for other purposes, but the way I learned how to deal with UI has an Animator instead, which I am not familiar with.

Thanks!

There are probably lots of ways to solve this, but here’s one:

Animator allows you to attach State Machine Behaviours to any state. You could make a behaviour that deactivates the game object (which you’d find via the animator parameter) when entering a state, and then attach this to the appropriate part of your state machine.

2 Likes

Yea, the only thing I could find on it went into that a little bit when I googled how to get the length of the animation. I’m not sure how much resources different methods to solve these type of problems cost.

Would there be anything wrong with just having the first frame of the animation be a blank transparent texture and keeping them all active? As a noob, I should probably just test them.

Thank you for all your help btw. Usually I come back and read them and don’t want to bump my threads just to say thanks.

Right… but usually performance isn’t the biggest problem a project faces; the biggest problem it faces is never getting done. The second-biggest problem is code rot (where hacks and dependencies pile up to the point where you can no longer maintain it).

So, your first and foremost consideration should almost always be: which approach is simplest and easiest to maintain?

That’d be a good exercise! And yeah, I think this solution would probably be fine too.

Performance-wise, it’s always better to deactivate objects when you can, but… see above. :slight_smile:

No worries! One thing I really like about this forum is the “Like” feature, which I often use to indicate “Thanks!” without bumping the thread. So, that’s an option if you feel the urge.

2 Likes

I’m gonna get a tattoo of this, or maybe just a poster for my wall.

I spend so much time planning and designing systems in my head that I never get around to writing the code. Trying to cover all the ‘what ifs’ and plan systems to be scaleable and elegant is all well and good, but when it comes down to it, you’re better off building a crappy system that doesn’t suit your needs and then modifying or rebuilding that than never doing any of it in the first place.

3 Likes

But Unity UI :P.

There was a point where the cheapest way to handle inactive UI was to put it on an layer that wasn’t rendered by the camera. I haven’t benchmarked it lately to see if this is still true.

If the tiniest bit of performance matter, (which it doesn’t to the OP), it’s worth benchmarking a few options.

1 Like

Bonus points if you use No Man’s Sky (or Duke Nukem Forever but fewer remember that game) for the background. :stuck_out_tongue:

1 Like

After the animation plays (check the animator’s state) set the object’s enabled flag to false.