How to make a Horizontal Slider slide between resolutions?

I just cant imagine how to do it. What I would like, is a horizontal slider, that you can slide with to choose resolution. Then you click a button and it will apply what you have chosen. Any ideas? I’ve look at docs and googled something similar, but my mind just cant imagine it. Any help appreciated, thanks in advance. Last resort is to use a ton of buttons, but that’s just… weird… lol.

3 Answers

3

Building up on tbkn’s answer. You can get all the available resolutions wwith Screen.resolutions (an array of type Resolution). You can then create a variable that is modified by the HorizontalSlider and is used as an index to point values in the Screen.resolutions array:

var resolutionPointer:int=0;

function OnGUI(){
  resolutionPointer=GUI.HorizontalSlider(sliderRect,resolutionPointer,0,Screen.resolutions.length);
  if(GUI.Button(buttonRect,"Apply")
    Screen.SetResolution(Screen.SetResolution(Screen.resolutions[resolutionPointer].width,Screen.resolutions[resolutionPointer].height,true); ,true);      
}

What seems to be the error? Edit: Whoops. I meant Screen.SetResolution(Screen.resolutions[resolutionPointer].width,Screen.resolutions[resolutionPointer].height,true);

I fixed most of the errors, now the last one is (176,25): BCE0023: No appropriate version of 'UnityEngine.Screen.SetResolution' for the argument list '(UnityEngine.Resolution, boolean)' was found. Error is in Screen.SetResolution(Screen.resolutions[resolutionPointer],true);

Okay I figured it out. It works, except it makes it full screen when switching. What I was thinking, is sliding between 19 differnet resolutions. How would I go about that? Making it so if it's at 1, it says 640480 in a label, then if it slides one more time to 2, it says 720480, then on and on till 19. Sorry for so much trouble with me, thanks in advance.

Make the third argument to SetResolution be false instead of true if you don't want it to go fullscreen. For your label, just do GUI.Label(labelRect,Screen.resolutions[resolutionPointer].width+"x"+Screen.resolutions[resolutionPointer].height);

That will do for now, thank you so much for your time!

x= Number Of Resolution

Create a GUI slider just like you would any other say for volume

I dont know how to make an option for resoltion but Im sure you can find out how on here.

But I would assign a value to each resolution
exmple
480p = 1 ;
720p = 2 ;
1080p = 3 ;

Im pretty sure the slider will snap to 1 2 3 based on how you write the var

First, to find out how to change resolutions, read here:

Second, you just have to create a GUI slider component:

Now all you have to do is connect the two. In a script, create a slider, and give it min value 0 and max value equal the number of resolution options you have. Now in that script, check the value of the slider, rounded down to the latest whole number. When the player clicks apply, change resolution based on that number.

Sample code:

public class ResolutionSelector : MonoBehaviour {
	public float hSliderValue = 0.0F;
	public int[,] resolutions = { {640, 480}, {1280, 1024}, {1920, 1080} }
	
	void OnGUI() {
		hSliderValue = GUI.HorizontalSlider(new Rect(25, 25, 100, 30), hSliderValue, 0.0F, 3.0f);
		if (GUI.Button(new Rect(25, 70, 100, 30), "Apply")) {
			int resolutionIndex = Mathf.FloorToInt(hSliderValue);
			Screen.SetResolution(resolutions[resolutionIndex, 0], resolutions[resolutionIndex, 1], true);
		}
	}
}