예제 #1
0
    private static Rect GetOutsideOfContainerRange(RectTransform tr)
    {
        Vector2 anchorPos        = GetAnchorsPosition(tr);
        Vector2 anchorSize       = GetAnchorsSize(tr);
        Vector2 rectPosAsAnchors = GetAnchorCoordianteFromRectCoordinate(tr, RteRectTools.GetPositionIgnoringAnchorsAndPivot(tr));
        Vector2 rectSizeAsAnchor = GetAnchorCoordianteFromRectCoordinate(tr, RteRectTools.GetSizeIgnoringAnchors(tr));
        Vector2 anchorsOffset    = anchorPos - rectPosAsAnchors;
        Rect    anchorsAsRect    = new Rect(anchorPos, anchorSize);
        Rect    rectAsRect       = new Rect(rectPosAsAnchors, rectSizeAsAnchor);
        Rect    bounds           = anchorsAsRect.CombineWith(rectAsRect);

        if (anchorsOffset.x < 0)
        {
            anchorsOffset.x = 0;
        }
        if (anchorsOffset.y < 0)
        {
            anchorsOffset.y = 0;
        }

        Vector2 minPos = -bounds.size + anchorsOffset;
        Vector2 maxPos = Vector2.one + anchorsOffset;

        return(new Rect(minPos, maxPos));
    }
예제 #2
0
    /// <summary>
    /// With this method you can set the size of the UI object in different kind of coordinate systems. Returns the size as a Vector2.
    /// </summary>
    /// <param name="tr"></param>
    /// <param name="coordinateSystem">The returned size will be from the coordinate system you specify in this parameter.</param>
    public static Vector2 GetSize(this RectTransform tr, CoordinateSystem coordinateSystem = CoordinateSystem.IgnoreAnchorsAndPivot)
    {
        Vector2 result = Vector2.zero;

        switch (coordinateSystem)
        {
        case CoordinateSystem.IgnoreAnchors:
            result = RteRectTools.GetSizeIgnoringAnchors(tr);
            break;

        case CoordinateSystem.IgnoreAnchorsAndPivot:
            result = RteRectTools.GetSizeIgnoringAnchors(tr);
            break;

        case CoordinateSystem.IgnoreAnchorsAndPivotNormalized:
            result = RteRectTools.GetSizeNormalizedIgnoringAnchors(tr);
            break;

        case CoordinateSystem.AsChildOfCanvas:
            result = RteAnchorTools.GetSizeInCanvasAnchorCoordinatesFromRectSize(tr);
            break;

        case CoordinateSystem.AsChildOfCanvasNormalized:
            result = RteAnchorTools.GetSizeNormalizedInCanvasAnchorCoordinatesFromRectSize(tr);
            break;

        case CoordinateSystem.ScreenSpacePixels:
            result = RteAnchorTools.GetScreenSpaceSizeFromRectSize(tr);
            break;
        }

        return(result);
    }
예제 #3
0
    /// <summary>
    /// Resize the anchors, resizing anchors also resizes the image as you may know. This is the same than
    /// moving the anchors holding shift key in the Unity editor.
    /// To set the size of another object you must pass: SetSize(targetTransform.GetSize()) Or for a
    /// more simple sintax use SetSize(targetRectTransform).
    /// The aim of this function is manipulating anchors with only a single Vector2, specially usefull
    /// for tweens and clean code. In most cases it's a good idea to move and resize images using anchors because
    /// everything becomes screen size independent.
    /// </summary>
    /// <param name="tr"></param>
    /// <param name="targetSize"></param>
    public static void SetAnchorsSize(RectTransform tr, Vector2 targetSize, bool centeredPivot = false, bool alsoChangeRect = true)
    {
        Vector2 rectPos  = Vector2.zero;
        Vector2 rectSize = Vector2.zero;

        if (!alsoChangeRect)
        {
            rectPos  = RteRectTools.GetPositionIgnoringAnchorsAndPivot(tr);
            rectSize = RteRectTools.GetSizeIgnoringAnchors(tr);
        }

        if (centeredPivot)
        {
            Vector2 sizeDif     = targetSize - (tr.anchorMax - tr.anchorMin);
            Vector2 sizeDifHalf = new Vector2(sizeDif.x * 0.5f, sizeDif.y * 0.5f);
            tr.anchorMax -= sizeDifHalf;
            tr.anchorMin -= sizeDifHalf;
        }

        tr.anchorMax = tr.anchorMin + targetSize;

        if (!alsoChangeRect)
        {
            RteRectTools.SetSizeIgnoringAnchors(tr, rectSize, centeredPivot);
            RteRectTools.SetPositionIgnoringAnchorsAndPivot(tr, rectPos);
        }
    }
