Example #1
0
    /// <summary>
    /// Get the anchors position. This is usefull to manipulate anchors position with only a single Vector2, specially usefull for tweens and clean code.
    /// </summary>
    /// <param name="tr"></param>
    /// <param name="coordinateSystem">The coordinate system you want the anchors position to be returned.</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 the square formed by the anchors, if this parameter is true then the pivot will be located at the center of the square formed by the anchors.</param>
    public static Vector2 GetAnchorsPosition(this RectTransform tr, AnchorsCoordinateSystem coordinateSystem = AnchorsCoordinateSystem.Default, bool rtePivotCentered = false)
    {
        switch (coordinateSystem)
        {
        case AnchorsCoordinateSystem.Default:
            return(RteAnchorTools.GetAnchorsPosition(tr, rtePivotCentered));

        case AnchorsCoordinateSystem.AsChildOfCanvas:
            return(RteAnchorTools.GetCanvasAnchorCoordinateFromAnchorCoordinate(tr, RteAnchorTools.GetAnchorsPosition(tr, rtePivotCentered)));

        case AnchorsCoordinateSystem.ScreenSpacePixels:
            return(RteAnchorTools.GetScreenSpaceCoordinateFromAnchorCoordinate(tr, RteAnchorTools.GetAnchorsPosition(tr, rtePivotCentered)));

        case AnchorsCoordinateSystem.AsRect:
            return(RteAnchorTools.GetRectCoordinateFromAnchorCoordinate(tr, RteAnchorTools.GetAnchorsPosition(tr, rtePivotCentered)));

        case AnchorsCoordinateSystem.InsideCanvas:
            return(RteAnchorTools.GetInsideOfCanvasCoordinateFromAnchorCoordinate(tr, RteAnchorTools.GetAnchorsPosition(tr, rtePivotCentered)));

        case AnchorsCoordinateSystem.OutsideCanvas:
            return(RteAnchorTools.GetOutsideOfCanvasCoordinateFromAnchorCoordinate(tr, RteAnchorTools.GetAnchorsPosition(tr, rtePivotCentered)));

        case AnchorsCoordinateSystem.OutsideContainer:
            return(RteAnchorTools.GetOutsideOfContainerCoordinateFromAnchorCoordinate(tr, RteAnchorTools.GetAnchorsPosition(tr, rtePivotCentered)));
        }

        return(RteAnchorTools.GetAnchorsPosition(tr, rtePivotCentered));
    }
    /// <summary>
    /// Sets the Y position of the object with the left down point as the position point instead of the pivot point as the position point, also the position is not set relative to the anchors but relative to the parent rect coordinates.
    /// </summary>
    /// <param name="transf"></param>
    /// <param name="newPositionY"></param>
    public static void SetPositionYIgnoringAnchorsAndPivot(RectTransform transf, float newPositionY, bool centerPivot = false)
    {
        Vector2 size = Vector2.zero;

        if (centerPivot)
        {
            size = GetSizeIgnoringAnchors(transf);
        }
        Vector2 newPos     = transf.anchoredPosition;
        Vector2 parentSize = GetParentSizeIgnoringAnchors(transf);

        newPos.y = newPositionY - (parentSize.y * RteAnchorTools.GetAnchorsPosition(transf, true).y) + (transf.rect.height * transf.pivot.y) - size.y * 0.5f;
        transf.anchoredPosition = newPos;
    }