Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public static Vector2 CanvasSpaceAnchoredPosition(RectTransform rectTransform, Rect canvasRect)
- {
- Vector2 pos = Camera.main.ScreenToViewportPoint(rectTransform.position);
- pos.x = (pos.x * canvasRect.width) - (canvasRect.width * 0.5f);
- pos.y = (pos.y * canvasRect.height) - (canvasRect.height * 0.5f);
- return pos;
- }
- // usage example.
- // rectTransform is a RectTransform you want the canvas space anchoredPosition of,
- // but can't get because it's nested under other RectTransforms.
- // mainCanvas is the Canvas in screen space overlay render mode.
- Vector2 anchorPos = CanvasSpaceAnchoredPosition(rectTransform, mainCanvas.rect);
- // Now set the anchoredPosition of another RectTransform that you want to align to it to anchorPos.
Advertisement
Add Comment
Please, Sign In to add comment