Esempio n. 1
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);
        }
    }
Esempio n. 2
0
    /// <summary>
    /// With this method you can set the position of the UI object in different kind of coordinate systems.
    /// </summary>
    /// <param name="tr"></param>
    /// <param name="targetPosition"> Your target position.</param>
    /// <param name="coordinateSystem"> The coordinate system of your target position.</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 void SetPosition(this RectTransform tr, Vector2 targetPosition, CoordinateSystem coordinateSystem = CoordinateSystem.IgnoreAnchorsAndPivot, bool rtePivotCentered = false)
    {
        switch (coordinateSystem)
        {
        case CoordinateSystem.IgnoreAnchors:
            RteRectTools.SetPositionIgnoringAnchors(tr, targetPosition);
            return;

        case CoordinateSystem.IgnoreAnchorsAndPivotNormalized:
            RteRectTools.SetPositionNormalizedIgnoringAnchorsAndPivot(tr, targetPosition, rtePivotCentered);
            return;

        case CoordinateSystem.AsChildOfCanvas:
            targetPosition = RteAnchorTools.GetRectCoordinateFromCanvasAnchorCoordinate(tr, targetPosition);
            break;

        case CoordinateSystem.AsChildOfCanvasNormalized:
            targetPosition = RteAnchorTools.GetRectCoordinateFromCanvasAnchorCoordinateNormalized(tr, targetPosition);
            break;

        case CoordinateSystem.ScreenSpacePixels:
            targetPosition = RteAnchorTools.GetRectCoordinateFromScreenSpaceCoordinate(tr, targetPosition);
            break;
        }

        RteRectTools.SetPositionIgnoringAnchorsAndPivot(tr, targetPosition, rtePivotCentered);
    }
Esempio n. 3
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);
    }
Esempio n. 4
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);
    }
Esempio n. 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);
        }
    }
    /// <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);
    }
Esempio n. 7
0
    /// <summary>
    /// With this method you can set the size of the UI object in different kind of coordinate systems.
    /// </summary>
    /// <param name="tr"></param>
    /// <param name="targetSize"></param>
    /// <param name="coordinateSystem">The coordinate system of your target size.</param>
    /// <param name="rtePivotCentered">When using this tool the Unity pivot is ignored and a pivot from this tool is used, wich 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 void SetSize(this RectTransform tr, Vector2 targetSize, CoordinateSystem coordinateSystem = CoordinateSystem.IgnoreAnchorsAndPivot, bool rtePivotCentered = false)
    {
        switch (coordinateSystem)
        {
        case CoordinateSystem.IgnoreAnchors:
            RteRectTools.SetSizeIgnoringAnchors(tr, targetSize, rtePivotCentered);
            break;

        case CoordinateSystem.IgnoreAnchorsAndPivot:
            RteRectTools.SetSizeIgnoringAnchors(tr, targetSize, rtePivotCentered);
            break;

        case CoordinateSystem.IgnoreAnchorsAndPivotNormalized:
            RteRectTools.SetSizeNormalizedIgnoringAnchors(tr, targetSize, rtePivotCentered);
            break;

        case CoordinateSystem.AsChildOfCanvas:
            RteRectTools.SetSizeIgnoringAnchors(tr, RteAnchorTools.GetRectSizeFromCanvasAnchorCoordinatesSize(tr, targetSize), rtePivotCentered);
            break;

        case CoordinateSystem.AsChildOfCanvasNormalized:
            RteRectTools.SetSizeIgnoringAnchors(tr, RteAnchorTools.GetRectSizeFromCanvasAnchorCoordinatesSizeNormalized(tr, targetSize), rtePivotCentered);
            break;

        case CoordinateSystem.ScreenSpacePixels:
            RteRectTools.SetSizeIgnoringAnchors(tr, RteAnchorTools.GetRectSizeFromScreenSpaceSize(tr, targetSize), rtePivotCentered);
            break;
        }
    }
