Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Voting for the Unity Awards are OPEN! We’re looking to celebrate creators across games, industry, film, and many more categories. Cast your vote now for all categories
    Dismiss Notice
  3. Dismiss Notice

Getting Camera Viewport Rect in Pixels

Discussion in 'Scripting' started by John-B, May 25, 2018.

  1. John-B

    John-B

    Joined:
    Nov 14, 2009
    Posts:
    1,250
    I have a second camera in a corner of the screen (viewport rect 0.755, 0.75, 0.225, 0.225), and a UI canvas image that acts as a frame around it. The canvas is set to scale with screen size. The problem is that when the screen resolution changes (iPad/iPhones), the camera rect and the image scale differently so the frame no longer fits the camera rect. Everything else in the canvas scales and adjusts properly with changes in resolution.

    I've tried scaling the image so it's a constant size relative to screen size, but that doesn't work. There's no constant scale factor that works at all resolutions. I assume that's because of dynamic resolution scaling (I can't find anything about how that works). Neither does scaling the image based on camera.pixelheight, which does not return the actual pixel size.

    If I could get the actual size of the camera rect in pixels, I could adjust the image frame to that size plus a constant amount. Is there a way to get the actual pixel size of the camera viewport rect, or some other way to match the scale of the image to the viewport rect of the camera?
     
  2. tonemcbride

    tonemcbride

    Joined:
    Sep 7, 2010
    Posts:
    1,077
  3. John-B

    John-B

    Joined:
    Nov 14, 2009
    Posts:
    1,250
    pixelRect is the same as pixelHeight, it doesn't return the correct value. For example, for iPhone X resolution, pixelRect and pixelHeight both return a height of 253, but when I count pixels, the actual height is 240. Depending on the resolution, sometimes the actual height is smaller or larger than the value returned. The pixel position returned by pixelRect is also not correct. Also, it looks like the camera rect and canvas use different resolutions. I have to divide pixelHeight by 2 to get it in the ballpark with the image size.

    Dynamic resolution is off. I've also tried putting the frame graphic in its own canvas, and tried every possible canvas scaling option. The camera rect doesn't appear to be affected by the way the canvas is set to scale, since its always the same. But the camera rect and canvas do not scale the same way. I don't understand this as I thought a camera rect height 0f 0.225 was always the same 22.5% of the screen height, regardless of the screen height/width.

    Here are some values I got with pixelHeight versus actual pixel sizes:

    Screen: 2048x1536 pixelHeight: 346 actual: 352
    Screen: 2208x1242 pixelHeight: 279 actual: 285
    Screen: 1920x1080 pixelHeight: 243 actual: 249
    Screen: 2436x1125 pixelHeight: 253 actual: 240
     
    Last edited: May 26, 2018
  4. tonemcbride

    tonemcbride

    Joined:
    Sep 7, 2010
    Posts:
    1,077
    That's a strange one - sorry I coudn't be more help. When I've used separate cameras in the past I've rendered them to a render texture of a fixed size, that should hopefully mean the camera rect height should always be the same. Have you tried rendering your camera to a render texture?
     
  5. John-B

    John-B

    Joined:
    Nov 14, 2009
    Posts:
    1,250
    I was never able to get the correct position of the camera rect. I solved the problem by putting the UI frame in its own canvas, and setting that canvas to Screen Space - Camera, Constant Pixel Size. That way the UI element is fixed relative to the camera rect and scales with it automatically. The only drawback of doing it this way is that I can't have rounded corners on the frame since the frame is inside the camera rect (not in front of it) and the camera shows through the edges.