Canvas with CanvasScaler doesn't behave as expected

Hi there,

I have a UI that is a ‘Screen Space - Overlay’ Canvas in the scene (with some children). It’s controlled by a Canvas scaler. By default, the GameObject of this UI is disabled.

Now, when the user presses a mouse button I enable the Canvas’ GameObject, and position it’s Child like this:

RectTransform UICanvas; // The canvas itself
RectTransform UICanvasChild; // The only child, used for positioning

UICanvas.gameObject.SetActive(true);
Vector2 pos;
RectTransformUtility.ScreenPointToLocalPointInRectangle(UICanvas, Input.mousePosition, null, out pos);
UICanvasChild.anchoredPosition = pos;

Now this works fine on most resolutions I’ve tried, but is problematic in one case: when running the game using the reference resolution (1080p in my case), the first time I open the menu the position is wrong. It’s at (0,0). With all other resolutions, and after the first time on reference resolution, it works just fine.

I suspect this is a bug, somewhere along the lines of this: The CanvasScaler takes control over the Canvas’ resolution, but then early-outs when it notices that the resolution is exactly the reference resolution. Nothing to do for the CanvasScaler, right? Wrong, since the Canvas relies on it to set the resolution, even in the most simple 1:1 case. There also another thread reporting a similar error, unfortunately without replies.

What I tried (after enabling the Canvas GameObject, before getting the position using RectTransformUtility):

None of this solved the problem. I suspect I could work around this using a WaitForEndOfFrame, but I’d rather keep looking for a cleaner solution. Does anyone have experience with this kind of error, or is there a Unity UI dev around who could double-check the early-out-related error I described above (before I open a ticket)? :slight_smile:

Cheers
Daerst

I’ve filed a big with the Unity support. They have confirmed it and are working on a fix.

For now, the workaround is to assign a different renderMode to your Canvas and change it back. This forces two updates of the Rect values, which is better than zero in my book :slight_smile: