Screen.resolutions problem

For some reason, Screen.resolutions is not giving me the correct list of resolutions.

Resolution[] resolutions = Screen.resolutions;

Debug.Log(resolutions.Length.ToString());
foreach(Resolution res in resolutions)
{
	Debug.Log(res.width.ToString() + " x " + res.height.ToString());
}

Resolution curRes = Screen.currentResolution;
Debug.Log(curRes.width.ToString() + " x " + curRes.height.ToString());

Given the above snippet of code, I get the following log:

1
640 x 480
1366 x 768

Basically, it’s saying that my monitor only supports a single resolution which is 640x480. Now that’s just stupid. The only way that can be true is if my computer is actually a relic from the olden days and that the list of resolutions shown in the Display options is merely an optical illusion. The third line is printing out the correct current resolution of my monitor though.

I’ve already exited and re-opened the program just to get that possible solution out of the way, but I still keep getting those values. Any thoughts?

Where do you try this code? Because it seems the resolution array always contains just 640x480 in the editor. When you create a build it should work.

I guess it’s a bug. Some related posts:

http://forum.unity3d.com/threads/61936-Unity-3.0-Screen.currentResolution-in-editor


edit

If you want to test it do something like:

using UnityEngine;
using System.Collections;

public class ResolutionTest : MonoBehaviour
{
    Rect winRect;

    void OnGUI ()
    {
        winRect = GUILayout.Window(0,winRect,OnWindow,"Resolutions");
    }
    
    void OnWindow(int ID)
    {
        GUI.DragWindow();
        GUILayout.BeginVertical();
        foreach (Resolution R in Screen.resolutions)
        {
            GUILayout.Label("Res: "+ R.width + " / "+ R.height,GUILayout.MinWidth(200));
        }
        GUILayout.EndVertical();
    }
}

Try this :)) (javascript )

var CalculatePositionForNewScreen:boolean=false;
var CalculateScaleForNewScreen:boolean=false;
var StartResolutions:Vector2;
var OffsetX:float;
var OffsetY:float;
var FindOffsetStart:boolean;
private var Can:boolean;


function Start () {
if(FindOffsetStart){
FindOffset();
}else{
Can=true;
}
}
private var XFaz:float;
private var YFaz:float;

function Update () {
var Widht=GetComponent.<GUITexture>().pixelInset.width ;
var Height=GetComponent.<GUITexture>().pixelInset.height;

if(Can){

if(OffsetX==0){
XFaz=((Widht-20)/2);
}else{
XFaz=0;
}
if(OffsetY==0){
YFaz=((Height-20)/2);
}else{
YFaz=0;
}
GetComponent.<GUITexture>().pixelInset.x=((Screen.width)/2)-XFaz-OffsetX;

GetComponent.<GUITexture>().pixelInset.y=((Screen.height)/2)-YFaz-OffsetY;
CalculateOffset();
}
}



function FindOffset () {
var Widht=GetComponent.<GUITexture>().pixelInset.width ;
var Height=GetComponent.<GUITexture>().pixelInset.height;

OffsetX= ((Screen.width)/2)-GetComponent.<GUITexture>().pixelInset.x;
OffsetY= ((Screen.height)/2)-GetComponent.<GUITexture>().pixelInset.y;

yield WaitForSeconds(0.1);
CalculateOffset();

}


function CalculateOffset() {
var Widht=GetComponent.<GUITexture>().pixelInset.width;
var Height=GetComponent.<GUITexture>().pixelInset.height;
var Xk=(StartResolutions.x)/Screen.width;
var Yk=(StartResolutions.y)/Screen.height;
if(CalculatePositionForNewScreen){
OffsetX=OffsetX/Xk;
OffsetY=OffsetY/Yk;

yield WaitForSeconds(0.001);
StartResolutions.x=Screen.width;
StartResolutions.y=Screen.height;
}
Can=true;
if(CalculateScaleForNewScreen){
GetComponent.<GUITexture>().pixelInset.width/=Xk;
GetComponent.<GUITexture>().pixelInset.height/=Yk;
}

}