/// <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); }
public static Vector2 GetLocalPositionFromCanvasPosition(RectTransform transform, Vector2 canvasPosition) { Vector2 rectCoordinate = RteAnchorTools.GetRectCoordinateFromCanvasAnchorCoordinateNormalized(transform, canvasPosition); Vector2 position = RteRectTools.GetPositionIgnoringAnchorsAndPivot(transform); Vector2 size = RteRectTools.GetSizeIgnoringAnchors(transform); rectCoordinate -= position; rectCoordinate.x /= size.x; rectCoordinate.y /= size.y; rectCoordinate.FixNaN(); return(rectCoordinate); }