2D Toolkit Forum

2D Toolkit => Support => Topic started by: pako on June 08, 2018, 02:57:03 pm

Title: Animator.Play significant FPS drop on first call
Post by: pako 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.
Title: Re: Animator.Play significant FPS drop on first call
Post by: unikronsoftware 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.
Title: Re: Animator.Play significant FPS drop on first call
Post by: pako 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?
Title: Re: Animator.Play significant FPS drop on first call
Post by: unikronsoftware 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.