예제 #1
0
    /// <summary>
    /// With this method you can get the position of the UI object in different kind of coordinate systems.
    /// </summary>
    /// <param name="tr"></param>
    /// <param name="coordinateSystem"> The returned position will be from the coordinate system you specify in this parameter.</param>
    /// <param name="rtePivotCentered"> This tool has it's own pivot, the Unity pivot is ignored in all the coordinate systems except "IgnoreAnchors", the pivot of this tool is located at the lower left corner of this object, if this parameter is true then the pivot will be located at the center of this object.</param>
    public static Vector2 GetPosition(this RectTransform tr, CoordinateSystem coordinateSystem = CoordinateSystem.IgnoreAnchorsAndPivot, bool rtePivotCentered = false)
    {
        Vector2 result = default(Vector2);

        switch (coordinateSystem)
        {
        case CoordinateSystem.IgnoreAnchorsAndPivot:
            result = RteRectTools.GetPositionIgnoringAnchorsAndPivot(tr, rtePivotCentered);
            break;

        case CoordinateSystem.IgnoreAnchors:
            result = RteRectTools.GetPositionIgnoringAnchors(tr);
            break;

        case CoordinateSystem.IgnoreAnchorsAndPivotNormalized:
            result = RteRectTools.GetPositionNormalizedIgnoringAnchorsAndPivot(tr, rtePivotCentered);
            break;

        case CoordinateSystem.AsChildOfCanvas:
            result = RteRectTools.GetPositionIgnoringAnchorsAndPivot(tr, rtePivotCentered);
            result = RteAnchorTools.GetCanvasAnchorCoordinateFromRectCoordinate(tr, result);
            break;

        case CoordinateSystem.AsChildOfCanvasNormalized:
            result = RteRectTools.GetPositionIgnoringAnchorsAndPivot(tr, rtePivotCentered);
            result = RteAnchorTools.GetCanvasAnchorCoordinateNormalizedFromRectCoordinate(tr, result);
            break;

        case CoordinateSystem.ScreenSpacePixels:
            result = RteAnchorTools.GetScreenSpaceCoordinateFromRectCoordinate(tr, RteRectTools.GetPositionIgnoringAnchorsAndPivot(tr, rtePivotCentered));
            break;
        }

        return(result);
    }
예제 #2
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));
    }
예제 #3
0
    /// <summary>
    /// Convert rect coordinates into pivot coordinates (values between 0 and 1).
    /// </summary>
    public static Vector2 GetRectCoordinatesFromPivotPosition(RectTransform tr, Vector2 pivotPosition)
    {
        Vector2 pivotPosInLocalRectCoord = new Vector2(pivotPosition.x * tr.rect.size.x, pivotPosition.y * tr.rect.size.y);
        Vector2 rectPos = RteRectTools.GetPositionIgnoringAnchorsAndPivot(tr);

        return(pivotPosInLocalRectCoord + rectPos);
    }
예제 #4
0
    /// <summary>
    /// Move the anchors, moving anchors also moves the image as you may know. This is the same than
    /// moving the anchors holding control + shift key in the Unity editor.
    /// To set the position of another object you must pass: SetPosition(tr.anchorMin.x, tr.anchorMin.y) because
    /// these are the values that determines the anchors position of the object. For a more simple sintax use
    /// SetPosition(targetTransform.GetPosition()) or SetPosition(targetTransform).
    /// 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="targetPos"></param>
    /// <param name="centeredPivot">If false the pivot is in the down left corner, if true the pivot is in the center</param>
    public static void SetAnchorsPosition(RectTransform tr, Vector2 targetPos, bool centeredPivot = false, bool alsoChangeRect = true)
    {
        Vector2 rectPos = Vector2.zero;

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

        Vector2 size = GetAnchorsSize(tr);

        if (centeredPivot)
        {
            targetPos -= new Vector2(size.x * 0.5f, size.y * 0.5f);
        }

        // anchorMin.x and anchorMix.y moves the object, the other 2 values just move the same amount (if the size is not changed).
        tr.anchorMin = targetPos;
        tr.anchorMax = targetPos + size;

        if (!alsoChangeRect)
        {
            RteRectTools.SetPositionIgnoringAnchorsAndPivot(tr, rectPos);
        }
    }
예제 #5
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);
        }
    }
예제 #6
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));
    }
예제 #7
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);
    }
예제 #8
0
 /// <summary>
 /// Convert pivot coordinates (values between 0 and 1) into rect coordinates.
 /// </summary>
 public static Vector2 GetPivotPositionFromRectCoordinatesIgnoringAnchors(RectTransform tr, Vector2 rectCoordinates)
 {
     rectCoordinates -= RteRectTools.GetPositionIgnoringAnchorsAndPivot(tr);
     return(new Vector2(rectCoordinates.x / tr.rect.size.x, rectCoordinates.y / tr.rect.size.y).FixNaN());
 }
예제 #9
0
    /// <summary>
    /// Returns a size in rect coordinates from a size in anchor canvas coordinate (normalized).
    /// </summary>
    /// <param name="tr"></param>
    /// <param name="canvasAnchorCoordinateSize"></param>
    /// <returns></returns>
    public static Vector2 GetRectSizeFromCanvasAnchorCoordinatesSizeNormalized(RectTransform tr, Vector2 canvasAnchorCoordinateSize)
    {
        Vector2 canvasPos = GetCanvasAnchorCoordinateNormalizedFromRectPosition(tr);
        Vector2 canvasAnchorCoordinateSizeToLocalAnchorSize = GetAnchorCoordinateFromCanvasAnchorCoordinate(tr, canvasPos + canvasAnchorCoordinateSize) - GetAnchorCoordianteFromRectCoordinate(tr, RteRectTools.GetPositionIgnoringAnchorsAndPivot(tr));

        return(GetRectCoordinateFromAnchorCoordinate(tr, canvasAnchorCoordinateSizeToLocalAnchorSize));
    }
예제 #10
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);
    }
예제 #11
0
 /// <summary>
 /// Return the rect position as a value relative to the canvas size. When the rect is located at the Left Down corner of the canvas this returns (0,0) and returns (1,1) when is placed at the top right corner of the canvas. It does not matter if the container is not the canvas or the anchors positions.
 /// </summary>
 /// <param name="tr"></param>
 /// <param name="centerPivot"></param>
 public static Vector2 GetCanvasAnchorCoordinateNormalizedFromRectPosition(RectTransform tr, bool centerPivot = false)
 {
     return(GetCanvasAnchorCoordinateNormalizedFromRectCoordinate(tr, RteRectTools.GetPositionIgnoringAnchorsAndPivot(tr, centerPivot)));
 }
예제 #12
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);
    }