【问题标题】:How to calculate widget bounds?如何计算小部件边界?
【发布时间】:2014-01-20 07:42:38
【问题描述】:

我需要找到一个小部件及其所有孩子的边界。我尝试使用以下代码,但得到了一个奇怪的输出。小部件有宽度和高度超过 100 像素的子级,输出大约为 1。

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

我做错了什么?

谢谢。

【问题讨论】:

    标签: c# widget unity3d bounds ngui


    【解决方案1】:

    这是你想要的吗?

    // 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;
    }
    

    【讨论】:

    • 喜欢Encapsulate() 而不是SetMinMax(),如此壮观。 :D
    猜你喜欢
    • 2012-02-22
    • 1970-01-01
    • 1970-01-01
    • 2021-02-20
    • 1970-01-01
    • 2020-09-10
    • 1970-01-01
    • 1970-01-01
    • 2016-06-10
    相关资源
    最近更新 更多