コード例 #1
0
    // 父节点在父节点坐标系下的各条边
    public static void getParentSides(GameObject parent, Vector3[] sides)
    {
#if MAKE_CS_DLL
        callStatic(UTILITY_WIDGET, "getParentSides", new object[] { parent, sides });
#else
        WidgetUtility.getParentSides(parent, sides);
#endif
    }
コード例 #2
0
    protected void updateRect(bool force = false)
    {
        if (!force && !mDirty)
        {
            return;
        }
        mDirty = false;
        float   width      = 0.0f;
        float   height     = 0.0f;
        Vector3 pos        = Vector3.zero;
        UIRect  parentRect = WidgetUtility.findParentRect(gameObject);

        if (parentRect != null)
        {
            GameObject parent = parentRect.gameObject;
            mParentSides = WidgetUtility.getParentSides(parent);
            float thisLeft   = mAnchorPoint[0].mRelative * mParentSides[2].x + mAnchorPoint[0].mAbsolute;
            float thisRight  = mAnchorPoint[2].mRelative * mParentSides[2].x + mAnchorPoint[2].mAbsolute;
            float thisTop    = mAnchorPoint[1].mRelative * mParentSides[1].y + mAnchorPoint[1].mAbsolute;
            float thisBottom = mAnchorPoint[3].mRelative * mParentSides[1].y + mAnchorPoint[3].mAbsolute;
            width  = thisRight - thisLeft;
            height = thisTop - thisBottom;
            pos.x  = (thisRight + thisLeft) / 2.0f;
            pos.y  = (thisTop + thisBottom) / 2.0f;
        }
        else
        {
            width  = mAnchorPoint[2].mAbsolute - mAnchorPoint[0].mAbsolute;
            height = mAnchorPoint[1].mAbsolute - mAnchorPoint[3].mAbsolute;
        }
        if (width < 0)
        {
            UnityUtility.logError("width error in anchor!");
        }
        if (height < 0)
        {
            UnityUtility.logError("height error in anchor!");
        }
        UIWidget thisWidget = WidgetUtility.getGameObjectWidget(gameObject);

        // 没有widget则是panel,panel是没有宽高的
        if (thisWidget != null)
        {
            thisWidget.width  = (int)(width + 0.5f);
            thisWidget.height = (int)(height + 0.5f);
        }
        Transform thisTrans = gameObject.transform;

        thisTrans.localPosition = pos;
    }
コード例 #3
0
    // 将锚点设置到相对于父节点的中心,并且各边界距离父节点对应边界占的比例固定,但是如果父节点为空,则只能固定大小
    protected void setToNearParentCenterScaleSize()
    {
        UIRect parentRect = WidgetUtility.findParentRect(gameObject);

        if (parentRect == null)
        {
            Vector3[] sides = getSides(null);
            for (int i = 0; i < 4; ++i)
            {
                mAnchorPoint[i].setRelative(0.0f);
                if (i == 0 || i == 2)
                {
                    mAnchorPoint[i].setAbsolute(sides[i].x);
                }
                else
                {
                    mAnchorPoint[i].setAbsolute(sides[i].y);
                }
            }
        }
        else
        {
            GameObject parent      = parentRect.gameObject;
            Vector3[]  sides       = getSides(parent);
            Vector3[]  parentSides = WidgetUtility.getParentSides(parent);
            for (int i = 0; i < 4; ++i)
            {
                mAnchorPoint[i].setAbsolute(0.0f);
                if (i == 0 || i == 2)
                {
                    mAnchorPoint[i].setRelative(sides[i].x / parentSides[2].x);
                }
                else
                {
                    mAnchorPoint[i].setRelative(sides[i].x / parentSides[1].y);
                }
            }
        }
    }
