- Home /
Setting Resolutions in game/checking windowed mode
Hey!
So, basically i have a button that lets you go from full screen to windowed, in game.
Problem is that when you go to full screen, the windowed resolution still carries on.
What i want to do is let the user, in a dialog box IN GAME, select from the list of their supported resolutions a WINDOWED mode resolution and another for FULLSCREEN.
How would i go about doing this?
I was trying to do something like this... #pragma strict
var resolutions : Resolution[];
static var selectedFULLres : int;
static var selectedWINres : int;
private var isfullscreen : boolean;
function Start()
{
resolutions = Screen.resolutions;
for (var res in resolutions) {
print(res.width + "x" + res.height);
}
}
function toggleFS()
{
if(!isfullscreen)
{
Screen.SetResolution (resolutions[selectedFULLres].width, resolutions[selectedFULLres].height, true);
isfullscreen = true;
}
if(isfullscreen)
{
Screen.SetResolution (resolutions[selectedWINres].width, resolutions[selectedWINres].height, false);
isfullscreen = false;
}
}
The problem is that... 1. How can i see if the user is in fullscreen or not? I want to get UNITY's answer (like if they had chosen windowed on the launcher) 2. How would i have a list of resolutions, in a visible way, so that the user could click on the desired resolution for each mode?
Also, would i be able to do it this way?
All help is appreciated.
Answer by GusdudeTyr · Jan 09, 2014 at 03:16 AM
I have been treating with similar issues, here is an example code that covers your question, Screen.fullScreen returns true if you're on fullscreen mode, false if you're on windowed mode. Create a new monobehaiour class, paste this code and test it.
using UnityEngine;
using System.Collections;
public class ResolutionSelection : MonoBehaviour {
string showResolution; //Text that will hold the chosen resolution
Resolution[] resolutions;//Variable that holds the different resolutions available
int myResolutionIndex = 0; //Used as the index to scroll between resolution values
void Start () {
//Screen.resolutions takes the supported fullscreen resolution values of the monitor
resolutions = Screen.resolutions;
}
void Update () {
}
void OnGUI() {
GUILayout.BeginArea(new Rect((Screen.width / 2) - (Screen.width / 4), Screen.height / 2 - (Screen.height / 4), Screen.width / 2, Screen.height / 2));
GUILayout.BeginHorizontal();
GUILayout.Label("RESOLUTION");
GUILayout.FlexibleSpace();
if (GUILayout.Button("<")) { showResolution = toggleResolutions(false); }
GUILayout.Label(showResolution);
if (GUILayout.Button(">")) { showResolution = toggleResolutions(true); }
GUILayout.EndHorizontal();
GUILayout.BeginHorizontal();
//Take the value of fullscreen from the Screen class. (This can be used to SET the value too)
GUILayout.Label("Is the game in fullscreen?: " + Screen.fullScreen.ToString());
GUILayout.EndHorizontal();
GUILayout.EndArea();
}
//This function 'scrols' between the different resolution values
private string toggleResolutions(bool direction)
{
if (direction)//If direction = true, the right arrow button was pressed
{
myResolutionIndex++;
if (myResolutionIndex >= resolutions.Length)
{
myResolutionIndex = 0;
}
}
else if (!direction)//If false, the left button was pressed
{
myResolutionIndex--;
if (myResolutionIndex < 0)
{
myResolutionIndex = resolutions.Length - 1;
}
}
//Returns the next or previous value of resolution in the array
return resolutions[myResolutionIndex].width + " x " + resolutions[myResolutionIndex].height;
}
}
Your answer
Follow this Question
Related Questions
Screen.SetResolution doesn't work when the game starts windowed? 0 Answers
Unity Player Crashes ONLY on Firefox after migration 0 Answers
Is it possible to change between exclusive fullscreen and borderless fullscreen in-game? 2 Answers
How do I force a Unity executable to start on a secondary monitor? 0 Answers
Lighting is wrong unless native resolution is used 2 Answers