Edwarddv

CanvasSpaceAnchoredPosition()

Apr 27th, 2016
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.77 KB | None | 0 0
  1. public static Vector2 CanvasSpaceAnchoredPosition(RectTransform rectTransform, Rect canvasRect)
  2. {
  3. Vector2 pos = Camera.main.ScreenToViewportPoint(rectTransform.position);
  4. pos.x = (pos.x * canvasRect.width) - (canvasRect.width * 0.5f);
  5. pos.y = (pos.y * canvasRect.height) - (canvasRect.height * 0.5f);
  6. return pos;
  7. }
  8.  
  9. // usage example.
  10. // rectTransform is a RectTransform you want the canvas space anchoredPosition of,
  11. // but can't get because it's nested under other RectTransforms.
  12. // mainCanvas is the Canvas in screen space overlay render mode.
  13.  
  14. Vector2 anchorPos = CanvasSpaceAnchoredPosition(rectTransform, mainCanvas.rect);
  15.  
  16. // 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