Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.

Remove Animation event

Discussion in 'Editor & General Support' started by yinono, Feb 21, 2011.

  1. yinono

    yinono

    Joined:
    Sep 15, 2010
    Posts:
    27
    Hi,
    I use AddEvent on animation clip to add few AnimationEvents for a specific model.
    Then I load a different scene, which have a copy of the same model.
    It appears that the new model's animation still contain the events added to a different model in a different scene.
    This model doesn't have the same Components attached to it, so I get an Exception saying that AnimationEvent <functionName> has no receiver.

    Isn't this a bug? Shouldn't all animation event added by code should be remove when changing scenes?
    If not, is there a way to remove animation events, so I'll be able to remove all animation events I added before continuing to the next scene?

    Thanks,
    Yinon
     
  2. andeeeee

    andeeeee

    Joined:
    Jul 19, 2005
    Posts:
    8,768
    Please can you post the script where you add the events?
     
  3. yinono

    yinono

    Joined:
    Sep 15, 2010
    Posts:
    27
    Here the method that adds the event
    The HasClipEvent and AddClipEvent calls are just used to keep track of the names of the event added so I wont add the same event twice

    :


    Code (csharp):
    1. private void setupAnimation(string animationName, int layer, string functionName, string functionParam, float eventTime)
    2.     {
    3.         animation[animationName].layer = layer;
    4.         if(functionName != null   AnimEventHandlers.HasClipEvent(animation[animationName].clip,functionName) == false)
    5.         {
    6.             AnimationEvent animEvent = new AnimationEvent();
    7.             animEvent.functionName = functionName;
    8.             if(functionParam != null)
    9.             {
    10.                 animEvent.stringParameter = functionParam;
    11.             }
    12.             float localEventTime = eventTime;
    13.             if(eventTime == -1)
    14.             {
    15.                 localEventTime = animation[animationName].clip.length * 0.9f;
    16.             }
    17.             animEvent.time = localEventTime;
    18.             animation[animationName].clip.AddEvent(animEvent);
    19.             AnimEventHandlers.AddClipEvent(animation[animationName].clip, functionName);
    20.             //print("Added animation event: anim= " + animationName + " time = " + animEvent.time + " functionName = " + animEvent.functionName + "param = " + animEvent.stringParameter);
    21.         }
    22.        
    23.     }
     
  4. andeeeee

    andeeeee

    Joined:
    Jul 19, 2005
    Posts:
    8,768
    It turns out that this has already been logged as a bug (it is case number 230246 if you want to follow it up) but it hasn't been fixed yet.
     
  5. ddt

    ddt

    Joined:
    Sep 14, 2010
    Posts:
    8
    Do we have an update to this bug? I'm trying to add an animation event which only triggers the first time and then removes itself. How would you recommend I do this, particularly in such a way that I can trigger a one-shot event going either forwards or backwards in the animation. My application is to use it as a trigger to stop looping sounds with enough of a margin before the animation completion that they have a little while to fade out.

    Thanks so much!
     
  6. 3Duaun

    3Duaun

    Joined:
    Dec 29, 2009
    Posts:
    600
    +1, is there any update on this annoyance ?
     
  7. ddt

    ddt

    Joined:
    Sep 14, 2010
    Posts:
    8
    As a workaround, I ended up creating my own looping sound support and just putting repeating animation events at the start and end of animations. Did the trick.
     
  8. modegames

    modegames

    Joined:
    Dec 1, 2014
    Posts:
    35
    You can remove events like this:

    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;
    if(animationEvent.functionName != functionName){
    updatedAnimationEvents.Add(animationEvent);
    }
    }

    animationClip.events = updatedAnimationEvents.ToArray ();
    }
     
    Whatever560, tarnumius and Pulas like this.
unityunity