Convert global or local position to anchorPosition for a RectTransform?
I have a RectTransform inside a Canvas (myRectTransform).
I have a global position of the mouse on the screen (688, 5).
I can convert globalPosition to localPosition (194.5, -531.6):
Vector2 localPosition;
RectTransformUtility.ScreenPointToLocalPointInRectangle(
myRectTransform.parent.GetComponent<RectTransform>(),
globalPosition,
GetComponentInParent<Canvas>().worldCamera,
out localPosition);
If I set myRectTransform.localPosition = localPosition, it moves to the mouse, and automatically sets myRectTransform.anchoredPosition to (1153.8, -1071.6)
How do I calculate the final anchoredPosition WITHOUT setting myRectTransform's position, and allowing for unknown anchors, pivot, scale, etc?
I never found a good solution, but I use the workaround I alluded to: briefly set the localPosition, record the anchoredPosition, then set it back. I'd love some equivalent of Transform.TransformPoint ins$$anonymous$$d, but it doesn't exist, and Unity uses injected C code for positioning so the source isn't easily accessible.
Your answer
![](https://koobas.hobune.stream/wayback/20220612185115im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
UI Canvas Anchor points are stuck in the bottom left corner and disabled when in overlay mode. 1 Answer
how to set position of multiple images instantiate and set images in canvas verticallly 0 Answers
What is the difference between translate and position 1 Answer
OnTriggerEnter can't change detected gameObjects transform.position 1 Answer