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