コード例 #4
0
    public void updateRect(bool force = false)
    {
        if (!force && !mDirty)
        {
            return;
        }
        // 如果窗口带缩放,则可能适配不正确
        if (!MathUtility.isVectorZero(transform.localScale - Vector3.one))
        {
            UnityUtility.logWarning("transform's scale is not 1, may not adapt correctely, " + transform.name + ", scale:" + StringUtility.vector3ToString(transform.localScale, 6));
        }
        mDirty = false;
        Vector2    newSize    = GetComponent <RectTransform>().rect.size;
        GameObject parent     = transform.parent.gameObject;
        Vector2    parentSize = parent.GetComponent <RectTransform>().rect.size;
        Vector3    pos        = transform.localPosition;

        if (parent != null)
        {
            WidgetUtility.getParentSides(parent, mParentSides);
            // 仅仅停靠到父节点的某条边,只需要根据当前大小和父节点大小计算位置
            if (mAnchorMode == ANCHOR_MODE.PADDING_PARENT_SIDE)
            {
                // 横向位置
                if (mHorizontalNearSide == HORIZONTAL_PADDING.LEFT)
                {
                    pos.x = mDistanceToBoard[0].mRelative * mParentSides[0].x + mDistanceToBoard[0].mAbsolute + newSize.x * 0.5f;
                }
                else if (mHorizontalNearSide == HORIZONTAL_PADDING.RIGHT)
                {
                    pos.x = mDistanceToBoard[2].mRelative * mParentSides[2].x + mDistanceToBoard[2].mAbsolute - newSize.x * 0.5f;
                }
                else if (mHorizontalNearSide == HORIZONTAL_PADDING.CENTER)
                {
                    pos.x = mHorizontalPositionRelative * parentSize.x * 0.5f + mHorizontalPositionAbsolute;
                }
                // 纵向位置
                if (mVerticalNearSide == VERTICAL_PADDING.TOP)
                {
                    pos.y = mDistanceToBoard[1].mRelative * mParentSides[1].y + mDistanceToBoard[1].mAbsolute - newSize.y * 0.5f;
                }
                else if (mVerticalNearSide == VERTICAL_PADDING.BOTTOM)
                {
                    pos.y = mDistanceToBoard[3].mRelative * mParentSides[3].y + mDistanceToBoard[3].mAbsolute + newSize.y * 0.5f;
                }
                else if (mVerticalNearSide == VERTICAL_PADDING.CENTER)
                {
                    pos.y = mVerticalPositionRelative * parentSize.y * 0.5f + mVerticalPositionAbsolute;
                }
            }
            // 根据锚点和父节点大小计算各条边的值
            else if (mAnchorMode != ANCHOR_MODE.NONE)
            {
                float thisLeft   = mAnchorPoint[0].mRelative * mParentSides[0].x + mAnchorPoint[0].mAbsolute;
                float thisRight  = mAnchorPoint[2].mRelative * mParentSides[2].x + mAnchorPoint[2].mAbsolute;
                float thisTop    = mAnchorPoint[1].mRelative * mParentSides[1].y + mAnchorPoint[1].mAbsolute;
                float thisBottom = mAnchorPoint[3].mRelative * mParentSides[3].y + mAnchorPoint[3].mAbsolute;
                newSize.x = thisRight - thisLeft;
                newSize.y = thisTop - thisBottom;
                pos.x     = (thisRight + thisLeft) * 0.5f;
                pos.y     = (thisTop + thisBottom) * 0.5f;
            }
        }
        if (newSize.x < 0)
        {
            UnityUtility.logError("width:" + newSize.x + " is not valid, consider to modify the PaddingAnchor! " + gameObject.name + ", parent:" + gameObject.transform.parent.name);
        }
        if (newSize.y < 0)
        {
            UnityUtility.logError("height:" + newSize.y + " is not valid, consider to modify the PaddingAnchor! " + gameObject.name + ", parent:" + gameObject.transform.parent.name);
        }
        WidgetUtility.setRectSize(GetComponent <RectTransform>(), newSize, mAdjustFont);
        transform.localPosition = MathUtility.round(pos);
    }
