Author Topic: ScrollView Y-position variable increasing endlessly  (Read 2747 times)

Biggix

  • Newbie
  • *
  • Thank You
  • -Given: 2
  • -Receive: 0
  • Posts: 33
    • View Profile
ScrollView Y-position variable increasing endlessly
« on: December 09, 2014, 05:51:09 PM »
Hi,

I'm implementing a slots game and using 5 vertical ScrollViews for 5 reels with symbols on them.

I use a sequence of tweens to animate the ScrollReel.transform.localPosition and ScrollReel.GetComponent<UIPanel> ().clipOffset (inversed value). This works fine and I get the desired motion effect with nice bounces etc.

However I foresee a problem, the Y position of Transform (and it's inversed negative ClipOffset friend) tend to increase a good amount with every slots spin. This can lead to really big numbers after several thousands of spins and I'm afraid of overflows. And it's generally not too nice to let variables get this high. Also, the tweens somehow are not too precise and sometimes the reels do not stop at the exact position.

I've tried all I could think of, to reset ScrollReel Y back to 0 (this should do the trick, as the moving reel always stops at 0 object location)

1) This does the trick, but produces nasty flickering

  1. offset.y = 0.01f;
  2. ScrollReel.transform.localPosition = offset;

2) This does the trick, but is not invinsible to the user & leads to the reel disappearing ocassionally

  1. offset.y = 0.01f;
  2. SpringPanel.Begin (ScrollReel, offset, 1000000f);

What I'm looking for is a method to reset the Y coordinate of the ScrollReel back to 0, the sprites are the same, I just need to reduce the flickering and noticeable reel movement while doing that. I know a workaround (placing duplicate sprites in front of the scroll wheels) but it's painful & maybe you have better considerations. Maybe there's a way to turn off the draw calls while changing the Y location.

It's my first day in Unity so don't judge too strictly. Thanks!

Biggix

  • Newbie
  • *
  • Thank You
  • -Given: 2
  • -Receive: 0
  • Posts: 33
    • View Profile
Re: ScrollView Y-position variable increasing endlessly
« Reply #1 on: December 11, 2014, 12:37:03 AM »
Maybe this could help someone. I couldn't find a reliable flicker-free way to revert the Y position of the UIScrollviews back to zero. So I just cover the screen with a LevelUp overlay in every 5-7 minutes and perform the reverting underneath. This works!

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: ScrollView Y-position variable increasing endlessly
« Reply #2 on: December 11, 2014, 09:11:59 AM »
Just a note, it's quite easy to run into floating point precision issues with Unity, as the range of floats is quite limited, and tends to lead to issues as it gets above a few thousand. What I'd do in your case is use a fract type operation. Say your content is 800 pixels tall. This means that when offset is 0, you get the same result as when position is 800. Just run a while loop:
  1. while (currentOffset > 800) currentOffset -= 800;

Biggix

  • Newbie
  • *
  • Thank You
  • -Given: 2
  • -Receive: 0
  • Posts: 33
    • View Profile
Re: ScrollView Y-position variable increasing endlessly
« Reply #3 on: December 11, 2014, 11:01:47 AM »
Thank you for the reply. I indeed got the floating point inadequacies after several hundreds of spins. I managed to minimize them by allowing small pauses (0.1 sec) after stopping of all tweens.

As for the code you suggested. I tried it. It works, but there is a nasty flicker at the moment the offset is substracted -800. And sometimes the objects just disappear without being redrawn. If there was a way to do it flicker-free, this would be amazing.

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: ScrollView Y-position variable increasing endlessly
« Reply #4 on: December 12, 2014, 11:09:05 PM »
Not sure why it would flicker in your case, but you can always force a refresh immediately by using NGUITools.ImmediatelyCreateDrawCalls, or just calling Refresh() on the panel.