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.
  2. Dismiss Notice

GC Allocs within Animator.FireAnimationEvents when using AnimationEvent as target function argument.

Discussion in 'Scripting' started by Q-Ted, Aug 25, 2021.

  1. Q-Ted

    Q-Ted

    Joined:
    Dec 16, 2016
    Posts:
    46
    Hi,

    Our game currently uses animation events to call some functions which in turn execute some code if the animatorClipInfo's weight within the AnimationEvent class is above a certain value. However it appears everytime these functions get called it causes GC allocs, both in the editor and in builds.

    In the profiler shot below you can see that our functions (blacked out) don't have any allocs, but the Animator's function itself is. It seems like maybe the generation of the AnimationEvent class is causes this? Is this a bug within the engine? (2020.2.4f1) Any way to get rid of these allocations while still somehow getting the weight of the animation clip that triggered the call?

    Here's an stripped down version of the functions we are using.
    Code (CSharp):
    1.     public void OurAnimEvt(AnimationEvent evt)
    2.     {
    3.         if (evt.animatorClipInfo.weight >= m_someWeightValue)
    4.         {
    5.             // ...
    6.         }
    7.     }
     

    Attached Files:

  2. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    6,195
    Whenever the Animation event is fired, Unity new()s an AnimationEvent instance, so you'll always allocate if you use AnimationEvent parameters.

    To avoid it, you'll have to avoid the parameter, and gather the information about the weight in some other way.
     
    april_4_short likes this.
  3. april_4_short

    april_4_short

    Joined:
    Jul 19, 2021
    Posts:
    489
    So AnimationClip timeline Events without parameters are garbage free?

    If so... that's some good news!
     
  4. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    6,195
    I'm pretty sure they are, yes. Test it yourself, but there's no reason for them to involve any GC.

    A good implementation would have reused AnimationEvent instances (like you can with Collision callbacks), or just have used structs, but the Animator was written a long time ago and they really never considered the performance implications of the C# part of the API.
     
    april_4_short likes this.
  5. april_4_short

    april_4_short

    Joined:
    Jul 19, 2021
    Posts:
    489
    I had just avoided them, and all Actions, Events and Delegates (and even Lambdas) because they all throw garbage, so I'd presumed that these little things did, too.

    As you say, I should have checked. But it's gotten to the point where I'm reaching checking weariness...

    I need a nap.
     
  6. Q-Ted

    Q-Ted

    Joined:
    Dec 16, 2016
    Posts:
    46
    Yea, I pretty much expected this. This is the first time I've actually seen AnimationEvent's being used for callbacks in code as normally I'd always use int or no argument. Thank you, we'll look in to another way of solving this.