Optimization problem with tweening UI using DOTween in Unity

I read in a Unity blog post that using normal animation with UI in Unity is not really recommended, but instead you should use tweening if you can. To test this so I tried the same animation with both methods. I'm not really expert in tweening but this was really simple animation so I thought it should be ok. Basically I'm filling a image from 0 to 1 with looping animation. When I compared these two methods of animation in the profiler I noticed that while tweening, I get spikes of about 20ms for the time the tween is active, which is similar to normal animation. I'm not sure if this is normal behavior for UI or I am doing something wrong. This is my tween code:

Tween tween;
private void OnEnable()
{
tween = image.DOFillAmount(1, 1f).From(0).SetAutoKill(false);
tween.OnComplete(() => tween.Restart());
}

private void OnDisable()
{
tween.OnComplete(null);
tween.SetAutoKill(true);
}


I also tried something like this which does the same thing:

 private void OnEnable()
{
tween = image.DOFillAmount(1, 1f).From(0).SetLoops(-1, LoopType.Restart);
}

private void OnDisable()
{
tween.Kill();
}


Profiler Screenshot:

• Can you show us the profiler spike you're concerned about? Oct 12 at 13:12
• I edited the question most of the spikes are under editor loop.
– Ivan
Oct 12 at 13:18
• Sounds like it would be worth profiling a standalone build then, to see if these are just normal editor overhead and not a problem in your game. Oct 12 at 13:18
• I will try that but the spikes only appear when i start the animation.
– Ivan
Oct 12 at 13:26
• @DMGregory I tested it in standalone and the spikes are gone, still don't understand why do the only appear when I start the animation but if is not affecting the game performance it that is good for me. At least i know to check the profiler data in builds more often now. Thank you!
– Ivan
Oct 12 at 13:37

1 Answer

As pointed out in the comments the issue was normal editor overhead. While profiling game in standalone spikes disappeared.