How to calculate world space size of ui elements

Hello, first time posting, let me know if I am doing this wrong.

I am making a 2d grid game and want to resize the orthographic camera to fill the whole screen with the grid. That part was not to hard to figure out. The orthographic size is just half of height of the grid in world space. But now I have a UI and I need to recalculate the orthographic size to account for the ui that is on the top and bottom of the screen. So I need a way to calculate the height of the ui in world space so I can add that to the height of my grid and then calculate the orthographic size.

Here is my code so far for trying to figure out the ui height.

GameObject objectivePanel = this.uiOrientation_.FindChild(“ObjectivePanel”);
GameObject powerUpPanel = this.uiOrientation_.FindChild(“PowerUpPanel”);

RectTransform objectivePanelTransform = objectivePanel.GetComponent();
RectTransform powerUpPanelTransform = powerUpPanel.GetComponent();

Vector2 objectivePanelMinWorldSpace = Camera.main.ScreenToWorldPoint(objectivePanelTransform.rect.min);
Vector2 objectivePanelMaxWorldSpace = Camera.main.ScreenToWorldPoint(objectivePanelTransform.rect.max);

Vector2 powerUpPanelMinWorldSpace = Camera.main.ScreenToWorldPoint(powerUpPanelTransform.rect.min);
Vector2 powerUpPanelMaxWorldSpace = Camera.main.ScreenToWorldPoint(powerUpPanelTransform.rect.max);

float powerUpPanelHeight = powerUpPanelMaxWorldSpace.x - powerUpPanelMinWorldSpace.x;
float objectivePanelHeight = objectivePanelMaxWorldSpace.y - objectivePanelMinWorldSpace.y;

I am assuming I am missing some sort of scale / offset that I need to apply first. When I add my calculated height to the size of the grid the ui is either overlapping or, at a different resolution, there is a huge gap between the ui and the grid.

If anyone could lend some advice, that would be most appreciated.

Thanks!

Hello there, I’m facing the same problem. While I don’t have a solution for you yet, it’s nice to know there’s other tortured souls out there.

My shower thoughts so far have been:

  • Skip the canvas system and build the ui in world space. Let’s say my board is 15x20 units. Add the height of my ui at runtime, let’s say another 4 units, and divide by two to get the orthographic size for the camera. This solution seems a bit dirty… but it would probably work!
  • Have two cameras, one perfectly sized to just the game board and rendered to a texture, the other being the main camera that’s drawn to the screen. Layout the screen on the main camera using the canvas tools, anchoring and all that jazz, including a panel which displays the texture from the other camera (your game board). Again, seems a bit dirty… but it would probably work!

Hopefully someone a bit smarter than me will show up and help us do this the “right” way :slight_smile:

As it turns out, the solution to this problem was much simpler than I expected. There’s a setting on the canvas named “Render Mode”. I changed the value to “World Space” on my UI Canvas, and it did pretty much exactly what you’d expect. See Redirecting to latest version of com.unity.ugui for more info.