How to force counter-adjusting of RectTransform Position when changing the Pivot at runtime?


I would like to change the pivot of a RectTransform at runtime, unfortunately the position does not counter-adjust like it does when I change the pivot in the editor.
Is there a way to force the counter-adjustment at runtime?


I’ve got a method that does this:

public static void SetPivot(RectTransform rectTransform, Vector2 pivot)
		if (rectTransform == null) return;

		Vector2 size = rectTransform.rect.size;
		Vector2 deltaPivot = rectTransform.pivot - pivot;
		Vector3 deltaPosition = new Vector3(deltaPivot.x * size.x, deltaPivot.y * size.y);
		rectTransform.pivot = pivot;
		rectTransform.localPosition -= deltaPosition;