- Home /
Only allowing windowed mode and 1:1 aspect ratio in my game?
I've been trying to build my game without any fullscreen capabilities. I want this game to only be played in the 1:1 aspect ratio.
When I build my game the game suggests resolutions that are not 1:1. How can I fix this?
Thanks in advance!
Answer by IsaiahKelly · Sep 10, 2017 at 01:12 AM
Change Your Player Settings
Navigate to Edit > Project Settings > Player and just uncheck everything you don't want.
Thank you for the answer but this does not help me.
Answer by Bunny83 · Sep 10, 2017 at 01:54 AM
It's actually better if you do not restrict the different resolutions too much. It might result in worse device support if you try to enforce a certain window mode. It's better to allow any resolution and just "letterbox" your camera so it has a 1:1 ratio.
You can use a simple script like this:
// CameraLetterBox.cs
using UnityEngine;
public class CameraLetterBox : MonoBehaviour
{
public Camera backgroundCam;
public Camera mainCam;
public float targetAspectRatio = 1f;
private void Start()
{
if (mainCam == null)
{
mainCam = Camera.main;
if (mainCam == null)
mainCam = GetComponent<Camera>();
}
if (backgroundCam == null)
{
foreach (var cam in Camera.allCameras)
{
if (cam != mainCam)
{
backgroundCam = cam;
break;
}
}
if (backgroundCam == null)
{
backgroundCam = new GameObject("BackgroundCam").AddComponent<Camera>();
}
backgroundCam.depth = mainCam.depth - 1;
}
}
private void Update()
{
float w = Screen.width;
float h = Screen.height;
float a = w / h;
Rect r;
if (a > targetAspectRatio)
{
float tw = h * targetAspectRatio;
float o = (w - tw) * 0.5f;
r = new Rect(o,0,tw,h);
}
else
{
float th = w / targetAspectRatio;
float o = (h - th) * 0.5f;
r = new Rect(0, o, w, th);
}
mainCam.pixelRect = r;
}
}
This script allows you to specify a desired aspect ratio for the main cam. It will center the main camera rect on the screen and if the screen is too wide it will put a letterbox on both sides. If the screen is too high it will letterbox above and below the cam rect.
Of course when a letterbox is required there will be unused space. That's why there should be another "background" camera which just clears the whole screen before the main cam so the letterbox area get either cleared with a color or an image / texture.
I would allow to resize the window. I personally hate games which force you to a certain resolution or fullscreen setting. That's something the user should decide. Also restricting the display resolution dialog wouldn't be enough. The Unity player supports command line parameters to force certain settings. So if your game requires a certain aspect ratio it's better to inset the desired aspect ratio inside the available space. That way you can ensure to have the correct ratio, no matter the resolution.
Thank you! How would I set up the background camera and main camera?
Well, your main camera is just your usual camera. The background camera probably should have a culling mask of "Nothing" if you just want to clear with a solid color or just render a fullscreen background image.
To draw a background image you can use a script like this on the background camera:
using UnityEngine;
public class BackgroundImage : $$anonymous$$onoBehaviour
{
public Texture2D texture;
void OnPostRender()
{
GL.LoadPixel$$anonymous$$atrix(0, Screen.width, Screen.height, 0);
Graphics.DrawTexture(new Rect(0, 0, Screen.width, Screen.height), texture);
}
}
It will stretch the given texture across the whole screen. It's drawn after the camera it's attached to has finished rendering.
Your answer
Follow this Question
Related Questions
Aspect Ration For 2D Sprites and Canvas 0 Answers
Pixel Perfect in different resolutions 0 Answers
How to build android game with 18.5:9 ratio? 1 Answer
Unity Mobile - Lazy way to scale resolution/aspect-ratio? 0 Answers
Build stuck in 4:3? 0 Answers