예제 #4
0
    public static Vector2 GetCanvasPositionFromLocalPosition(RectTransform tr, Vector2 pivotPosition)
    {
        Vector2 size     = RteRectTools.GetSizeIgnoringAnchors(tr);
        Vector2 position = RteRectTools.GetPositionIgnoringAnchorsAndPivot(tr);

        size.x *= pivotPosition.x;
        size.y *= pivotPosition.y;
        size   += position;
        return(RteAnchorTools.GetCanvasAnchorCoordinateNormalizedFromRectCoordinate(tr, size));
    }
예제 #5
0
    public static Vector2 GetLocalPositionFromCanvasPosition(RectTransform transform, Vector2 canvasPosition)
    {
        Vector2 rectCoordinate = RteAnchorTools.GetRectCoordinateFromCanvasAnchorCoordinateNormalized(transform, canvasPosition);
        Vector2 position       = RteRectTools.GetPositionIgnoringAnchorsAndPivot(transform);
        Vector2 size           = RteRectTools.GetSizeIgnoringAnchors(transform);

        rectCoordinate   -= position;
        rectCoordinate.x /= size.x;
        rectCoordinate.y /= size.y;
        rectCoordinate.FixNaN();
        return(rectCoordinate);
    }
예제 #6
0
    /// <summary>
    /// Return the rect size normalized as a value relative to the canvas size. Examples: When the size is the same than the canvas this returns (1,1) and returns (0.5,0.5) when the size is the half of the canvas size in both axis. It does not matter if the container is not the canvas or the anchors positios.
    /// </summary>
    /// <param name="tr"></param>
    public static Vector2 GetSizeNormalizedInCanvasAnchorCoordinatesFromRectSize(RectTransform tr)
    {
        Vector2 position     = GetCanvasAnchorCoordinateNormalizedFromRectPosition(tr);
        Vector2 topLeftPoint = GetCanvasAnchorCoordinateNormalizedFromRectCoordinate(tr, RteRectTools.GetPositionIgnoringAnchorsAndPivot(tr) + RteRectTools.GetSizeIgnoringAnchors(tr));

        return(topLeftPoint - position);
    }
예제 #7
0
    /// <summary>
    /// Return the rect size as screen space pixels. It does not matter if the container is not the canvas or the anchors positios.
    /// </summary>
    /// <param name="tr"></param>
    public static Vector2 GetScreenSpaceSizeFromRectSize(RectTransform tr)
    {
        Vector2 position     = GetScreenSpaceCoordinateFromAnchorCoordinate(tr, GetAnchorCoordianteFromRectCoordinate(tr, RteRectTools.GetPositionIgnoringAnchorsAndPivot(tr)));
        Vector2 topLeftPoint = GetScreenSpaceCoordinateFromAnchorCoordinate(tr, GetAnchorCoordianteFromRectCoordinate(tr, RteRectTools.GetPositionIgnoringAnchorsAndPivot(tr) + RteRectTools.GetSizeIgnoringAnchors(tr)));

        return(topLeftPoint - position);
    }