예제 #1
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;
        }
    }
예제 #2
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);
        }
    }