- Home /
Enumerate AnimationEvents
Does anybody know a way to get access to the AnimationEvents
for a particular animation from a non-Editor script? I know aboutt AnimationUtility
but that's only good for Editor scripts, it seems.
My use case is that I want to be able to add 'tags' to different points on the animation timeline, so that I can start an animation part way through. I figured the events might be a good way to simulate this, since I can use a string function argument as a tag name.
Answer by andygeers · Jul 17, 2011 at 07:28 AM
One solution I'm trying is to do it at edit time with an editor script that accesses AnimationUtility.GetAnimationEvents, and then just save the tags it finds as data on the game object being animated.
This is a workable solution - I wanted to know the times so I could detect whether the event has fired already and manually fire it. This is for when I am not playing the whole animation and starting it part way through. The situation was a door animation that fired an event to say the edge the door is blocking (tiled based game) is closed or open. It was possible to 'miss' the even because the door could be opened and closed rapidly.
Answer by Waz · Jul 15, 2011 at 09:31 PM
You could at Start play the animation through (using Sample, not actually by time passing), and in the animation event function, record the parameter and time of the animation.
This sounds like an egregious hack, but I like your style! But sadly, Sample doesn't seem to trigger the events (unless I've misunderstood what you meant?). I tried an even worse hack, where I just set the speed the a REALLY big number and tried to Play the animation quickly, but it was so quick that there was no way to get accurate time information at the point where the event was fired.
Ooh yeah, it would certainly be a kludge (though not as crazy as using AnimationEvents as tags in the first place ;-), but yes, you're right, Sample doesn't work.
Answer by Waz · Jul 16, 2011 at 10:12 PM
Okay, far less kludgy idea: have an enum property and increase it by 1 (constant curve) at each tag point. Process the curve to extract the times and associate that back to the enum.
So I'd have to play through the animation at normal speed first? Given that you think my original idea is a kludge, can you think of a better approach that doesn't require AnimationEvents in the first place? I just quite like the fact that you can place them directly onto the timeline and see them side-by-side with the curves.
No, I mean process (in code) the animation curve. Exa$$anonymous$$e the `key's (array of $$anonymous$$eyframes) for the tag enum and I suspect it will be pretty easy to find the tag time points. The question then is only whether the UI for setting the enum increment points is sufficiently comfortable for you.
And I mean no offense: a kludge is perfectly warranted when you need to do something but the functionality is not directly available. But it would certainly be that all these suggestions are a kludge simply because we're talking about using the animation system in a way far from intended.
Your answer
![](https://koobas.hobune.stream/wayback/20220612144507im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Abrupt change of position between animation transition 2 Answers
AnimationEvent has no receiver, but there is no AnimationEvent 1 Answer
Rotating arm with Animation Event 1 Answer
How to add new curves or animation events to an imported animation? 6 Answers
Can the animation editor create local rotational data? 3 Answers