コード例 #5
0
    // 停靠父节点的指定边界,并且大小不改变
    protected void setToPaddingParentSide(HORIZONTAL_PADDING horizontalSide, VERTICAL_PADDING verticalSide, bool relativeDistance)
    {
        Vector3[]  sides      = null;
        Vector2    pos        = transform.localPosition;
        GameObject parent     = transform.parent.gameObject;
        Vector2    parentSize = parent.GetComponent <RectTransform>().rect.size;

        if (parent != null)
        {
            sides = getSides(parent);
            WidgetUtility.getParentSides(parent, mParentSides);
        }
        int count = mDistanceToBoard.Length;

        for (int i = 0; i < count; ++i)
        {
            mDistanceToBoard[i].setRelative(0.0f);
            mDistanceToBoard[i].setAbsolute(0.0f);
        }
        // 相对于左右边界
        if (horizontalSide == HORIZONTAL_PADDING.LEFT)
        {
            if (relativeDistance)
            {
                mDistanceToBoard[0].mRelative = Mathf.Abs(sides[0].x / mParentSides[0].x);
                mDistanceToBoard[0].setAbsolute(0.0f);
            }
            else
            {
                mDistanceToBoard[0].mRelative = 1.0f;
                mDistanceToBoard[0].setAbsolute(sides[0].x - mParentSides[0].x);
            }
        }
        else if (horizontalSide == HORIZONTAL_PADDING.RIGHT)
        {
            if (relativeDistance)
            {
                mDistanceToBoard[2].mRelative = Mathf.Abs(sides[2].x / mParentSides[2].x);
                mDistanceToBoard[2].setAbsolute(0.0f);
            }
            else
            {
                mDistanceToBoard[2].mRelative = 1.0f;
                mDistanceToBoard[2].setAbsolute(sides[2].x - mParentSides[2].x);
            }
        }
        else if (horizontalSide == HORIZONTAL_PADDING.CENTER)
        {
            if (relativeDistance)
            {
                mHorizontalPositionRelative = pos.x / (parentSize.x * 0.5f);
                mHorizontalPositionAbsolute = 0;
            }
            else
            {
                mHorizontalPositionRelative = 0.0f;
                mHorizontalPositionAbsolute = (int)(pos.x + 0.5f * Mathf.Sign(pos.x));
            }
        }
        if (verticalSide == VERTICAL_PADDING.TOP)
        {
            if (relativeDistance)
            {
                mDistanceToBoard[1].mRelative = Mathf.Abs(sides[1].y / mParentSides[1].y);
                mDistanceToBoard[1].setAbsolute(0.0f);
            }
            else
            {
                mDistanceToBoard[1].mRelative = 1.0f;
                mDistanceToBoard[1].setAbsolute(sides[1].y - mParentSides[1].y);
            }
        }
        else if (verticalSide == VERTICAL_PADDING.BOTTOM)
        {
            if (relativeDistance)
            {
                mDistanceToBoard[3].mRelative = Mathf.Abs(sides[3].y / mParentSides[3].y);
                mDistanceToBoard[3].setAbsolute(0.0f);
            }
            else
            {
                mDistanceToBoard[3].mRelative = 1.0f;
                mDistanceToBoard[3].setAbsolute(sides[3].y - mParentSides[3].y);
            }
        }
        else if (verticalSide == VERTICAL_PADDING.CENTER)
        {
            if (relativeDistance)
            {
                mVerticalPositionRelative = pos.y / (parentSize.y * 0.5f);
                mVerticalPositionAbsolute = 0;
            }
            else
            {
                mVerticalPositionRelative = 0.0f;
                mVerticalPositionAbsolute = (int)(pos.y + 0.5f * Mathf.Sign(pos.y));
            }
        }
        for (int i = 0; i < 4; ++i)
        {
            mAnchorPoint[i].setRelative(0.0f);
            mAnchorPoint[i].setAbsolute(0.0f);
        }
    }
