- Home /
Is unity able to do what I need?
Heyo. Hopefully this question is in the right place. If not, please move it, because I seriously need help with this. Thanks!
So I got working on a new project that was migrated over from plain C++ to Unity, since this engine is doing everything I needed (except one major thing), and development time is so much better compared to my C++ project. But, with the C++ project, I could do what I needed quite easily with a call to a function named "glViewport()." But with Unity, I'm not sure how I could do this, other than put two random quads above and below my "game area" (and this would seem to me to be a nasty hack that I would like to avoid at al costs...if at all possible...), so I'm asking to get some feedback on whether this engine can do what I need.
Based on my designs, my game needs to have two black bars above and below the "viewport" of the game. I really, really need this to happen, because of the UI I have planned, and the general gameplay depends heavily on this requirement. Failing to meet this requirement would mean redesigning what I had previously come up with.
Here is what I'm talking about: I draw up this picture to describe what I mentioned.
So, even if the window is resized, the game should retain this style of viewport, so it's not anythign to do with aspect ratio "black bars" like other people have asked about.
Does anyone know how this could be done without the two quads hack? Is frustum culling useful for my game's needs? If so, how? This would be the first time I've ever used frustum culling, so I would need to do a lot of research on this, including trial and error, so if anyone has anything on that, I would appreciate it hugely. Or, is there anything in Unity that mimics what "glViewport()" would do in OpenGL?
Answer by Bunny83 · Sep 14, 2021 at 07:59 AM
You can set the viewport of a camera by changing its viewport rect in the inspector. Though this viewport rect is in normalized coordinates. So the offset and height / width is essentially given in "percentage" (0 - 1) of the entire screen. If you want a fix pixel offset, you have to recalculate it from the screen size whenever the window is resized. Note that (0,0) is the bottom left corner while (1,1) is the top right corner.
Since you seem to want an offset from the bottom and an offset from the top, you have to increase the Y offset according to your wanted pixel count and the height (H) of the viewport by twice that amount.
Of course if you actually want a percentage, you could just set the values in the inspector once and it will keep that percentage. Though according to your image you want a fix pixel offset.
Unity also has the pixelRect property which should be the pixel equivalent of the viewport size. Though you still would need to set it when the size changes.
It should be possible to use canvas and layout classes for convenience, then read back the actual screenspace rect of a particular rectTransform and apply that to the camera viewport
Your answer