Esempio n. 8
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));
    }
    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));
    }
    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);
    }
    /// <summary>
    /// Returns if a UI object is inside the visible area or not by checking if the rect is inside the canvas rect, the algorithm is very optimized. World space canvases not supported.
    /// </summary>
    /// <param name="canvas">A reference to the canvas is needed, getting the reference is a expensive operation so if you set null here the operation is made inside this method each time you call it, otherwise you can get the reference once and pass it here to improve performance.</param>
    public static bool IsVisible(this RectTransform tr, Canvas canvas = null)
    {
        if (canvas == null)
        {
            canvas = tr.GetCanvas().GetComponent <Canvas>();
        }

        RteRectTools.GetRectCornersInScreenSpacePixels(tr, canvas, Corners);

        float maxY = Mathf.Max(Corners[0].y, Corners[1].y, Corners[2].y, Corners[3].y);
        float minY = Mathf.Min(Corners[0].y, Corners[1].y, Corners[2].y, Corners[3].y);
        float maxX = Mathf.Max(Corners[0].x, Corners[1].x, Corners[2].x, Corners[3].x);
        float minX = Mathf.Min(Corners[0].x, Corners[1].x, Corners[2].x, Corners[3].x);

        return(!(
                   maxY.IsAproximatelyOrLess(0) ||
                   minY.IsAproximatelyOrMore(Screen.height) ||
                   maxX.IsAproximatelyOrLess(0) ||
                   minX.IsAproximatelyOrMore(Screen.width)
                   ));
    }
    public static void SetPivotPosition(RectTransform transform, Vector2 newPivotPos, bool alsoMoveRect = false)
    {
        if (alsoMoveRect)
        {
            transform.pivot = newPivotPos;
            return;
        }

        // This gives support to scaling, this is important when working with the pivot.
        Vector2 pivotDelta = newPivotPos - transform.pivot;

        pivotDelta.x /= transform.localScale.x;
        pivotDelta.y /= transform.localScale.y;
        pivotDelta    = pivotDelta.FixNaN();

        // Unity moves the rect to the pivot target position when moving the pivot with
        // code, also does strange things when the anchors are not joinded, so with this
        // we calculate ourselves the position that should have the rect to correct these issues.
        Vector2 correctPosition = GetRectCoordinatesFromPivotPosition(transform, newPivotPos);

        transform.pivot += pivotDelta;
        RteRectTools.SetPositionIgnoringAnchors(transform, correctPosition);
    }
Esempio n. 13
0
    /// <summary>
    /// Converts a point in the rect coordinates to anchor coordinates. The returned value will be the anchor coordinates if the anchors were in that specific position position in rect coordinates.
    /// </summary>
    /// <param name="tr"></param>
    public static Vector2 GetAnchorCoordianteFromRectCoordinate(RectTransform tr, Vector2 rectCoordinate)
    {
        Vector2 parentSize = RteRectTools.GetParentSizeIgnoringAnchors(tr);

        return(FixNaN(new Vector2(rectCoordinate.x / parentSize.x, rectCoordinate.y / parentSize.y)));
    }
Esempio n. 14
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));
    }
Esempio n. 15
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);
    }
Esempio n. 16
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)));
 }
Esempio n. 17
0
    /// <summary>
    /// Converts a point in the anchor coordinates to rect coordinates. For example place the rect in a specific position and the returned value will be the anchor coordinates if the anchors were in that specific position.
    /// </summary>
    /// <param name="tr"></param>
    public static Vector2 GetRectCoordinateFromAnchorCoordinate(RectTransform tr, Vector2 anchorCoordinate)
    {
        Vector2 parentSize = RteRectTools.GetParentSizeIgnoringAnchors(tr);

        return(new Vector2(parentSize.x * anchorCoordinate.x, parentSize.y * anchorCoordinate.y));
    }
 /// <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());
 }
Esempio n. 19
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);
    }