private void UpdateLayout(int axis)
        {
            // Verify and prepare horizontal axis.
            if (axis == 0)
            {
                if (!snapWidth)
                {
                    return;
                }
                m_tracker.Add(this, rectTransform, DrivenTransformProperties.SizeDeltaX |
                              DrivenTransformProperties.AnchorMinX |
                              DrivenTransformProperties.AnchorMaxX |
                              DrivenTransformProperties.AnchoredPositionX);
                rectTransform.SetAnchorMinX(rectTransform.GetPivotX());
                rectTransform.SetAnchorMaxX(rectTransform.GetPivotX());
                rectTransform.SetAnchoredPositionX(0f);
            }

            // Verify and prepare vertical axis.
            if (axis == 1)
            {
                if (!snapHeight)
                {
                    return;
                }
                m_tracker.Add(this, rectTransform, DrivenTransformProperties.SizeDeltaY |
                              DrivenTransformProperties.AnchorMinY |
                              DrivenTransformProperties.AnchorMaxY |
                              DrivenTransformProperties.AnchoredPositionY);
                rectTransform.SetAnchorMinY(rectTransform.GetPivotY());
                rectTransform.SetAnchorMaxY(rectTransform.GetPivotY());
                rectTransform.SetAnchoredPositionY(0f);
            }

            // Compute the sizes to be working with.
            Vector2 sizeDelta = rectTransform.sizeDelta;
            Mode    mode      = (axis == 0) ? widthMode : heightMode;

            float targetAbsolute = (axis == 0) ? targetWidthAbsolute : targetHeightAbsolute;
            float targetRelative = (axis == 0) ? targetWidthRelative : targetHeightRelative;

            float snapAtAbsolute = (axis == 0) ? snapAtWidthAbsolute : snapAtHeightAbsolute;
            float snapAtRelative = (axis == 0) ? snapAtWidthRelative : snapAtHeightRelative;

            if (mode == Mode.Absolute)
            {
                if (targetAbsolute > snapAtRelative * parentRectTransform.rect.size[axis])
                {
                    sizeDelta[axis] = parentRectTransform.rect.size[axis];
                }
                else
                {
                    sizeDelta[axis] = targetAbsolute;
                }
            }
            else if (mode == Mode.Relative)
            {
                if (targetRelative * parentRectTransform.rect.size[axis] > snapAtAbsolute)
                {
                    sizeDelta[axis] = parentRectTransform.rect.size[axis];
                }
                else
                {
                    sizeDelta[axis] = targetRelative * parentRectTransform.rect.size[axis];
                }
            }

            // Set the size delta.
            rectTransform.sizeDelta = sizeDelta;
        }
Esempio n. 2
0
        private void UpdateRect()
        {
            if (!IsActive())
            {
                return;
            }

            m_Tracker.Clear();

            switch (m_AspectMode)
            {
#if UNITY_EDITOR
            case AspectMode.None:
            {
                if (!Application.isPlaying)
                {
                    m_AspectRatio = Mathf.Clamp(rectTransform.rect.width / rectTransform.rect.height, 0.001f, 1000f);
                }

                break;
            }
#endif
            case AspectMode.HeightControlsWidth:
            {
                m_Tracker.Add(this, rectTransform, DrivenTransformProperties.SizeDeltaX);
                rectTransform.SetSizeWithCurrentAnchors(RectTransform.Axis.Horizontal, rectTransform.rect.height * m_AspectRatio);
                break;
            }

            case AspectMode.WidthControlsHeight:
            {
                m_Tracker.Add(this, rectTransform, DrivenTransformProperties.SizeDeltaY);
                rectTransform.SetSizeWithCurrentAnchors(RectTransform.Axis.Vertical, rectTransform.rect.width / m_AspectRatio);
                break;
            }

            case AspectMode.FitInParent:
            case AspectMode.EnvelopeParent:
            {
                m_Tracker.Add(this, rectTransform,
                              DrivenTransformProperties.Anchors |
                              DrivenTransformProperties.AnchoredPosition |
                              DrivenTransformProperties.SizeDeltaX |
                              DrivenTransformProperties.SizeDeltaY);

                rectTransform.anchorMin        = Vector2.zero;
                rectTransform.anchorMax        = Vector2.one;
                rectTransform.anchoredPosition = Vector2.zero;

                Vector2 sizeDelta  = Vector2.zero;
                Vector2 parentSize = GetParentSize();
                if ((parentSize.y * aspectRatio < parentSize.x) ^ (m_AspectMode == AspectMode.FitInParent))
                {
                    sizeDelta.y = GetSizeDeltaToProduceSize(parentSize.x / aspectRatio, 1);
                }
                else
                {
                    sizeDelta.x = GetSizeDeltaToProduceSize(parentSize.y * aspectRatio, 0);
                }
                rectTransform.sizeDelta = sizeDelta;

                break;
            }
            }
        }
Esempio n. 3
0
        private void RefreshScale()
        {
            if (scaler == null)             // When instantiating the icon for the first time
            {
                return;
            }

            if (!enabled)
            {
                return;
            }

            if (size == 0 && sizeMode == SizeMode.Manual)
            {
                fontSize = 0;
                return;
            }

            float tempSize = size;

            if (sizeMode == SizeMode.Manual)
            {
                m_ScaledSize = tempSize * scaler.scaleFactor;
            }
            else if (sizeMode == SizeMode.MatchWidth)
            {
                m_ScaledSize  = rectTransform.rect.width;
                tempSize      = m_ScaledSize;
                m_ScaledSize *= scaler.scaleFactor;
            }
            else if (sizeMode == SizeMode.MatchHeight)
            {
                m_ScaledSize  = rectTransform.rect.height;
                tempSize      = m_ScaledSize;
                m_ScaledSize *= scaler.scaleFactor;
            }
            else if (sizeMode == SizeMode.MatchMin)
            {
                Vector2 tempVector2 = new Vector2(rectTransform.rect.width, rectTransform.rect.height);

                m_ScaledSize  = Mathf.Min(tempVector2.x, tempVector2.y);
                tempSize      = m_ScaledSize;
                m_ScaledSize *= scaler.scaleFactor;
            }
            else if (sizeMode == SizeMode.MatchMax)
            {
                Vector2 tempVector2 = new Vector2(rectTransform.rect.width, rectTransform.rect.height);

                m_ScaledSize  = Mathf.Max(tempVector2.x, tempVector2.y);
                tempSize      = m_ScaledSize;
                m_ScaledSize *= scaler.scaleFactor;
            }

            if (m_ScaledSize > 500)
            {
                m_LocalScaleFactor = m_ScaledSize / 500;
            }
            else
            {
                m_LocalScaleFactor = 1f;
            }

            tempSize *= m_LocalScaleFactor;

            fontSize = Mathf.RoundToInt(tempSize);

                        #if UNITY_EDITOR
            if (!Application.isPlaying)
            {
                EditorUtility.SetDirty(this);
            }
                        #endif

            m_LocalScaleFactor *= (size / Mathf.Max(size));

            if (m_LocalScaleFactor != float.NaN && new Vector3(m_LocalScaleFactor, m_LocalScaleFactor, m_LocalScaleFactor) != rectTransform.localScale)
            {
                m_Tracker.Add(this, rectTransform, DrivenTransformProperties.Scale);
                rectTransform.localScale = new Vector3(m_LocalScaleFactor, m_LocalScaleFactor, m_LocalScaleFactor);
            }
        }