Author Topic: UISlider how to get positions along the background sprite?  (Read 2130 times)

Nubeh

  • Newbie
  • *
  • Thank You
  • -Given: 1
  • -Receive: 0
  • Posts: 41
    • View Profile
Hi,
I have a slider component and I would like to create a rectangle sprite on top of the slider's background image. I want this sprite to cover a certain area of the slider, let's say from 50% to 70%. How to I get the positioning and sizing to do that?
I know that the thumb image when positioned at those percentages will give me that position but how can that be calculated with code? I tried to get the slider's background widget width but the value is somehow much longer that on screen. How can I get the total width of my slider?

Thanks


ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: UISlider how to get positions along the background sprite?
« Reply #1 on: May 13, 2014, 09:03:20 AM »
Every UIRect (widget, panel) has localCorners and worldCorners, whichever you prefer to use. I'd suggest you take the local corners and then Vector3.Lerp between the values to get what you want. Don't forget to adjust by the slider's transform though. If you want it to be in local coordinates for positioning of another widget, use worldCorners, calculate the two points you need, then yourWidgetThatYouWantPositioned.transform.parent.InverseTransform(point) to bring it into local space.

Nubeh

  • Newbie
  • *
  • Thank You
  • -Given: 1
  • -Receive: 0
  • Posts: 41
    • View Profile
Re: UISlider how to get positions along the background sprite?
« Reply #2 on: May 13, 2014, 09:54:25 AM »
Thank you. I will try this. One little add on to the question always related to positioning. Sliders have the thumb sprite that goes left-right (in the case of a horizontal slider). If I want to have a label that follows the thumb position I can calculate the position the way you mentioned, but is there a simpler way to anchor an object to the thumb of a slider? The thing about the thumb sprite is that it seems that the sprite doesn't move, only its Box Collider changes the X coordinates. I guess that's because of the way it's implemented.

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: UISlider how to get positions along the background sprite?
« Reply #3 on: May 14, 2014, 05:16:28 AM »
Just make this label a child of the thumb. NGUI comes with a several control prefabs that have similar behaviour.