NGUI – Smooth Tween causing Spring Position to never complete

Recently I’ve run into a nasty bug using NGUI’s UIGrids for a Solitaire game at Mukutu Studio. My NGUI version was 3.7.0 or maybe 3.6.9

When you use UIGrids with cell height and width of zero or a very low value and animate the elements using Smooth Tween it seems that the animations never complete sometimes. This can cause a lot of problems, specially if you need to check if there’s something moving (using gameObject.getComponent<SpringPosition>().enabled).

I’ve posted my fix to this problem at NGUI’s forum here: http://www.tasharen.com/forum/index.php?topic=10909.msg50894#msg50894 but you can read it bellow too:

Hi!

I’m developing a solitaire card game using UIGrids and Smooth Tween to change the cards from one UIGrid to another.
Sometimes, when I do the reparenting, some Cards have their Spring Position active for ever, with the position changing constantly but with very very small values (visually imperceptible)…

I’ve put a Log to track it down and found that it was not getting in to the “enabled = false;” line… (ex. 2.328306E-15 >= 1.392746E-10 == false):

Code:
// line 96 of SpringPosition Class
if (mThreshold == 0f) mThreshold = (target - mTrans.localPosition).sqrMagnitude * 0.00001f;
mTrans.localPosition = NGUIMath.SpringLerp(mTrans.localPosition, target, strength, delta);

if (mThreshold >= (target - mTrans.localPosition).sqrMagnitude)
{
    mTrans.localPosition = target;
    NotifyListeners();
    enabled = false;
}

To solve it I’m checking if the local positions are different before applying the Spring Position in the UIGrid Class (If the positions are the same, there’s no need to animate them)
> “&& Vector3.SqrMagnitude(t.localPosition – pos) >= 0.0001f”

Code:
// Line 369 of UIGrid.cs
if (animateSmoothly && Application.isPlaying && Vector3.SqrMagnitude(t.localPosition - pos) >= 0.0001f)
{
    SpringPosition sp = SpringPosition.Begin(t.gameObject, pos, 15f);
    sp.updateScrollView = true;
    sp.ignoreTimeScale = true;
}
else t.localPosition = pos;

Solved my problem.. Any chance to have this (or something like that) in the next update?

Thanks

 

Join the conversation

2 Comments

  1. Dude, THX A LOT!
    You save my life man. I was animation a UIGrid, and the first time the animation was played, a strange lag appear.
    Thx for points the way!

Leave a comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.