Author Topic: Scroll View shaking (center and offset are always changing)  (Read 18189 times)

gorksmash

  • Newbie
  • *
  • Thank You
  • -Given: 1
  • -Receive: 0
  • Posts: 5
    • View Profile
Scroll View shaking (center and offset are always changing)
« on: February 28, 2014, 11:14:15 AM »
I have a scroll view that I am resizing with a grid inside it.
My hierarchy is as follows:

UI Root
-Camera
--UI Sprite (Window Background)
---UIDrag Resize (target: Window Background)
---Scroll View (Unified Anchor to Window Background)
----Grid
-----Grid Items

The Scroll View has it's UIPanel Offset and Center constantly changing. This causes the X or Y position to change back and forth between two numbers (shaking).
If I turn off clipping the problem goes away but only because the Offset and Center no longer apply.
If I turn off the Anchoring the problem goes away but then the scroll view no longer resizes with everything else.

Sometimes you have to use the scroll bars or the resizer to get it to shake.

any help? I can post a video or something if this is not clear...

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Scroll View shaking (center and offset are always changing)
« Reply #1 on: February 28, 2014, 06:03:50 PM »
So it happens when you have an anchored scroll view + scroll bar to go with it? Removing the scroll bar also makes the issue disappear?

It's odd because the Drag & Drop example is set up exactly like that. Anchored scroll view that has a scroll bar. And I see no issues there.

gorksmash

  • Newbie
  • *
  • Thank You
  • -Given: 1
  • -Receive: 0
  • Posts: 5
    • View Profile
Re: Scroll View shaking (center and offset are always changing)
« Reply #2 on: March 03, 2014, 10:07:34 AM »
The scroll bars themselves have no effect on the situation (off or on) but when I resize the clipped panel (sometimes even before I resize) I will post a video explaining the issue but the center and offset attributes of the clipped panel keep changing numbers. Sometimes they count in one direction towards + or - infinity
or sometimes it alternates between .5 px (e.g. 630 to 629.5) which causes the panel's actual position to alternate with it many times a second.

gorksmash

  • Newbie
  • *
  • Thank You
  • -Given: 1
  • -Receive: 0
  • Posts: 5
    • View Profile
Re: Scroll View shaking (center and offset are always changing)
« Reply #3 on: March 03, 2014, 03:50:29 PM »
here is the video showing the problem http://youtu.be/DP6oiD1buEk I have started a brand new project and everything and the problem still occurs.
Combining the drag resize script and a scroll view script causes the new anchors to go crazy I guess.
using:
NGUI 3.5.1 Unity 4.3.0
(will upgrade to NGUI 3.5.3 and Unity 4.3.4 tomorrow)

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Scroll View shaking (center and offset are always changing)
« Reply #4 on: March 03, 2014, 10:52:30 PM »
I can't seem to reproduce it on my end. Also there should never be ".5" anywhere. Make sure your window's background is centered, not top left pivot. The scroll view is always centered as well, so whatever is being resized needs to match.

gorksmash

  • Newbie
  • *
  • Thank You
  • -Given: 1
  • -Receive: 0
  • Posts: 5
    • View Profile
Re: Scroll View shaking (center and offset are always changing)
« Reply #5 on: March 04, 2014, 01:11:57 PM »
Thanks for that last reply (and keeping tabs on this issue) It turns out if you have the anchor set to a corner rather than a centered position, the new anchor system in conjunction with the scroll view (and possibly the drag resize) it shakes and causes several of the numbers to go crazy.
Center your pivots people.

BurningToad

  • Newbie
  • *
  • Thank You
  • -Given: 1
  • -Receive: 0
  • Posts: 9
    • View Profile
Re: Scroll View shaking (center and offset are always changing)
« Reply #6 on: March 06, 2014, 04:31:04 PM »
Hello, I had this same problem.  My panel/window background is also pivoted on the top left.  However, for a variety of reasons, I can't change that without a lot of pain.  So, I had to look for an alternative solution. 

Basically the panel was doing strange things with non-even number sizes.  It was either bouncing like in his video, or even if it didn't LOOK like it was bouncing, the offset and position were just increasing constantly every single frame.  This depended on the size of my realizable window.  Some sizes it would bounce, some it wouldn't.  Then, it also depended on my current scroll value, so some sizes it didn't bounce until I scrolled to a certain value...

I was able to fix it with the following workaround:

In UIPanel.OnAnchor()
changed:
      float newX = Mathf.Lerp(lt, rt, 0.5f);
      float newY = Mathf.Lerp(bt, tt, 0.5f);
to:
      float newX = Mathf.Round(Mathf.Lerp(lt, rt, 0.5f));
      float newY = Mathf.Round(Mathf.Lerp(bt, tt, 0.5f));

Basically, I just rounded the numbers to ensure they were integers, and not some floating point decimal that lead to bouncing or increasing each frame.

Not saying this is a good solution, but it solves my problem.


jbazerbo

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 8
    • View Profile
Re: Scroll View shaking (center and offset are always changing)
« Reply #7 on: March 07, 2014, 04:14:42 AM »
Same problem too. Need a fix.

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Scroll View shaking (center and offset are always changing)
« Reply #8 on: March 07, 2014, 09:32:44 AM »
@BurningToad: That doesn't fix the issue for me.

