- Home /
Animate GUI elements by code?
I have a user interface with a info button, when you press it a text box should animate forward, by animating the scale of it, so it "grows out". I've done this by using coroutines that animates the scaling with Lerp. The problem is that if you press the button twice you get strange behaviors; it's like the courotines stacks up and plays again and again. I've tried using StopCoroutine("AnimateScale") and StopAllCoroutines() to abort the first animation but it didn't work.
So how would you animate your GUI elements with code? Should I just move all the animation code in to the Update() method and control it with Booleans wheater the animation should be playing or not? The problem then is that the code needs to check every frame if it should animate or not, and most of the time it shouldn't animate (it's not a button that you should need to press very often), so it sounds like a waste of performance. That's way I looked into coroutines, for performance, but they don't seam to give me the control I need. Since I need to be able to abort or reverse the animation at any time.
Answer by Zodiarc · Jun 03, 2016 at 01:20 PM
Why the hassle with animating it by code? It's much easier using animation controllers and the unity animation system. >ou setup your state machine and within your code you can switch the states as you want.
With code I can dynamically change the size of the textbox depending on how much text it needs to fit.
I think this also can be done by the UI system itself. You can also use a scrollview if it shouldn't be possible. Here's an example in my current project how a scroll view behaves:
https://www.youtube.com/watch?v=qrXYOgYQcg0
And a 10 secons google search revealed the following: http://docs.unity3d.com/$$anonymous$$anual/HOWTO-UIFitContentSize.html
I will look into what the UI system can do itself before I go back to the code :)
Your answer
Follow this Question
Related Questions
Making a real coroutine 3 Answers
How to make a GUI button 1 Answer
Animated GUITexture based on Sprite Sheet? 0 Answers
GUI help ! 1 Answer