- Home /
Question by
EendhoornGames · Mar 17, 2016 at 08:34 AM ·
uicanvasworldspace
translating world position to (scaled) world-space canvas
I've been trying to position a canvas in world coordinates. There were lots of resources on this, but none of them yield correct results. I'm guessing this is because my canvas is scaled by the canvas scaler (for pixel-perfect art):
int startX = i * tilegrid.chunkSize;
GameObject stageStatsUI = Instantiate( ChallengeStageStats );
stageStatsUI.transform.SetParent( canvas.transform, false );
Vector2 destPos = new Vector2( tilegrid.tf.position.x + startX, tilegrid.tf.position.y );
Vector2 viewportPoint = Camera.main.WorldToViewportPoint( destPos );
// Vector2 viewportPoint = RectTransformUtility.WorldToScreenPoint( Camera.main, destPos );
RectTransform rectTransform = stageStatsUI.GetComponent<RectTransform>();
rectTransform.position = viewportPoint;
//rectTransform.anchoredPosition = viewportPoint;
Any idea what I'm doing wrong? Thanks!
canvas.png
(64.8 kB)
Comment
Your answer
Follow this Question
Related Questions
Scalewithscreensize equivalent for worldspace canvas's 0 Answers
Canvas to World Coordinates Works in Editor but Not in Builds 1 Answer
UI canvas attached to vive controller disappears after first run in editor on vive 0 Answers
World Space not displaying my UI. Any suggestions? 2 Answers
Convert UI element to world position 1 Answer