So I think this question is probably a result of my own basic lack of understanding of the various coordinate systems in 3D programming... but I'll pose it here as my specific application is related to moving around some NGUI widgets.
So, lets say I've got two UISprite rectangles on the screen. One is on the left hand side, the other is on the right. I simply want to programatically move the right hand rectangle so it is horizontally centered on the right edge of the first triangle.
Before:
xxxx
x x zzz
x x zzz
xxxxAfter:
xxxx
x zzz
x zzz
xxxxSo... it seems from various posts I've found that part of the key here is to use
NGUIMath.CalculateAbsoluteWidgetBounds to get my widget rectangle coordinates. Then I think I should be able to derive the target screen point to pass to TweenPosition.Begin on my right hand rectangle.
On button press I'm doing the following:
Bounds lBounds = NGUIMath.CalculateAbsoluteWidgetBounds(leftWidget.transform);
print(lBounds);
This generates the following log output:
Center: (-0.8, 0.0, 0.0), Extents: (0.4, 1.0, 0.0)
So, I'm deducing from this output that we are in some kind of -1:1 coordinate system. (Does this have a name one could Google to get educated? It's sort of normalized but not, because < 0 so I'm not sure what I'm even looking at here.) If my assumption is correct, it seems I should be able to figure out the right edge of my left box by taking:
xPos = (center + (width / 2)) * Screen.width / 2
[aka]
xPos = (-0.8 + (0.4 / 2)) * Screen.width / 2
... but I think this is the basic spot where I'm getting off track.
When I:
TweenPosition
.Begin(rightWidget
.gameObject, 1f,
new Vector3
(xPos, 0f
))
The box moves a bit too far to the left and overshoots it's target. Any ideas? Is there any easier way to translate the result of CalculateAbsoluteWidgetBounds into something usable by TweenPosition?