- Home /
Why UI Element corners are equal to Canvas corners?
I'm building a game in Unity for Android in portrait mode. I have a Canvas as big as the display and the render mode is set to Screen Space - Camera. I also have a RawImage (child of the Canvas) colored in blue that I resized and repositioned in code to fit my needs. The problem came when I had to get the corners of the RawImage using the GetWorldCorners property of RectTransform. There was something wrong with the results and I finally found the problem, which is that even though the RawImage was resized and repositioned, the corners of the RawImage were the same as the corners of the Canvas. Look at the image below:
I selected the RawImage from the Hierarchy and currently, the corners of the RawImage are the ones pointed by the RED Arrows. But I need the corners to be the ones pointed by the GREEN Arrows.
Here is a simplified version of the resizing and repositioning function:
private void applyResize()
{
//The values of the Vector2s are the output, in the real function there is a formula that isn't relevant to this question
myRawImage.GetComponent<RectTransform>().sizeDelta = new Vector2(864, 864);
myRawImage.GetComponent<RectTransform>().anchoredPosition = new Vector2(0, -1178);
}
I am sure that the problem comes from the 2 lines of code in the above function since you can see in the image that even the Pivot has not changed. I need to find a way to adapt the corners to the repositioned and resized RawImage, not before it was modified. I can provide any additional information if needed. I will greatly appreciate any amount of help. Thank you in advance!