You can force the widget resizing to always work using even numbers by modifying NGUIMath.AdjustWidget function like so:
  1.                 // Centered pivot should mean having to perform even number adjustments
  2.                 if (iLeft == 0 || iRight == 0)
  3.                 {
  4.                         iLeft = ((iLeft >> 1) << 1);
  5.                         iRight = ((iRight >> 1) << 1);
  6.                 }
  7.  
  8.                 if (iBottom == 0 || iTop == 0)
  9.                 {
  10.                         iBottom = ((iBottom >> 1) << 1);
  11.                         iTop = ((iTop >> 1) << 1);
  12.                 }

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Scroll View shaking (center and offset are always changing)
« Reply #9 on: March 07, 2014, 10:02:44 AM »
You can also remove the jitter by getting rid of UpdatePosition() call inside UIPanel's baseClipRegion:
  1.         public Vector4 baseClipRegion
  2.         {
  3.                 get
  4.                 {
  5.                         return mClipRange;
  6.                 }
  7.                 set
  8.                 {
  9.                         if (Mathf.Abs(mClipRange.x - value.x) > 0.49f ||
  10.                                 Mathf.Abs(mClipRange.y - value.y) > 0.49f ||
  11.                                 Mathf.Abs(mClipRange.z - value.z) > 0.49f ||
  12.                                 Mathf.Abs(mClipRange.w - value.w) > 0.49f)
  13.                         {
  14.                                 mResized = true;
  15.                                 mCullTime = (mCullTime == 0f) ? 0.001f : RealTime.time + 0.15f;
  16.                                 mClipRange = value;
  17.                                 mMatrixFrame = -1;
  18.  
  19.                                 //UIScrollView sv = GetComponent<UIScrollView>();
  20.                                 //if (sv != null) sv.UpdatePosition();
  21. #if UNITY_EDITOR
  22.                                 if (!Application.isPlaying) UpdateDrawCalls();
  23. #endif
  24.                         }
  25.                 }
  26.         }
...just be aware that this means that your scroll views won't keep their scrolled position as you resize them.

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Scroll View shaking (center and offset are always changing)
« Reply #10 on: March 07, 2014, 10:06:51 AM »
Although looking at this closer, I think the proper fix is to get rid of Mathf.Round inside UIScrollView.SetDragAmount.
  1.         public virtual void SetDragAmount (float x, float y, bool updateScrollbars)
  2.         {
  3.                 if (mPanel == null) mPanel = GetComponent<UIPanel>();
  4.  
  5.                 DisableSpring();
  6.  
  7.                 Bounds b = bounds;
  8.                 if (b.min.x == b.max.x || b.min.y == b.max.y) return;
  9.  
  10.                 Vector4 clip = mPanel.finalClipRegion;
  11.  
  12.                 float hx = clip.z * 0.5f;
  13.                 float hy = clip.w * 0.5f;
  14.                 float left = b.min.x + hx;
  15.                 float right = b.max.x - hx;
  16.                 float bottom = b.min.y + hy;
  17.                 float top = b.max.y - hy;
  18.  
  19.                 if (mPanel.clipping == UIDrawCall.Clipping.SoftClip)
  20.                 {
  21.                         left -= mPanel.clipSoftness.x;
  22.                         right += mPanel.clipSoftness.x;
  23.                         bottom -= mPanel.clipSoftness.y;
  24.                         top += mPanel.clipSoftness.y;
  25.                 }
  26.  
  27.                 // Calculate the offset based on the scroll value
  28.                 float ox = Mathf.Lerp(left, right, x);
  29.                 float oy = Mathf.Lerp(top, bottom, y);
  30.  
  31.                 // Update the position
  32.                 if (!updateScrollbars)
  33.                 {
  34.                         Vector3 pos = mTrans.localPosition;
  35.                         if (canMoveHorizontally) pos.x += clip.x - ox;
  36.                         if (canMoveVertically) pos.y += clip.y - oy;
  37.                         mTrans.localPosition = pos;
  38.                 }
  39.  
  40.                 if (canMoveHorizontally) clip.x = ox;
  41.                 if (canMoveVertically) clip.y = oy;
  42.  
  43.                 // Update the clipping offset
  44.                 Vector4 cr = mPanel.baseClipRegion;
  45.                 mPanel.clipOffset = new Vector2(clip.x - cr.x, clip.y - cr.y);
  46.  
  47.                 // Update the scrollbars, reflecting this change
  48.                 if (updateScrollbars) UpdateScrollbars(mDragID == -10);
  49.         }

BurningToad

  • Newbie
  • *
  • Thank You
  • -Given: 1
  • -Receive: 0
  • Posts: 9
    • View Profile
Re: Scroll View shaking (center and offset are always changing)
« Reply #11 on: March 07, 2014, 01:33:06 PM »
Although looking at this closer, I think the proper fix is to get rid of Mathf.Round inside UIScrollView.SetDragAmount.

This fix seems to work well for me.  Thanks for the help!