Пример #1
0
    /// <summary>
    /// A method get the anchors size. This is usefull to manipulate anchors size 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 size to be returned.</param>
    public static Vector2 GetAnchorsSize(this RectTransform tr, AnchorsCoordinateSystem coordinateSystem = AnchorsCoordinateSystem.Default)
    {
        switch (coordinateSystem)
        {
        case AnchorsCoordinateSystem.Default:
            return(RteAnchorTools.GetAnchorsSize(tr));

        case AnchorsCoordinateSystem.AsChildOfCanvas:
            return(RteAnchorTools.GetSizeInCanvasAnchorCoordinatesFromAnchorsSize(tr));

        case AnchorsCoordinateSystem.ScreenSpacePixels:
            return(RteAnchorTools.GetScreenSpaceSizeFromAnchorsSize(tr));

        case AnchorsCoordinateSystem.AsRect:
            return(RteAnchorTools.GetRectSizeFromAnchorSize(tr, RteAnchorTools.GetAnchorsSize(tr)));

        case AnchorsCoordinateSystem.InsideCanvas:
            return(RteAnchorTools.GetSizeInCanvasAnchorCoordinatesFromAnchorsSize(tr));

        case AnchorsCoordinateSystem.OutsideCanvas:
            return(RteAnchorTools.GetSizeInCanvasAnchorCoordinatesFromAnchorsSize(tr));

        case AnchorsCoordinateSystem.OutsideContainer:
            return(RteAnchorTools.GetAnchorsSize(tr));
        }

        return(RteAnchorTools.GetAnchorsSize(tr));
    }
Пример #2
0
    /// <summary>
    /// Get the height not ignoring anchors from a height value that ignores anchors.
    /// </summary>
    /// <param name="transf"></param>
    /// <param name="heightIgnoringAnchors"></param>
    /// <returns></returns>
    public static float GetHeightAnchored(RectTransform transf, float heightIgnoringAnchors)
    {
        Vector2 anchorsSize = RteAnchorTools.GetAnchorsSize(transf);
        Vector2 parentSize  = GetParentSizeIgnoringAnchors(transf);

        float height = parentSize.y * anchorsSize.y - heightIgnoringAnchors;

        return(height);
    }
Пример #3
0
    /// <summary>
    /// Get the width not ignoring anchors from a width value that ignores anchors.
    /// </summary>
    /// <param name="transf"></param>
    /// <param name="widthIgnoringAnchors"></param>
    /// <returns></returns>
    public static float GetWidthAnchored(RectTransform transf, float widthIgnoringAnchors)
    {
        Vector2 anchorsSize = RteAnchorTools.GetAnchorsSize(transf);
        Vector2 parentSize  = GetParentSizeIgnoringAnchors(transf);

        float width = parentSize.x * anchorsSize.x - widthIgnoringAnchors;

        return(width);
    }
Пример #4
0
    /// <summary>
    /// The With and Height parameters of RectTransform gives relative to anchors values when anchors are not joined, wich is not so usefull. With this you can get the real Size of the RectTransform. Like when the anchors are joined.
    /// </summary>
    /// <param name="transf"></param>
    public static Vector2 GetSizeIgnoringAnchors(RectTransform transf)
    {
        Vector2 parentSize  = GetParentSizeIgnoringAnchors(transf);
        Vector2 anchorsSize = RteAnchorTools.GetAnchorsSize(transf);

        return(new Vector2
               (
                   parentSize.x * anchorsSize.x + transf.sizeDelta.x,
                   parentSize.y * anchorsSize.y + transf.sizeDelta.y
               ));
    }
Пример #5
0
    /// <summary>
    /// Returns the position of the pivot point relative to the parent rect coordinates, instead of what you get with AnchoredPosition property which is the position of the pivot relative to the anchors center point.
    /// </summary>
    /// <param name="transf"></param>
    /// <returns></returns>
    public static Vector2 GetPositionIgnoringAnchors(RectTransform transf)
    {
        Vector2 anchorsSize       = RteAnchorTools.GetAnchorsSize(transf);
        Vector2 anchorsPivotedPos = new Vector2(transf.anchorMin.x + anchorsSize.x * transf.pivot.x, transf.anchorMin.y + anchorsSize.y * transf.pivot.y);
        Vector2 parentSize        = GetParentSizeIgnoringAnchors(transf);

        return(new Vector2
               (
                   transf.anchoredPosition.x + parentSize.x * anchorsPivotedPos.x,
                   transf.anchoredPosition.y + parentSize.y * anchorsPivotedPos.y
               ));
    }
Пример #6
0
    /// <summary>
    /// Sets the position of the object with a coordinate relative to the parent rect coordinates, instead of what you set with AnchoredPosition property which is the position relative to the anchors center point.
    /// </summary>
    /// <param name="transf"></param>
    /// <param name="newPosition"></param>
    public static void SetPositionIgnoringAnchors(RectTransform transf, Vector2 newPosition)
    {
        Vector2 anchorsSize       = RteAnchorTools.GetAnchorsSize(transf);
        Vector2 anchorsPivotedPos = new Vector2(transf.anchorMin.x + anchorsSize.x * transf.pivot.x, transf.anchorMin.y + anchorsSize.y * transf.pivot.y);
        Vector2 parentSize        = GetParentSizeIgnoringAnchors(transf);

        transf.anchoredPosition = newPosition - new Vector2
                                  (
            parentSize.x * anchorsPivotedPos.x,
            parentSize.y * anchorsPivotedPos.y
                                  );
    }
Пример #7
0
    /// <summary>
    /// Get the size not ignoring anchors from a size value that ignores anchors.
    /// </summary>
    /// <param name="transf"></param>
    /// <param name="sizeIgnoringAnchors"></param>
    public static Vector2 GetSizeAnchored(RectTransform transf, Vector2 sizeIgnoringAnchors)
    {
        Vector2 anchorsSize = RteAnchorTools.GetAnchorsSize(transf);
        Vector2 parentSize  = GetParentSizeIgnoringAnchors(transf);

        Vector2 size = new Vector2
                       (
            parentSize.x * anchorsSize.x - sizeIgnoringAnchors.x,
            parentSize.y * anchorsSize.y - sizeIgnoringAnchors.y
                       );

        return(size);
    }
Пример #8
0
    /// <summary>
    /// Sets the 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="newPosition"></param>
    public static void SetPositionIgnoringAnchorsAndPivot(RectTransform transf, Vector2 newPosition, bool centerPivot = false)
    {
        Vector2 rectSize          = GetSizeIgnoringAnchors(transf);
        Vector2 anchorsSize       = RteAnchorTools.GetAnchorsSize(transf);
        Vector2 anchorsPivotedPos = new Vector2(transf.anchorMin.x + anchorsSize.x * transf.pivot.x, transf.anchorMin.y + anchorsSize.y * transf.pivot.y);
        Vector2 parentSize        = GetParentSizeIgnoringAnchors(transf);

        transf.anchoredPosition = newPosition - new Vector2
                                  (
            parentSize.x * anchorsPivotedPos.x - rectSize.x * transf.pivot.x,
            parentSize.y * anchorsPivotedPos.y - rectSize.y * transf.pivot.y
                                  );

        if (centerPivot)
        {
            transf.anchoredPosition -= new Vector2(rectSize.x * 0.5f, rectSize.y * 0.5f);
        }
    }