- Home /
How to wait for Screen.SetResolution to finish?
Because of technical concerns, I need to run my Unity application in a "fullscreen" borderless windowed mode. The Unity window position and size is red from an external configuration file on start.
...
Screen.fullScreen = false;
Application.runInBackground = true;
Screen.SetResolution (windowWidth, windowHeight, false);
// ** Need to wait for the resolution to be changed here **
SetPosition(windowX, windowY);
...
As you may be aware of, the Screen.SetResolution function takes a few seconds to get its job done. It runs on another thread. The problem with this code is that the window position can't be changed while the resolution is changing. Is there a way to 'block' the function until the resolution has changed?
I was thinking of a line like:
WaitUntil (Screen.GetResolution() == wantedResolution)
Note: The SetPosition() function here calls SetWindowPos from user32.dll
Edit: This is written in C#!
Answer by Eroteme · Jan 27, 2014 at 05:51 PM
I found a quick fix for this issue.
I set my default settings in (File > Build Settings > Player Settings) which makes the code runs faster.
Then, I changed the code for:
Screen.SetResolution (windowWidth, windowHeight, false);
yield return new WaitForSeconds(0.5F);
SetPosition(windowX, windowY);
This works properly; however a cleaner solution would be appreciated.
Yes, this approach works. But I'd like a cleaner solution as well. Lot's of variety in the amount of time it takes a monitor to change resolution. I tried WaitForEndOfFrame(), but that didn't work.
Answer by Kiloblargh · Jan 27, 2014 at 04:42 PM
yield Screen.SetResolution (windowWidth, windowHeight, false);
I assume this is Javascript code; any way to get this to work in C#?
Answer by ThomasKang · Oct 25, 2019 at 07:13 PM
Here's a cleaner fix than waiting for 0.5f seconds.
You can use a coroutine with WaitUntil()
Like so:
void ApplyResolution(Resolution resolution) {
Screen.SetResolution(resolution.width, resolution.height, Screen.fullScreen, targetFPS);
StartCoroutine(_CalculateScreenResolution(resolution));
}
IEnumerator _CalculateScreenResolution(Resolution targetResolution) {
yield return new WaitUntil(() => Screen.width == targetResolution.width
&& Screen.height == targetResolution.height);
scalableCamera.CalculatePixelRes();
}
Your answer
![](https://koobas.hobune.stream/wayback/20220613131701im_/https://answers.unity.com/themes/thub/images/avi.jpg)