How can I change the user's desktop resolution with Unity?

This is a question I’ve been having for a long time now and, so far, nobody has been able to answer it.
I want my game to run at a specific resolution and I want to change the user’s desktop/monitor resolution accordingly.

For a clear example, let’s suppose I want my game to run at 800x600, regardless of the fact the user’s monitor is most likely 16:9, just for “teh lolz”.
Unity by default will adjust the render to the user’s current resolution (1920x1080, for example) and kind of scale everything.
Well, I want to change the user’s desktop resolution to 800x600 at the game startup to match my game.

This is the kind of behavior AAA games have. You can easily test it: set your desktop resolution to 1024x768 (for example) and try running some AAA (like Tomb Raider) and you will see that the game will switch the desktop resolution to whatever you choose in-game, like 1920x1080, and when you close the game, it will restore your previous resolution (1024x768 in this case)

I’ve read suggestions about using some methods inside “user32.dll” as demonstrated here: Dynamic Screen Resolution - CodeProject
The problem with this is that:
A) It’s a Microsoft dll so redistributing it with a game might involve some… “legal matters”
B) It only works in Windows. If I want to achieve the same on some other platform, like Linux or OSX, it won’t work.

That’s why I’m asking here if there a way to achieve this with Unity: to easily allow the desktop resolution changing feature to work fine regardles of platform and also to be included with the game with having to worry about the legal matters surrounding it.

Thanks.

What would this be useful for? When you look for behavior like this, it’s mostly perceived as annoying, unwanted and potentially buggy. As far as i’m concerned, you definitely should not be able to control the desktop resolution of the user - at least anything other than the ingame resolution.
You can make a menu to set the ingame resolution, but i dont know why you would have to / want to / should be allowed to meddle with the user desktop settings. And i may be wrong, but i dont think there is any platform-independant way to do this either, unless you count tools where somebody already implemented a solution for all platforms.

Are you sure this is not just a side-effect of setting a higher resolution in game than the monitor is set at? What happens when you try this with Unity? Just as you described it, setting a lower resolution, implementing a menu to set a higher ingame resolution in Unity, then check what happens.

I don’t know if it’s perceived as you claim. I guess it depends on the audience and the game in question.
Take the game “Kono Yo no Hate de Koi wo Utau Shoujo: Yu-No” for Windows, for example (the orignal port from the PC-9801, not the new remake that’s also available on PS4).
This game, being a direct port from the PC-98, also runs at the same resolution: 640x400 (yes, 400, not 480). I’m 100% sure that anyone who wants to play this version instead of the remake, wouldn’t touch the game if it’s not run at that resolution (I’m one of those people). I don’t want it scaled to my monitor resolution, and I don’t want to manually change my resolution to match game for it to run at the resolution it’s supposed to. The game does this by itself and then restores my resolution when I close it.

Now, I noticed you said you don’t know of a way to do this in Unity unless it’s with some kind of tool.
Do you know the name of said tool?

About the last part of your message, let’s say I make a game meant to be run at 1920x1080 but I set my monitor resolution to 1024x768.
When I start the game, it’s scaled to 1024x768.
What happens if I add some button in-game to change the resolution to 1920x1080? Nothing.
Unity never goes higher than the desktop resolution. If I’m running my PC at 1024x768 and I want to play my game at 1920x1080, I have to manually go into Windows resolution changer screen (right click on the desktop) and change my monitor resolution to 1920x1080.

This is, as I said, not needed in AAA games where the game itself just switches you monitor resolution while the game is running and then restores it when you close it.

The reason I posted this question is because of that: I don’t want the user to have to manually change their resolution in the OS to run the game at that resolution.
If I have an option inside my game to select a certain resolution, I want my game to be able to change it temporarily while the game is running and then restore it.

This last example I gave you (about me using 1024x768 but wanting to run a game in 1920x1080) is actually true: although my monitor is a 1080p, 3D Vision Ready one, when I’m using Windows, I set my resolution to 1024x768. And that’s because I’m running Windows 8. While I was running Windows XP, I used 800x600 (same monitor)
The thing is, when I play a game, I run it at 1920x1080 but after I’m done, I want to go back to 1024x768.
This is the reason why I want to switch the user’s resolution instead of that scaling that Unity does.
AAA companies do it so it is achievable and they have been doing it since the Windows 95/98 days. If Unity or one of the addons from the Store can’t do it, then I guess I’ll have to do it another way.

Have you tried using Unity - Scripting API: Screen.SetResolution

