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