コード例 #6
0
    //------------------------------------------------------------------------------------------------------------------------------------------------
    // 将锚点设置到距离相对于父节点最近的边,并且各边界到父节点对应边界的距离固定不变
    protected void setToNearParentSides(bool relative)
    {
        GameObject parent = transform.parent.gameObject;

        if (parent == null)
        {
            return;
        }
        Vector3[] sides = getSides(parent);
        WidgetUtility.getParentSides(parent, mParentSides);
        for (int i = 0; i < 4; ++i)
        {
            if (i == 0 || i == 2)
            {
                float relativeLeft   = sides[i].x - mParentSides[0].x;
                float relativeCenter = sides[i].x;
                float relativeRight  = sides[i].x - mParentSides[2].x;
                float disToLeft      = Mathf.Abs(relativeLeft);
                float disToCenter    = Mathf.Abs(relativeCenter);
                float disToRight     = Mathf.Abs(relativeRight);
                if (relative)
                {
                    mAnchorPoint[i].setRelative(sides[i].x / mParentSides[i].x);
                    mAnchorPoint[i].setAbsolute(0.0f);
                }
                else
                {
                    // 靠近左边
                    if (disToLeft < disToCenter && disToLeft < disToRight)
                    {
                        mAnchorPoint[i].setRelative(Mathf.Sign(sides[i].x) * Mathf.Sign(mParentSides[i].x));
                        mAnchorPoint[i].setAbsolute(relativeLeft);
                    }
                    // 靠近右边
                    else if (disToRight < disToLeft && disToRight < disToCenter)
                    {
                        mAnchorPoint[i].setRelative(Mathf.Sign(sides[i].x) * Mathf.Sign(mParentSides[i].x));
                        mAnchorPoint[i].setAbsolute(relativeRight);
                    }
                    // 靠近中心
                    else
                    {
                        mAnchorPoint[i].setRelative(0.0f);
                        mAnchorPoint[i].setAbsolute(relativeCenter);
                    }
                }
            }
            else if (i == 1 || i == 3)
            {
                float relativeTop    = sides[i].y - mParentSides[1].y;
                float relativeCenter = sides[i].y;
                float relativeBottom = sides[i].y - mParentSides[3].y;
                float disToTop       = Mathf.Abs(relativeTop);
                float disToCenter    = Mathf.Abs(relativeCenter);
                float disToBottom    = Mathf.Abs(relativeBottom);
                if (relative)
                {
                    mAnchorPoint[i].setRelative(sides[i].y / mParentSides[i].y);
                    mAnchorPoint[i].setAbsolute(0.0f);
                }
                else
                {
                    // 靠近顶部
                    if (disToTop < disToCenter && disToTop < disToBottom)
                    {
                        mAnchorPoint[i].setRelative(Mathf.Sign(sides[i].y) * Mathf.Sign(mParentSides[i].y));
                        mAnchorPoint[i].setAbsolute(relativeTop);
                    }
                    // 靠近底部
                    else if (disToBottom < disToTop && disToBottom < disToCenter)
                    {
                        mAnchorPoint[i].setRelative(Mathf.Sign(sides[i].y) * Mathf.Sign(mParentSides[i].y));
                        mAnchorPoint[i].setAbsolute(relativeBottom);
                    }
                    // 靠近中心
                    else
                    {
                        mAnchorPoint[i].setRelative(0.0f);
                        mAnchorPoint[i].setAbsolute(relativeCenter);
                    }
                }
            }
        }
    }
コード例 #7
0
    // 停靠父节点的指定边界,并且大小不改变,0,1,2,3表示左上右下
    protected void setToPaddingParentSide(NEAR_SIDE side)
    {
        UIRect parentRect = WidgetUtility.findParentRect(gameObject);

        if (parentRect == null)
        {
            Vector3[] sides = getSides(null);
            for (int i = 0; i < 4; ++i)
            {
                mAnchorPoint[i].setRelative(0.0f);
                if (i == 0 || i == 2)
                {
                    mAnchorPoint[i].setAbsolute(sides[i].x);
                }
                else
                {
                    mAnchorPoint[i].setAbsolute(sides[i].y);
                }
            }
        }
        else
        {
            GameObject parent      = parentRect.gameObject;
            Vector3[]  sides       = getSides(parent);
            Vector3[]  parentSides = WidgetUtility.getParentSides(parent);
            // 相对于左右边界
            if (side == NEAR_SIDE.NS_LEFT || side == NEAR_SIDE.NS_RIGHT)
            {
                for (int i = 0; i < 4; ++i)
                {
                    if (i == 0 || i == 2)
                    {
                        mAnchorPoint[i].setRelative((side == NEAR_SIDE.NS_LEFT) ? -1.0f : 1.0f);
                        mAnchorPoint[i].setAbsolute(sides[i].x - parentSides[(int)side].x);
                    }
                    else
                    {
                        mAnchorPoint[i].setRelative(0.0f);
                        mAnchorPoint[i].setAbsolute(sides[i].y);
                    }
                }
            }
            // 相对于上下边界
            else if (side == NEAR_SIDE.NS_TOP || side == NEAR_SIDE.NS_BOTTOM)
            {
                for (int i = 0; i < 4; ++i)
                {
                    if (i == 0 || i == 2)
                    {
                        mAnchorPoint[i].setRelative(0.0f);
                        mAnchorPoint[i].setAbsolute(sides[i].x);
                    }
                    else
                    {
                        mAnchorPoint[i].setRelative((side == NEAR_SIDE.NS_TOP) ? 1.0f : -1.0f);
                        mAnchorPoint[i].setAbsolute(sides[i].y - parentSides[(int)side].y);
                    }
                }
            }
        }
    }
