Hello Guest

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

pako

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 17
    • 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.

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9673
    • View Profile
Re: Animator.Play significant FPS drop on first call
« Reply #1 on: June 18, 2018, 10:09:02 PM »
Is this the specific issue, or the fact that your sprite collection isn't loaded at that point and it pauses to load? I recommend looking in the profiler to see what the bottleneck is at that point.

pako

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 17
    • View Profile
Re: Animator.Play significant FPS drop on first call
« Reply #2 on: June 19, 2018, 04:12:05 PM »
As far as I can tell it's the sprite collection not being loaded. i.e. when the problem occurs the profiler shows tk2dSpriteCollectionData.Init() at 98.1%, whereas when there's no problem it's at 0%. Additionally, a break point after the return statement in tk2dSpriteCollectionData.Init() get's hit only when the problem occurs.

So, what would be the best way to deal with this issue?

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9673
    • View Profile
Re: Animator.Play significant FPS drop on first call
« Reply #3 on: June 29, 2018, 11:13:54 AM »
Init is where it loads the data. It does it on first enable. Easiest workaround is to have a sprite in the scene somewhere that is visible (enabled) during load. That will get it to load as the level loads.