Tasharen Entertainment Forum
Support => NGUI 3 Support => Topic started by: Nubeh on May 13, 2014, 06:51:12 AM
-
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
-
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.
-
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.
-
Just make this label a child of the thumb. NGUI comes with a several control prefabs that have similar behaviour.