Exemple #1
0
    public void updateRect(bool force = false)
    {
        // 是否为编辑器手动预览操作,手动预览不需要启动游戏
#if UNITY_EDITOR
        bool preview = !EditorApplication.isPlaying;
#else
        bool preview = false;
#endif
        // 如果是第一次更新,则需要获取原始属性
        if (mFirstUpdate || preview)
        {
            mScreenScale = UnityUtility.getScreenScale(UnityUtility.getRootSize(preview));
            mOriginSize  = WidgetUtility.getRectSize(GetComponent <RectTransform>());
            mOriginPos   = transform.localPosition;
            mFirstUpdate = false;
        }
        if (!preview && !force && !mDirty)
        {
            return;
        }
        mDirty = false;
        Vector3 realScale  = UnityUtility.adjustScreenScale(mScreenScale, mKeepAspect ? mAspectBase : ASPECT_BASE.NONE);
        float   thisWidth  = mOriginSize.x * realScale.x;
        float   thisHeight = mOriginSize.y * realScale.y;
        MathUtility.checkInt(ref thisWidth, 0.001f);
        MathUtility.checkInt(ref thisHeight, 0.001f);
        Vector2 newSize = new Vector2(thisWidth, thisHeight);
        // 只有在刷新时才能确定父节点,所以父节点需要实时获取
        WidgetUtility.setRectSize(GetComponent <RectTransform>(), newSize, mAdjustFont);
        transform.localPosition = MathUtility.round(MathUtility.multiVector3(mOriginPos, realScale));
    }
Exemple #2
0
    public void init(int renderOrder)
    {
        mScript = mLayoutManager.createScript(this);
        int count = mLayoutScriptCallback.Count;

        for (int i = 0; i < count; ++i)
        {
            mLayoutScriptCallback[i].Invoke(mScript, true);
        }
        if (mScript == null)
        {
            logError("can not create layout script! id : " + mID);
            return;
        }
        myUIObject parent = mIsScene ? null : mLayoutManager.getUIRoot();

        // 初始化布局脚本
        mScript.newObject(out mRoot, parent, mName);

        if (mRoot != null)
        {
            // 去除自带的锚点
            // 在unity2020中,不知道为什么实例化以后的RectTransform的大小会自动变为窗口大小,为了适配计算正确,这里需要重置一次
            RectTransform rectTransform = mRoot.getRectTransform();
            Vector3       size          = WidgetUtility.getRectSize(rectTransform);
            rectTransform.anchorMin = Vector2.one * 0.5f;
            rectTransform.anchorMax = Vector2.one * 0.5f;
            WidgetUtility.setRectSize(rectTransform, new Vector2(GameDefine.STANDARD_WIDTH, GameDefine.STANDARD_HEIGHT), false);
        }

        mRoot.setDestroyImmediately(true);
        mDefaultLayer = mRoot.getObject().layer;
        mScript.setRoot(mRoot);
        mScript.assignWindow();
        // assignWindow后设置布局的渲染顺序,这样可以在此处刷新所有窗口的深度
        setRenderOrder(renderOrder);
        // 布局实例化完成,初始化之前,需要调用自适应组件的更新
        if (mLayoutManager.isUseAnchor())
        {
            applyAnchor(mRoot.getObject(), true, this);
        }
        mAnchorApplied = true;
        mScript.init();
        mScriptInited = true;
        // 加载完布局后强制隐藏
        setVisibleForce(false);
#if UNITY_EDITOR
        mRoot.getUnityComponent <LayoutDebug>().setLayout(this);
#endif
    }
    //------------------------------------------------------------------------------------------------------------------------------
    protected static void roundRectTransformToInt(RectTransform rectTransform)
    {
        if (rectTransform == null)
        {
            return;
        }
        rectTransform.localPosition = round(rectTransform.localPosition);
        WidgetUtility.setRectSize(rectTransform, round(WidgetUtility.getRectSize(rectTransform)), false);
        int childCount = rectTransform.childCount;

        for (int i = 0; i < childCount; ++i)
        {
            roundRectTransformToInt(rectTransform.GetChild(i) as RectTransform);
        }
    }
    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);
    }
 public override void setWindowSize(Vector2 size)
 {
     WidgetUtility.setRectSize(mRectTransform, size, false);
 }