Skip to content

Instantly share code, notes, and snippets.

@SiarheiPilat
Last active November 23, 2019 20:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save SiarheiPilat/baa75368adcf3dd6652d831cb23847ab to your computer and use it in GitHub Desktop.
Save SiarheiPilat/baa75368adcf3dd6652d831cb23847ab to your computer and use it in GitHub Desktop.
Unity animation events or AnimationUtility API does not contain methods for deleting animation clip events at runtime. The following code makes it possible.
/// <summary>
/// Unity animation events or AnimationUtility API does not contain methods for deleting animation clip events at runtime.The following code makes it possible.
/// Taken from (with a minor bug fix): https://forum.unity.com/threads/remove-animation-event.78957/
/// </summary>
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();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment