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; }
public void Awake() { if (GameBase.mLayoutManager != null && mRoot == null) { mRoot = GameBase.mLayoutManager.getNGUIRootObject().GetComponent <UIRoot>(); UIWidget widget = WidgetUtility.getGameObjectWidget(gameObject); if (widget != null) { widget.keepAspectRatio = UIWidget.AspectRatioSource.Free; } mHorizontalScale = mRoot.activeHeight * GameBase.mCameraManager.getUICamera().getCamera().aspect / (float)CommonDefine.STANDARD_WIDTH; mVerticalScale = mRoot.activeHeight / (float)CommonDefine.STANDARD_HEIGHT; } // 由于初始位置是在Awake中记录的,所以在动态实例化预设后挂接到父节点下时,坐标必须为0,也就是与预设初始状态保持一致 mOriginPos = transform.localPosition; UIWidget thisWidget = WidgetUtility.getGameObjectWidget(gameObject); if (thisWidget != null) { mOriginWidth = thisWidget.width; mOriginHeight = thisWidget.height; } }
//------------------------------------------------------------------------------------------------------------------------------------------------ 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 = WidgetUtility.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 = WidgetUtility.findParentRect(gameObject); Vector2 parentSize = WidgetUtility.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; } }