3
\$\begingroup\$

When change the framebuffer resolution by calling

Screen.setResolution(1920,1080)

Screen.currentResolution, Screen.width, Screen.height reports back the new resolution 1920x1080.

Screen.resolutions gave me empty array (tested on real android device).

I am unable to get back the original resolution which was a native resolution of the device. Do I have to remember the native resolution or is there a way to get that through unity API ?

\$\endgroup\$
1
\$\begingroup\$

Old question, but it now has a canonical answer.

From a recent QA on SO:

Display.main.systemHeight and Display.main.systemWidth

\$\endgroup\$
0
\$\begingroup\$

If you're testing in the editor, then Screen.resolutions could return an empty array since it's not really running in a screen. If you make a build and then test it, then it should give you the correct resolutions and the highest one is your best bet for a native resolution. Now, the better way of doing this, is by a static variable (assuming you're running C#).

Just make a new script named StaticVariableHolder (or something like that so you can remember it) and it should go something like this:

public class StaticVariableHolder : MonoBehaviour {

 public static Resolution nativeResolution;
 // Use this for initialization
 void Start () 
 {
     DontDestroyOnLoad(gameObject);
     nativeResolution = Screen.currentResolution;
     //print(nativeResolution); //debug line to see if it actually sets the resolution
 }
}

Now, add the script to some object like StaticVariables (as I have done). This object will now persist through ALL scenes because it's not destroyed on level loads (so be sure to put this in one level only, it shouldn't matter if that level is loaded several times but I'm not sure). Anyway, to get this variable later on you need to some line like this:

 Resolution nativeRes = StaticVariableHolder.nativeResolution;

Remember, you have to make sure the object is never lost (where the DontDestroyOnLoad part comes in) and you have to make sure the variable is public and static, otherwise it won't be accessible unless you search for the object (we don't want that because using Find calls in Update calls will be a huge performance hog). enter image description here

\$\endgroup\$
  • \$\begingroup\$ Screen.resolutions turns out empty on actual device. \$\endgroup\$ – Melon Therapy Dec 3 '16 at 12:04
  • \$\begingroup\$ Well, it's a code that's not really reliable. So, like I said, just get the initial resolution and then save it somewhere. After that, set your own resolution and you should be good to go. Also, I haven't seen any other method to get the resolution on a device. If you find anything, please let us know here too ;) \$\endgroup\$ – John Hamilton Dec 3 '16 at 12:05
  • \$\begingroup\$ Yeah thats a problably only way to go now. Thanks for a help. \$\endgroup\$ – Melon Therapy Dec 3 '16 at 12:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for?Browse other questions tagged or ask your own question.