Tasharen Entertainment Forum

Support => NGUI 3 Support => Topic started by: Kolyunya on January 17, 2014, 07:28:11 AM

Title: How to calculate UIWidget size?
Post by: Kolyunya on January 17, 2014, 07:28:11 AM
Hello!

In my instance of UIRoot I have an instance of UIWidget. UIRoot's pivot is in the middle-center. And UIWidget's pivot is in the top-left. And I want to position the widget in the center of the UIRoot. I can not change pivots for a specific reason.

Essentially I need to set the widget x coordinate to half of it's width multiplied by -1 and it's y coordinate to half of it's height.

I tried to use the following code:

        Vector3 windowDimensions = NGUIMath.CalculateRelativeWidgetBounds(this.transform).size;
        Vector3 windowPosition = new Vector3();
        windowPosition.x = -1 * windowDimensions.x / 2;
        windowPosition.y = windowDimensions.y / 2;
        this.transform.localPosition = windowPosition;

But it didn't work as expected since I was geting strange numbers from "CalculateRelativeWidgetBounds". I've read something about converting them to "screen coordinates" but I don't know what it means.

Could you please tell me, what I am doing wrong and how can I fix my code?
Thank you!
Title: Re: How to calculate UIWidget size?
Post by: ArenMook on January 17, 2014, 09:29:53 PM
Why are you calculating something's bounds? If it's just one widget, then you can get its 'width' and 'height' properties.

Also if you change the pivot to center, then you won't need to do anything as the transform.localPosition is going to be the widget's center.
Title: Re: How to calculate UIWidget size?
Post by: Kolyunya on January 20, 2014, 01:47:44 AM
I need to calculate bounds of the widget and all of it's children since it's content is added dynamically.
'width' and 'height' properties tell me nothing since the widget is just a container and those properties are useless.
Title: Re: How to calculate UIWidget size?
Post by: ArenMook on January 20, 2014, 03:53:04 AM
Then have a look at NGUIMath.Calculate series of functions.
Title: Re: How to calculate UIWidget size?
Post by: Kolyunya on January 20, 2014, 04:17:47 AM
In the original message I've posted a piece of code using one of the functions of this series.
The problem is the function provides unexpected results: the widget was not in the center of the screen.
Title: Re: How to calculate UIWidget size?
Post by: ArenMook on January 20, 2014, 07:21:52 AM
Likely because you were using CalculateRelative. You were calculating the dimensions relative to 'this.transform', which already includes the localPosition offset as a part of it. You need to use the version of that function that has 2 parameters and pass the transform's parent as the 'root'.

The code snipplet you posted is quite odd in general. It sounds like you are not sure about the coordinate systems. To convert from local to world space, use transform.TransformPoint. To convert from world space to local space, use transform.InverseTransformPoint. To convert from screen to world, use camera.ScreenToWorldPoint, etc. Unity's documentation may explain this better.