Actual Desktop Resolution

I’m trying to get the actual desktop resolution, and failing - apart from a nasty hack. Can anyone shed more light on this?

For LCD monitors, desktop resolution is usually the highest resolution listed in the array from Screen.Resolutions - ie the Native resolution.

This is not the case for older, or non-LCD monitors on which the graphics card can display many resolutions - and the highest possible is not necessarily the current one used by the user.

Now Screen.CurrentResolution gives the current size of the unity window, NOT the size of the user’s screen resolution.

The only reliable method I can find to always get the user’s desktop resolution is to read through the output_log.txt file and parse the line “desktop: width x height” - which to my mind is a horrible hack.

Is there (please there must be) a better solution to get the user’s desktop resolution, so that I can set the Unity resolution to that? This is for a PC build game.

I’ve tried setting FullScreen, but using some debug logging I’ve discovered that although I can set fullscreen, the resolution that Unity has chosen for me is 314 x 212, got by using Screen.CurrentResolution. Despite the desktop (and native) resolution being 1366 x 768.

So for these 2 reasons; using a non-LCD monitor, and Unity picking a silly resolution, I’d like to find the actual desktop resolution that the user is using.

Many thanks,

I think I have found a way,

It does seem that Screen.currentResolution is bugged in the current version (at least) of unity.

Okay so the following is not particularly pleasant however it is not really a hack as it is how it is done in C# but is is not so easy in unity. I also doubt this works from a webplayer (but I have not checked) so it might not be worth your effort if you plan on using this from a webplayer (I explain in point 3 why).

  1. Locate System.Windows.Forms.dll and System.Drawing.dll for .NET version 2.0 either on your system already or download them (I am using windows 8 (:S) atm and found the files in C:/Windows/Microsoft.NET/Framework/v2.0.xxxx)
  2. Copy those .dll files into your assets folder of your project.
  3. Go to Edit > Project Settings > Player and under ‘Other Settings’ find ‘API Compatibility Level’ and change this to ‘.NET 2.0’ not Subset (The reason I don’t think this will work on the webplayer is that I have no option for the webplayer compatibility)
  4. Now that you have done that you should be able to import the libraries into your code and use them without any errors.

For the code you will need to be using the two libraries we copied into your assets folder:

using System.Drawing;
using System.Windows.Forms; 

Then to find the resolution you can do:

System.Windows.Forms.Screen thisScreen = System.Windows.Forms.Screen.PrimaryScreen;
Rectangle resolution = thisScreen.Bounds;
Debug.Log(resolution.Width + "x" + resolution.Height);

In your Start method for example (this is C# code though I hope it would work in Unityscript as well, where you would use the keyword ‘import’ as opposed to ‘using’ and do the other conversions as usual)

I have tested this in the unity editor and it returns the correct resolution when I’ve switched it around several times.