Hello Guest

Author Topic: Animator.Play significant FPS drop on first call  (Read 25 times)

pako

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 16
    • View Profile
Animator.Play significant FPS drop on first call
« on: June 08, 2018, 02:57:03 PM »
Hi,

I have the following code:

protected tk2dSpriteAnimator Animator;

void Awake()
{
     Animator = GetComponent<tk2dSpriteAnimator>();
}

void MyMethod()
{
      Animator.Play(CurrentAnimationClipName);
}

Up to now, I've calling MyMethod() to set the Player's animation before the Player became visible, and there was no problem.

I recently added one more Player animation which gets set during gameplay (where CurrentAnimationClipName = Problem_AnimationClipName). In this case, the very first time that I call MyMethod() there is a significant FPS drop while the animation changes.  Visually, it's like the game freezes for a second and then the game continues normally. There's no problem in subsequent calls to MyMethod(). I used the Profiler to identify that the FPS spike is due to MyMethod().

I have found a temporary fix:

void Awake()
{
     Animator = GetComponent<tk2dSpriteAnimator>();

     Animator.Play(Problem_AnimationClipName);
     Animator.Stop(Problem_AnimationClipName);
}

This fix works because the correct clip gets sets after Awake(), and well, game freeze is not a problem any more.

Any ideas what could be causing this, and how I could fix it in a more elegant way?

Thank you in advance.