Author Topic: UISlider thumb position bug  (Read 7173 times)

Decco

  • Newbie
  • *
  • Thank You
  • -Given: 2
  • -Receive: 1
  • Posts: 20
    • View Profile
UISlider thumb position bug
« on: March 25, 2015, 09:55:32 AM »
Hello ArenMook,

I have encounter un problem with the slider thumb position.
The thumb position is not correctly set, if you change the value of the slider while the control scale is Zero. (see attachment)

To replicate it :
- Set the LocaleScale of the UISlider to 0,0,0
- Change the UISlider value
- Set the LocaleScale of the UISlider to 1,1,1
- The thumb is at the center and not at the value

Why am I setting the scale to 0 ? Because of some transition/animation purpose.

I dug in the code to see where the pb come from :
The SetThumbPosition of UIProgressBar use InverseTransformPoint to setup the position, which is affected by the scale.
But I think that the scale of a widget should not affect his behaviour..

  1.         protected void SetThumbPosition (Vector3 worldPos)
  2.         {
  3.                 Transform t = thumb.parent;
  4.  
  5.                 if (t != null)
  6.                 {
  7.                         worldPos = t.InverseTransformPoint(worldPos);
  8.                         worldPos.x = Mathf.Round(worldPos.x);
  9.                         worldPos.y = Mathf.Round(worldPos.y);
  10.                         worldPos.z = 0f;
  11.  
  12.                         if (Vector3.Distance(thumb.localPosition, worldPos) > 0.001f)
  13.                                 thumb.localPosition = worldPos;
  14.                 }
  15.                 else if (Vector3.Distance(thumb.position, worldPos) > 0.00001f)
  16.                         thumb.position = worldPos;
  17.         }
  18.  

Decco
« Last Edit: March 26, 2015, 11:45:14 AM by Decco »

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: UISlider thumb position bug
« Reply #1 on: March 25, 2015, 12:15:07 PM »
Zero scale is invalid. Think about it... you can't divide by zero, so how can an inverse matrix be calculated from it?

Decco

  • Newbie
  • *
  • Thank You
  • -Given: 2
  • -Receive: 1
  • Posts: 20
    • View Profile
Re: UISlider thumb position bug
« Reply #2 on: March 26, 2015, 11:42:56 AM »
Yeah that's excalty why this is bugged with Scale 0,0,0

Notice that the foreground is corretly calculated, may be you can use it to setup the thumb position without using the inverse matrix ?
Or even easier, you know the width of the UISlider and his value, just get the thumb position with a Lerp ;-)

Decco

  • Newbie
  • *
  • Thank You
  • -Given: 2
  • -Receive: 1
  • Posts: 20
    • View Profile
Re: UISlider thumb position bug
« Reply #3 on: April 06, 2015, 06:54:30 AM »
Any solution ?

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: UISlider thumb position bug
« Reply #4 on: April 06, 2015, 08:14:01 AM »
I told you... scale of zero is invalid. It's not a bug. You can't divide by zero. Use a small value instead such as 0.01.

Decco

  • Newbie
  • *
  • Thank You
  • -Given: 2
  • -Receive: 1
  • Posts: 20
    • View Profile
Re: UISlider thumb position bug
« Reply #5 on: April 08, 2015, 05:42:45 AM »
yeah but ..
Can you tell me why you are using a world position to set the thumb and not a local position ?

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: UISlider thumb position bug
« Reply #6 on: April 09, 2015, 03:31:17 PM »
Because to determine where on the screen the interaction occurs you need to take screen space, convert it to world space, then inverse transform it to local space.

You can't go from screen to local. Raycast result is always world space, which then gets inverse transformed to local.

Zero scale makes inverse transform impossible.