コード例 #8
0
    //------------------------------------------------------------------------------------------------------------------------------------------------
    // 将锚点设置到距离相对于父节点最近的边,并且各边界到父节点对应边界的距离固定不变
    protected void setToNearParentSides()
    {
        UIRect parentRect = WidgetUtility.findParentRect(gameObject);

        if (parentRect == null)
        {
            Vector3[] sides = getSides(null);
            for (int i = 0; i < 4; ++i)
            {
                mAnchorPoint[i].setRelative(0.0f);
                if (i == 0 || i == 2)
                {
                    mAnchorPoint[i].setAbsolute(MathUtility.getLength(sides[i]));
                }
                else if (i == 1 || i == 3)
                {
                    mAnchorPoint[i].setAbsolute(MathUtility.getLength(sides[i]));
                }
            }
            return;
        }
        else
        {
            GameObject parent      = parentRect.gameObject;
            Vector3[]  sides       = getSides(parent);
            Vector3[]  parentSides = WidgetUtility.getParentSides(parent);
            for (int i = 0; i < 4; ++i)
            {
                if (i == 0 || i == 2)
                {
                    float relativeLeft   = sides[i].x - parentSides[0].x;
                    float relativeCenter = sides[i].x;
                    float relativeRight  = sides[i].x - parentSides[2].x;
                    float disToLeft      = Mathf.Abs(relativeLeft);
                    float disToCenter    = Mathf.Abs(relativeCenter);
                    float disToRight     = Mathf.Abs(relativeRight);
                    // 靠近左边
                    if (disToLeft < disToCenter && disToLeft < disToRight)
                    {
                        mAnchorPoint[i].setRelative(-1.0f);
                        mAnchorPoint[i].setAbsolute(relativeLeft);
                    }
                    // 靠近右边
                    else if (disToRight < disToLeft && disToRight < disToCenter)
                    {
                        mAnchorPoint[i].setRelative(1.0f);
                        mAnchorPoint[i].setAbsolute(relativeRight);
                    }
                    // 靠近中心
                    else
                    {
                        mAnchorPoint[i].setRelative(0.0f);
                        mAnchorPoint[i].setAbsolute(relativeCenter);
                    }
                }
                else if (i == 1 || i == 3)
                {
                    float relativeTop    = sides[i].y - parentSides[1].y;
                    float relativeCenter = sides[i].y;
                    float relativeBottom = sides[i].y - parentSides[3].y;
                    float disToTop       = Mathf.Abs(relativeTop);
                    float disToCenter    = Mathf.Abs(relativeCenter);
                    float disToBottom    = Mathf.Abs(relativeBottom);
                    // 靠近顶部
                    if (disToTop < disToCenter && disToTop < disToBottom)
                    {
                        mAnchorPoint[i].setRelative(1.0f);
                        mAnchorPoint[i].setAbsolute(relativeTop);
                    }
                    // 靠近底部
                    else if (disToBottom < disToTop && disToBottom < disToCenter)
                    {
                        mAnchorPoint[i].setRelative(-1.0f);
                        mAnchorPoint[i].setAbsolute(relativeBottom);
                    }
                    // 靠近中心
                    else
                    {
                        mAnchorPoint[i].setRelative(0.0f);
                        mAnchorPoint[i].setAbsolute(relativeCenter);
                    }
                }
            }
        }
    }