Apart from monitor limitations it should get the job done.

Doesn’t work.
For it to even attempt to change the desktop resolution, you have to use the Exclusive Fullscreen argument of the method (for example: Screen.SetResolution(1920, 1080, FullScreenMode.ExclusiveFullScreen) )

A) ExclusiveFullScreen only works on Windows, according to the documentation (Unity - Manual: Standalone Player settings)
B) Even on Windows, it always crashes the game when changing resolution (no matter if the new resolution is lower or higher than the current one). I’ve searched around the web and it seems this problem has been around for quite a long time.

Like everything built-in in Unity, it’s half-assed and barely operational.
I reckon there has to be some third-party tool to achieve this but I can’t find it.
If I can’t find any, I guess I’ll have to go the “OS library method” and see if I can make it work.

Wrong. It’s not available on other platforms because it requires support from the operating system, and neither macOS nor Linux support it at all. That you couldn’t find a third party tool should have been a good hint at this.

In the future when you read the documentation and it says that a feature is not or only available for certain platforms or operating systems, understand that it’s not that they haven’t been implemented but that the feature isn’t supported for those platforms or OSes.

1 Like

Thanks for the explanation Deniii. While i still dont really get why anybody would use a lower resolution and then not want to play a game on that, i guess it’s a corner case that may exist.
When i mentioned a ‘tool’ i didnt talk about anything specific, but some hypothetical tool where somebody already implemented a solution for all platforms. This may or may not exist, since as Ryiah said it depends on whether or not the platforms themselves support it, which is also a likely reason for Unity not having this functionality by default.
Downscaling and / or using windowed mode to get a lower resolution, like in your first example, should work fine. If it’s a special resolution but you do not want to use window mode, you could probably add your own black borders on that resolution to make it fit another aspect ratio, if that’s a problem. I just believe this would get messy sooner or later, and is a kinda niche requirement either way.

Very much untrue. While definitely not flawless, Unity is of pretty high quality. New content (experimental) may still lack documentation or other features (like DOTS currently for example), but that’s still better than not having access to it at all, and gets fixed until its proper release. If anything is missing afterwards, it’s probably not Unitys fault, or is getting worked on.
Either way, nobody forces you to use Unity… so if you dislike it that much simply go look for another engine that better suits your expectations, even tho i doubt any engine will fulfill them.

@Ryiah : I think you misunderstood what I meant when I said what I said.
I wasn’t complaining about the feature not being present on an OS that doesn’t support it. I was talking about the feature not working on an OS where it is supported: the switching crashes the game on Windows, where it is supported.
I did read the documentation, I always do and I find it quite poor, for such a… big, let’s say, product. Compared to Microsoft’s documentation, Unity’s is really lacking.
I’m not saying it has to be as detailed as Microsoft’s but there shouldn’t be missing stuff and things should be better explained.

@Yoreki : It’s true that I should probably look for another engine and it’s also true that there won’t be one that does everything I want the way I want.
I haven’t had good experiences with Unity. In my experience, I wouldn’t call it “high quality”. The reason I’m using it is because it supports C#, which is the language I’m most confortable with. It’s a pity that there aren’t more engines which support it. Althought not an engine, I suppose I could give MonoGame a shot since the game I have in mind right now, isn’t terribly complex but I have made quite a lot of progress on Unity already.
Now I have to decide if I want to stick to Unity or if I want to start over with some other tool.

Are we just talking about the case where people run their monitors at a lower resolution (for some reason) than they could, but want their games (for some reason) to run at a higher resolution than that?
If i dare say so, this is an increadibly niche situation. Engines focus on providing functionality that is frequently used in game development, such as raycasting, rendering, … and so on. Thus, obviously, the focus lies on what the majority needs. It is expected of you to implement more specific features for your game yourself. An Engine is a toolkit to help you speed up development. It doesnt, and cannot, provide every single thing you may or may not need.

So if you need something very specific, then you could always write your own platform dependant library or tool, for each platform you want to support, and then make use of that from within your game code. A bit more effort, but doable if that’s what you require for your game. If a tool or engine to do this does not exist, then that should hint you that it’s not exactly a common request and thus something you may need to tackle yourself if you need it.

While Unity is a pretty popular and beginner friendly engine, if your only reason for using Unity is that you like C#, then you may wanna consider learning another language. Most object oriented languages are increadibly similar, to the point where you can nearly copy and paste code from one to another with minor changes - so that really shouldnt be a reason holding you back if you hate everything about Unity other than the scripting language it uses.