Example #1
0
        public void UpdateSize()
        {
            // TODO: must be remove after whole components serialize it
            if (this.isFirst)
            {
                this.isFirst = false;
                this.ResetOriginOffsets();
            }

            var position = this.originPosition;
            var size     = this.originSize;

            var scaledScreenSize = Responsive.GetAreaSizeByMode(AreaMode.Screen);

            var baseArea        = Responsive.GetAreaRectByMode(AreaMode.Safe);
            var baseContentSize = Responsive.GetAreaSizeByMode(AreaMode.Viewport);

            var contentScale = UnityExtension.GetScaleOfAreaSize(baseArea.size, baseContentSize);
            var scaleFactor  = 1f / contentScale;

            var baseAreaRect = baseArea;
            var baseRectSize = new Vector2(baseContentSize.x * contentScale,
                                           baseContentSize.y * contentScale);
            var baseAreaRectSize = new Vector2(baseAreaRect.size.x, baseAreaRect.size.y);

            var actualContentSize  = baseRectSize;
            var actualSafeAreaSize = baseAreaRectSize;

            var areaPosition           = baseArea.position;
            var halfArea               = scaledScreenSize * 0.5f;
            var halfActualContentSize  = actualContentSize * 0.5f;
            var halfActualSafeAreaSize = actualSafeAreaSize * 0.5f;

            this.rightBlankValue  = Mathf.CeilToInt(scaledScreenSize.x - (halfArea.x + (this.ignoreContentTrasnformRight ? halfActualSafeAreaSize.x : halfActualContentSize.x)) - areaPosition.x);
            this.leftBlankValue   = Mathf.CeilToInt(halfArea.x - (this.ignoreContentTrasnformLeft ? halfActualSafeAreaSize.x : halfActualContentSize.x) + areaPosition.x);
            this.topBlankValue    = Mathf.CeilToInt(scaledScreenSize.y - (halfArea.y + (this.ignoreContentTrasnformTop ? halfActualSafeAreaSize.y : halfActualContentSize.y)) - areaPosition.y);
            this.bottomBlankValue = Mathf.CeilToInt(halfArea.y - (this.ignoreContentTrasnformBottom ? halfActualSafeAreaSize.y : halfActualContentSize.y) + areaPosition.y);

            var leftSize   = this.normalizedOffset.left * this.leftBlankValue;
            var rightSize  = this.normalizedOffset.right * this.rightBlankValue;
            var topSize    = this.normalizedOffset.top * this.topBlankValue;
            var bottomSize = this.normalizedOffset.bottom * this.bottomBlankValue;


            this.resultLeft   = leftSize * scaleFactor;
            this.resultRight  = rightSize * scaleFactor;
            this.resultTop    = topSize * scaleFactor;
            this.resultBottom = bottomSize * scaleFactor;

            if (this.useSizeLimit)
            {
                var blankMin = (this.minSize - size) * 0.5f;
                var blankMax = (this.maxSize - size) * 0.5f;

                this.resultLeft   = Mathf.Clamp(resultLeft, blankMin.x, blankMax.x);
                this.resultRight  = Mathf.Clamp(resultRight, blankMin.x, blankMax.x);
                this.resultTop    = Mathf.Clamp(resultTop, blankMin.y, blankMax.y);
                this.resultBottom = Mathf.Clamp(resultBottom, blankMin.y, blankMax.y);
            }

            var trans = this.CachedRectTransform;

            //TODO : Remove
#if LOG_DEBUG
            if (null == trans)
            {
                Debug.LogError("rt null");
            }
#endif// LOG_DEBUG

            this.SetSizeWithEdge(trans, RectTransform.Edge.Left, this.resultLeft);
            this.SetSizeWithEdge(trans, RectTransform.Edge.Right, this.resultRight);
            this.SetSizeWithEdge(trans, RectTransform.Edge.Top, this.resultTop);
            this.SetSizeWithEdge(trans, RectTransform.Edge.Bottom, this.resultBottom);

#if LOG_DEBUG
            if (this.calibrateCenter > CalibrateCenter.Both)
            {
                Debug.LogError("Invalid CalibrateCenter");
            }
#endif// LOG_DEBUG

            var anchorMax = trans.anchorMax;
            var anchorMin = trans.anchorMin;
            switch (this.calibrateCenter)
            {
            case CalibrateCenter.Horizontal:
                this.CalibratePositionX(trans, areaPosition, position, anchorMax, anchorMin, scaleFactor);
                break;

            case CalibrateCenter.Vertical:
                this.CalibratePositionY(trans, areaPosition, position, anchorMax, anchorMin, scaleFactor);
                break;

            case CalibrateCenter.Both:
                this.CalibratePositionXY(trans, areaPosition, position, anchorMax, anchorMin, scaleFactor);
                break;
            }
        }
Example #2
0
        public static Vector2 GetAreaSizeByMode(AreaMode mode)
        {
            var areaRect = Responsive.GetAreaRectByMode(mode);

            return(areaRect.size);
        }