- Home /
How to get the actual World Positions of the Corners of an UI element when using GetWorldCorners if Canvas is set to Screen Space - World
Hey guys!
I've written my custom script for dragging UI objects within an area provided by a RectTransform. I worked with the script with the UI in "Screen Space - Overlay" mode and GetWorldCorners got me the actual world coordinates of the corners. Everything looked good.
I currently need a particle effect in the game, so I set the UI to "Screen Space - Camera", but the GetWorldCorners method is currently returning the RectTransform's corners relative to the Canvas position. When I set the position of anything to one of the corners they end up in completely different places than the RectTransform.
Is anyone familiar with this problem? It seems this guy, (2nd paragraph) had the same problem but no-one had an answer. Maybe one of you guys do?
Greetings & thanks in advance!
Answer by spajus · Feb 12, 2018 at 06:57 AM
Try this:
Vector3[] corners = new Vector3[];
GetComponent<RectTransform>().GetWorldCorners(corners);
foreach (Vector3 corner in corners) {
Debug.Log(corner);
}
Answer by madhavarora · May 05 at 12:43 PM
The solution given by Spajus didn't work for me but this did:
Canvas.ForceUpdateCanvases();
var menuCorners = new Vector3[4];
menu.GetComponent<RectTransform>().GetWorldCorners(menuCorners);