- Home /
How do you save a player's prefered resolution in Unity 2017.1.1f?
This is a C# script and I only coding this game in this language. I will learn to do Js after this project.
Hello, I have a dropdown UI element which will load all supported resolutions the computer supports. Once loaded in code it will then add options to the dropdown UI element. Now once you select a resolution it will change it to whatever it is currently to what you selected. Now lets say the player closed the game out once the resolution has changed ( and here is where I want it to save their selected resolution ). And here where the problems lies, Once they reboot the game the resolution changes back to what it had by default not what they changed it to.
I will have the full screen & resolution portions of my code revealed to you to make sure that the fullscreen coding isn't causing issues either (saving and loading my full screen settings is working fine but my saving and loading resolution isn't). If you wish to #region to organize the code then do so (If you have no idea what #region then please scroll down to my reference's to help you out)
If you need visual help then download my Unity game below this line of text.
Windows : https://drive.google.com/open?id=0B2_VFeFsfP8zUlVQSUtqZG5kTFU
Linux : https://drive.google.com/open?id=0B2_VFeFsfP8zaUJvdHpPbnZ2MFU
Mac : https://drive.google.com/open?id=0B2_VFeFsfP8zdnhjOFF0Q2kzdE0
Here is my code which that I been working on to make saving & loading resolution work
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using System.IO;
public class SettingsCode1 : MonoBehaviour
{
public Dropdown ResoluctionDropdownSource;
public Toggle FullToggleSouce;
Resolution[] resolutions;
void Start ()
{
resolutions = Screen.resolutions;
for (int i = 0; i < resolutions.Length; i++)
{
ResoluctionDropdownSource.options.Add(new Dropdown.OptionData(ResToString (resolutions [i])));
ResoluctionDropdownSource.value = i;
}
if (PlayerPrefs.GetInt ("StoredFullBool") == 1)
{
FullToggleSouce.isOn = true;
}
if (PlayerPrefs.GetInt ("StoredFullBool") == 0)
{
FullToggleSouce.isOn = false;
}
OnResoluctionChange();
}
string ResToString(Resolution res)
{
return res.width + "x" + res.height + "@" + res.refreshRate;
}
public void OnResoluctionChange()
{
Screen.SetResolution (resolutions [ResoluctionDropdownSource.value].width, resolutions [ResoluctionDropdownSource.value].height, Screen.fullScreen);
}
public void UpdateFullscreen()
{
if (FullToggleSouce.isOn == true)
{
PlayerPrefs.SetInt ("StoredFullBool", 1);
print ("Going Full");
Screen.SetResolution (resolutions [0].width, resolutions [0].height, true);
}
if (FullToggleSouce.isOn == false)
{
PlayerPrefs.SetInt ("StoredFullBool", 0);
print ("Going small");
Screen.SetResolution (resolutions [0].width, resolutions [0].height, false);
}
}
This is what the program should look like when you boot it up.
I have been looking at these reference's on unity scripting doc for help on how to make these lines of code work.
Dropdown : https://docs.unity3d.com/ScriptReference/UI.Dropdown.html
Resoluction : https://docs.unity3d.com/ScriptReference/Resolution.html
Screen : https://docs.unity3d.com/ScriptReference/Screen.html
PlayerPrefs : https://docs.unity3d.com/ScriptReference/PlayerPrefs.html
Toggle.IsOn : https://docs.unity3d.com/ScriptReference/UI.Toggle-isOn.html
String : https://docs.unity3d.com/ScriptReference/String.html
region : https://forum.unity.com/threads/unityscript-support-for-region.96203/
Thank you for reading this. I thank you if you can help me with this question. I hope you have a nice day.
Your answer
Follow this Question
Related Questions
Saving and Loading Various Things 1 Answer
Save/Load Animation State of Instantiated Prefabs 0 Answers
webgl saving. 1 Answer
Save and Load Prefab and Mesh 1 Answer