- Home /
Question by
swagentrotz · Jun 12, 2017 at 08:48 AM ·
positionrecttransformpivotadjust
How to force counter-adjusting of RectTransform Position when changing the Pivot at runtime?
Hi,
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?
Thanks, Sascha
Comment
this would be a really useful feature to add even if its just doable at editor time in script.
Answer by Mrpxl · Jun 21, 2021 at 08:22 AM
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;
}