- Home /
Force aspect ratio of window (standalone Windows)
Hi,
I am designing an application that is supposed to run exclusively in a 1:1 window. No fullscreen, no different aspect ratio.
I have been searching high and low for the following but cannot seem to find it out:
1/ How do I prevent ALT+Enter from switching to fullscreen?
2/ How do I force the aspect ratio of the window, when the window is resized by the user?
My player options are set so that fullscreen is disabled by default, the window resolution is 800x800 and the window is resizable. I cannot see any built-in option for any of the problems above.
I am guessing (and hoping) it is possible to script a solution (in the window resize event, force the ratio?) but I cannot figure it out.
Is that possible?
Thanks!
Answer by fafase · Aug 02, 2014 at 12:05 PM
You could try something like this:
void Start()
{
Screen.SetResolution (800, 800, false);
}
void Update()
{
if (Screen.fullScreen || Camera.main.aspect != 1)
{
Screen.SetResolution (800, 800, false);
}
}
This is just a start as it will flicker when switching to full mode, but that could get you starting.
Hey, thanks for the reply.
Using your approach I have come up with this:
int lastWidth = Screen.width;
void Update () {
if (Camera.main.aspect != 1) {
if (Screen.width != lastWidth) {
// user is resizing width
Screen.SetResolution(Screen.width, Screen.width, false);
lastWidth = Screen.width;
} else {
// user is resizing height
Screen.SetResolution(Screen.height, Screen.height, false);
}
}
}
Which is close to what I want - the window can be resized but resizing it will keep it square. The window moves in the process, which is not very nice looking. Also I am not sure why, but using this code, hitting ALT+ENTER almost crashes the Windows window manager (some of my other Windows apps crashed). I guess that could be prevented by killing the ALT+ENTER event somehow. I would keep this as a solution if:
a) I was able to kill ALT+ENTER
b) I was able to prevent the window from recentering on screen each time the resolution is set (e.g. if resizing from E or S, it should anchor at NW corner, and vice versa)
Using alt enter makes a few flickerings, I would think it culd be due to the fact that it is happening in Update but the redrawing is done with the refresh rate. So if both are not synchronized, you could end up with multiple calls to SetResolution.
Using an approach that somehow wait and prevent to call many times, it seems to avoid the weird flickering. On the other hand, you get a full screen for a short time:
int lastWidth = Screen.width;
bool isReseting = false;
void LateUpdate () {
if (Camera.main.aspect != 1 && !isReseting) {
if (Screen.width != lastWidth) {
// user is resizing width
StartCoroutine(SetResolution ());
lastWidth = Screen.width;
} else {
// user is resizing height
StartCoroutine(SetResolution ());
}
}
}
IEnumerator SetResolution(){
isReseting = true;
Screen.fullScreen = ! Screen.fullScreen;
Screen.SetResolution(Screen.width, Screen.width, false);
yield return new WaitForSeconds(0.5F);
isReseting = false;
}
Your answer
Follow this Question
Related Questions
How to make standalone player resizable? 2 Answers
RenderTexture contents lost on Standalone Window resize 1 Answer
SetResolution in full screen is a nightmare (Standalone Windows) 1 Answer
Is it possible to create a game build in unity’s runtime? (I’m making something like terror engine) 0 Answers
Standalone Mac build ignores aspect ratio in MacBookPro 15 retina 0 Answers