- Home /
How do I access the current playing animation clip of a gameObject?
More specifically, I need to check to ensure that the current "Pressed" animation clip of the Unity 4.6 Button has finished before I fire the onClick event.
I've found many posting about various techniques on how to check if an animation clip is still playing, wait for complete, etc. -- but they all make assumptions that I already have a handle to the animation/clip itself.
However, I have a more fundamental issue, given a gameObject (in this case, a UI Button game object), how do I traverse the object relationships, use GetComponents, etc. -- to get to what I need to do the check I want?
Everything I've tried has thrown an error. I am programming using UnityScript/Javascript.
Thanks, Manny
I'm not 100% sure what your ai$$anonymous$$g for it's not clear if you're using the legacy animation or mecanim.
The most simple way is to record when the animation starts and check for the time it takes the animation to play then trigger the event. I do this for firing arrows, only fire if the animation has had time to reach a $$anonymous$$imum fire level.
Oh and if you get an answer mark it as correct and click thumbs up for comments that help. $$anonymous$$arma points don't overly bother me but you'll get more help if you take the trouble to simply click answered.
What I'm trying to do is use the new Unity UI Button transition animation. I made a pulsate grow animation where once a button is clicked, it expands a bit and then shrinks back down to normal size.
What I've discovered, sadly, is that as cool as it is that Unity helps in creating the animation by automatically adding the controller, and assigning it to the button, etc -- it's still not a fully baked solution just yet. I've encountered a number of issues/shortco$$anonymous$$gs:
There is no ready support for an onclick sound effect.
There is no obvious way to specify that the animation be allowed to run its full cycle before firing the onclick event function.
There are issues with rapid clicks causing issues with animations, etc.
So I'm trying to code my way around these feature gaps and quirks but what I'm slowly seeing is my code is beco$$anonymous$$g spaghetti. As soon as I think I've solved one issue another comes up.
I may have to abandon using the built in animation support altogether and try to come up with an alternate approach.
Did you look at my answer? By the way there are many ways to use animation to play sounds. Like having a script with a PlaySound method and call this method in your animation by adding a new animation event.\
Cheers
Yes I saw your reply and I thank you for your efforts.
I ended up using the following code to get access to a gameObject's animation state data:
var anim = myGameObject.GetComponent(Animator);
var stateInfo : AnimatorStateInfo = anim.GetCurrentAnimatorStateInfo(0);
But I had to abandon that path for my code was beco$$anonymous$$g too convoluted. I realize that there is a way to get an animation to play a sound at the desired moment. What I had done was simply add an AudioSource component to my button in the Inspector, turn on AutoPlay and simply have the animation turn on the component at the start of the animation clip and off again at the end.
$$anonymous$$y larger point, though, was that I had wished that this was baked into the UI.Button a little better as to simplify the process a little bit more.
I'm not ungrateful for what Unity has provided thus far in the new UI system but I await the next revision to see what else they can improve upon.
$$anonymous$$y opinion is that it's really easy to just attached a simple script to your game object and use animation events ^^