Search code examples
c#widgetunity-game-engineboundsngui

How to calculate widget bounds?


I need to find the bounds of a widget with all his children. I tried using the following code but got a strange output. Either the widget had children which were more than 100 pixels in width and height the output was something about 1.

Vector3 widgetBounds = NGUIMath.CalculateRelativeWidgetBounds(this.transform).size;

What am I doing wrong?

Thank you.


Solution

  • Does this do what you want?

    // copied from UIDragObject.UpdateBounds()
    public static Bounds GetContentRectBounds(UIRect content, UIPanel uiPanel){
        Matrix4x4 toLocal = uiPanel.transform.worldToLocalMatrix;
        Vector3[] corners = content.worldCorners;
        for( int i = 0; i < 4; ++i ){
            corners[i] = toLocal.MultiplyPoint3x4(corners[i]);
        }
        Bounds mBounds = new Bounds(corners[0], Vector3.zero);
        for( int i = 1; i < 4; ++i ){
           mBounds.Encapsulate(corners[i]);
        }
        return mBounds;
    }