- Home /
Removing or Editting an animationEvent
Hi, I see there is a way to add an animationevent to a clip at run-time, but can you edit an existing one (i.e. change it's time) or delete it completely from a clip at run-time? The docs don't seem to suggest that you can....?
Thanks
I've tried just changing the time of a pre-existing animationEvent, but that doesn't seem to work..anyone any ideas?
Have you tired creating the same event with different time and assigning that to your clip?
AntLewis, how did you do that? I'd like to access the animationEvents in my animation just so I have points to play the animation back form, but it seems like they're not exposed at runtime.
Answer by Bunny83 · Apr 10, 2012 at 02:47 PM
It's not possible at runtime. As you can see there's no function beside AddEvent in AnimationClip to modify / remove AnimationEvents.
AnimationEvents are designed at a part of the animation. I'm not sure why there's no function to remove AnimationEvents at runtime (since there is one to add them), but AFAIK there's no way to remove / change animation events at runtime.
In the editor you can use the AnimationUtility class, but not at runtime.
$$anonymous$$ost likely because Unity only has to inject new events at the right time which is trivial.
Answer by spilat12 · Nov 20, 2019 at 07:36 PM
Hey @AntLewis I know that I am writing very late - after 7 years, in fact - but maybe it will be useful for someone reading this now, as previous answers might misguide someone. It is true that API provides no dedicated function for removing events at runtime.
But it is/was possible to remove AnimationEvents all along by creating a list of new AnimationEvents and populating it with all the events except for the one that you would like to delete and then copying the elements of that list to the events list of the animation clip. Here is the code taken from Unity Forum, the original has a bug in it, but I fixed it:
public static void RemoveEventFromAnimator(string functionName, Animator animator)
{
AnimatorClipInfo animationClipInfo = animator.GetCurrentAnimatorClipInfo(0)[0];
AnimationClip animationClip = animationClipInfo.clip;
AnimationEvent[] animationEvents = animationClip.events;
List<AnimationEvent> updatedAnimationEvents = new List<AnimationEvent>();
for (int i = 0; i < animationEvents.Length; i++)
{
AnimationEvent animationEvent = animationEvents[i];
if (animationEvent.functionName != functionName)
{
updatedAnimationEvents.Add(animationEvent);
}
}
animationClip.events = updatedAnimationEvents.ToArray();
}