Exemplo n.º 1
0
        public static void CalcPanelNDCRect(RectTransform rc, OffsetCalcMode modeX, OffsetCalcMode modeY, ref Rect outRect)
        {
            if (rc == null)
            {
                Debug.LogError("RecTransform component required to calculate NDC rect.");
                return;
            }

            outRect.Set(0.0f, 0.0f, 0.0f, 0.0f);

            RectTransform rcParent = rc.parent != null?rc.parent.GetComponent <RectTransform> () : null;

            if (rc == null || rcParent == null)
            {
                Debug.LogError("Can not run transition on component with no RectTransform or parent object.");
                return;
            }

            Vector2 dim = Vector2.zero;

            Utils.CalcRectTransformDimensions(rc, ref dim);
            Vector2 parentDim = Vector2.zero;

            Utils.CalcRectTransformDimensions(rcParent, ref parentDim);

            float normalizedMarginX = CalcNDCMargin(modeX, dim.x, parentDim.x);             // modeX == OffsetCalcMode.Centered ? (1.0f-(dim.x/parentDim.x))*0.5f : 1.0f;
            float offsetMinX        = (normalizedMarginX - 0 /*rc.anchorMin.x*/) * dim.x;
            float offsetMaxX        = (normalizedMarginX - 1 /*rc.anchorMax.x*/) * dim.x + dim.x;

            float normalizedMarginY = CalcNDCMargin(modeY, dim.y, parentDim.y);             //modeY == OffsetCalcMode.Centered ? (1.0f-(dim.y/parentDim.y))*0.5f : 1.0f;
            float offsetMinY        = (normalizedMarginY - 0 /*rc.anchorMin.y*/) * dim.y;
            float offsetMaxY        = (normalizedMarginY - 1 /*rc.anchorMax.y*/) * dim.y + dim.y;

            outRect.xMin = offsetMinX;
            outRect.yMin = offsetMinY;
            outRect.xMax = offsetMaxX;
            outRect.yMax = offsetMaxY;
        }
Exemplo n.º 2
0
        private static float CalcNDCMargin(OffsetCalcMode mode, float dim, float parentDim)
        {
            dim = parentDim = 1;
            float margin = 0.0f;

            switch (mode)
            {
            case OffsetCalcMode.Centered:
                margin = (1.0f - (dim / parentDim)) * 0.5f;
                break;

            case OffsetCalcMode.ScreenRight:
            case OffsetCalcMode.ScreenTop:
                margin = 1.0f;
                break;

            case OffsetCalcMode.ScreenLeft:
            case OffsetCalcMode.ScreenBottom:
                margin = -1.0f;
                break;
            }

            return(margin);
        }