r/Unity3D • u/chickenheadedhat • 3d ago
Solved How to create in-game borders?
I'm making a retro style game that I want to have a consistent screen ratio but also accommodate people who have different screen sizes.
I've seen other unity games that had customizable screen borders as a solution for this.
In the past I've played with the viewport rect of the camera in order to change its in game width, but I was never sure how to put the customized borders on there.
How does one implement that?

2
Upvotes
1
u/DenPixelDev 3d ago
id render the game camera into a render texture and show it on a canvas with a RawImage + AspectRatioFitter (fit in parent), then you dont need the viewport rect at all. the border is just a full screen image behind it, swap it whenever you want. and since the hud is on the same full screen canvas you can put stuff on the border too, like the coin counter in your screenshot
1
u/FoxsterDev 3d ago
You can keep using the camera's viewport rect for the game view and put the border artwork on a Screen Space - Overlay canvas. Add UI Images around the viewport, then update their edges from the same aspect-ratio calculation you use for the camera rect. Update both when the screen size changes, including cases where the empty space is above and below the game view.
If the game UI needs to stay inside the viewport, you could use a separate Screen Space - Camera canvas assigned to the game camera. Test the layout at both wider and taller aspect ratios.
1
u/Federal-Row2373 3d ago
Another option that avoids UI entirely: use two cameras. Put a second camera behind your game camera (lower Depth) that only renders the border art, as a sprite or a quad that's scaled to fill the screen. Then your game camera keeps the viewport rect and draws on top of it. The border art then shows up in exactly the area the viewport rect leaves empty, so you don't have to calculate where the edges go.
If it's pixel art, also look at the Pixel Perfect Camera component. Its crop frame options keep the aspect ratio and you can get clean integer scaling. Just make sure the background camera's Clear Flags are set up so it fills the bars (Solid Color or the border sprite), otherwise you'll get black or garbage in the empty space.