Created
December 20, 2022 14:47
-
-
Save tomkail/b28ab29c2ea8248f240c4177a098a995 to your computer and use it in GitHub Desktop.
Converts between localPosition and anchoredPosition for a RectTransform.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Add this to convert a local position to an anchored position | |
public static Vector2 GetLocalToAnchoredPositionOffset(this RectTransform rectTransform) { | |
var parentRT = (RectTransform) rectTransform.parent; | |
var pivotAnchor = new Vector2(Mathf.LerpUnclamped(rectTransform.anchorMin.x, rectTransform.anchorMax.x, rectTransform.pivot.x), Mathf.LerpUnclamped(rectTransform.anchorMin.y, rectTransform.anchorMax.y, rectTransform.pivot.y)); | |
return -parentRT.rect.size * (pivotAnchor - parentRT.pivot); | |
} | |
// Add this to convert a local position to an anchored position | |
public static Vector2 GetAnchoredToLocalPositionOffset(this RectTransform rectTransform) { | |
return -rectTransform.GetLocalToAnchoredPositionOffset(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment