- Home /
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, Marcus
Answer by Scribe · Apr 13, 2014 at 10:24 PM
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).
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)
Copy those .dll files into your assets folder of your project.
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)
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.
Scribe
Thanks Scribe :) I'm going to look at your method - but I wonder if there's any copyright issues in including $$anonymous$$S dlls in a distributable game?
I've also proved (4.3.4), that Screen.* deals with the size of the Unity window, and NOT the user's screen resolution. I've yet to find a method that actually alters the user's screen resolution. This is beco$$anonymous$$g a real pain >.<
Some Important changes to the above instructions:
$$anonymous$$y first answer will work fine in the Editor but will fail on build as you need to also include System.Configuration.
I've found it is also better to get all these dlls from the unity package itself to make sure the are all compatible. On windows you can find them in:
[Program Files or Program Files (x86)]\Unity\Editor\Data\$$anonymous$$ono\lib\mono\2.0
I would suggest getting the Drawing and Windows.Forms dlls from here as well.
On build you will still get some warnings '...not supported on StandaloneWindows platform. Various failures might follow.' however at least for finding the resolution I have had no failures, I have only tried it on a Windows system so I'm not sure if an OSX build will work etc.
Reagrding Setting Resolution:
You may wish to checkout this project: http://www.codeproject.com/Articles/6810/Dynamic-Screen-Resolution
Disclaimer, I have not downloaded the project as it does require a free sign-up but everywhere I have looked about setting the resolution seems to suggest this project.
The problem I can see with all of this is that, unless you know very specifically which operating system you are building for, it is likely to be difficult to make it work on them all, as usually Unity handles that part for you. It appears that in the project I linked they use user32.dll which I feel most likely only works on Windows.
It may save you a lot of time to just force whatever you are building to be fullscreen so that you can ue the unity Screen resolution stuff.
Good luck!
Your answer
Follow this Question
Related Questions
Set Resolution game like Desktop Resolution not possible? 1 Answer
pixel in unit in unity3d and have a full screen texture 1 Answer
Fit GUIText in every resolution 3 Answers
print(Screen.currentResolution); not working 1 Answer
Screen.SetResolution causes screen to turn black (and other problems). Am I doing it right ? 1 Answer