Exemplo n.º 1
0
    static void Calculation()
    {
        if (Selection.activeGameObject == null)
        {
            return;
        }
        ScaleAnchor anchor = Selection.activeGameObject.GetComponent <ScaleAnchor>();

        if (anchor == null)
        {
            return;
        }
        var     obj     = Selection.activeGameObject;
        Vector2 thisPos = obj.transform.localPosition;
        // 获取父物体的属性
        UIRect  parentRect = CustomAnchor.findParentRect(obj);
        Vector2 parentSize = CustomAnchor.getRectSize(parentRect);

        // 计算
        anchor.mHorizontalRelativePos = thisPos.x / parentSize.x * 2;
        anchor.mVerticalRelativePos   = thisPos.y / parentSize.y * 2;
        // 设置成自定义方式
        anchor.mPadding = PADDING_STYLE.PS_CUSTOM_VALUE;
    }
Exemplo n.º 2
0
    //------------------------------------------------------------------------------------------------------------------------------------------------
    protected void updateRect(bool force = false)
    {
        if (mRoot == null || !force && !mDirty)
        {
            return;
        }
        mDirty       = false;
        mWidthScale  = mHorizontalScale;
        mHeightScale = mVerticalScale;
        if (mKeepAspect)
        {
            if (mAspectBase == ASPECT_BASE.AB_USE_HEIGHT_SCALE)
            {
                mWidthScale = mVerticalScale;
            }
            else if (mAspectBase == ASPECT_BASE.AB_USE_WIDTH_SCALE)
            {
                mHeightScale = mHorizontalScale;
            }
        }
        float    thisWidth  = 0.0f;
        float    thisHeight = 0.0f;
        UIWidget thisWidget = CustomAnchor.getGameObjectWidget(gameObject);

        if (thisWidget != null)
        {
            thisWidth         = mOriginWidth * mWidthScale;
            thisHeight        = mOriginHeight * mHeightScale;
            thisWidget.width  = (int)(thisWidth + 0.5f);
            thisWidget.height = (int)(thisHeight + 0.5f);
        }
        if (mPadding == PADDING_STYLE.PS_NONE)
        {
            mPosXScale = mWidthScale;
            mPosYScale = mHeightScale;
            gameObject.transform.localPosition = new Vector3(mOriginPos.x * mPosXScale, mOriginPos.y * mPosYScale, mOriginPos.z);
        }
        else
        {
            // 只有在刷新时才能确定父节点,所以父节点需要实时获取
            UIRect  parentRect = CustomAnchor.findParentRect(gameObject);
            Vector2 parentSize = CustomAnchor.getRectSize(parentRect);
            // hori为-1表示窗口坐标在父窗口的左侧边界上,为1表示在右侧边界上
            if (mPadding == PADDING_STYLE.PS_LEFT || mPadding == PADDING_STYLE.PS_LEFT_BOTTOM || mPadding == PADDING_STYLE.PS_LEFT_TOP)
            {
                mHorizontalRelativePos = -(parentSize.x - thisWidth) / (parentSize.x);
            }
            else if (mPadding == PADDING_STYLE.PS_RIGHT || mPadding == PADDING_STYLE.PS_RIGHT_BOTTOM || mPadding == PADDING_STYLE.PS_RIGHT_TOP)
            {
                mHorizontalRelativePos = (parentSize.x - thisWidth) / (parentSize.x);
            }
            else if (mPadding != PADDING_STYLE.PS_CUSTOM_VALUE)
            {
                mHorizontalRelativePos = mOriginPos.x / (parentSize.x / 2.0f);
            }
            if (mPadding == PADDING_STYLE.PS_TOP || mPadding == PADDING_STYLE.PS_LEFT_TOP || mPadding == PADDING_STYLE.PS_RIGHT_TOP)
            {
                mVerticalRelativePos = (parentSize.y - thisHeight) / (parentSize.y);
            }
            else if (mPadding == PADDING_STYLE.PS_BOTTOM || mPadding == PADDING_STYLE.PS_LEFT_BOTTOM || mPadding == PADDING_STYLE.PS_RIGHT_BOTTOM)
            {
                mVerticalRelativePos = -(parentSize.y - thisHeight) / (parentSize.y);
            }
            else if (mPadding != PADDING_STYLE.PS_CUSTOM_VALUE)
            {
                mVerticalRelativePos = mOriginPos.y / (parentSize.y / 2.0f);
            }
            Vector3 pos = mOriginPos;
            pos.x = mHorizontalRelativePos * (parentSize.x / 2.0f);
            pos.y = mVerticalRelativePos * (parentSize.y / 2.0f);
            gameObject.transform.localPosition = pos;
        }
        // 如果有UIGrid组件,则需要调整UIGrid中的排列间隔
        UIGrid grid = gameObject.GetComponent <UIGrid>();

        if (gameObject.GetComponent <UIGrid>() != null)
        {
            grid.cellHeight *= mHeightScale;
            grid.cellWidth  *= mWidthScale;
        }
    }