Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // This script change resolution in game.
- // Create a list to reject the same resolutions which "Screen.resolutions" gets.
- // Do not use PlayerPrefs on void OnEnable or Start because when we link other monitor it is possible we get different
- // resolutions and this may appear unwanted problems.
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using UnityEngine.UI;
- public class ChangeResolutionInGame : MonoBehaviour {
- [SerializeField] Button buttonBack;
- [SerializeField] Button buttonApplyResolution;
- [SerializeField] Text textResolution;
- List<Resolution> differentResolutionList;
- Resolution[] resolutions;
- int counterCurrentResolution = -1;
- void OnEnable() // or void Start()
- {
- differentResolutionList = new List<Resolution>();
- resolutions = Screen.resolutions;
- bool different=true;
- for (int i = 0; i < resolutions.Length; i++)
- {
- for (int j = 0; j < differentResolutionList.Count; j++)
- {
- if (resolutions[i].width == differentResolutionList[j].width && resolutions[i].height == differentResolutionList[j].height)
- {
- different = false;
- break;
- }
- else
- {
- different = true;
- }
- }
- if (different)
- {
- differentResolutionList.Add(resolutions[i]);
- different = false;
- }
- }
- resolutions = differentResolutionList.ToArray();
- buttonApplyResolution.GetComponent<Button>().enabled = false;
- for (int i = 0; i < resolutions.Length; i++)
- {
- if (resolutions[i].width == Screen.currentResolution.width && resolutions[i].height == Screen.currentResolution.height)
- {
- Debug.Log(Screen.currentResolution.width);
- Debug.Log(Screen.currentResolution.height);
- counterCurrentResolution = i;
- }
- Debug.Log(resolutions[i].width + "x" + resolutions[i].height);
- }
- if (counterCurrentResolution == -1)
- {
- Screen.SetResolution(resolutions[0].width, resolutions[0].height, true);
- counterCurrentResolution = 0;
- }
- textResolution.text = Screen.currentResolution.width + " x " + Screen.currentResolution.height;
- }
- [ContextMenu("ChangeResolution()")]
- void ChangeResolution()
- {
- Screen.SetResolution(resolutions[1].width, resolutions[1].height, true);
- }
- public void ChangeResolutionPlus()
- {
- if (counterCurrentResolution < resolutions.Length-1)
- {
- counterCurrentResolution++;
- textResolution.text = resolutions[counterCurrentResolution].width + "x" + resolutions[counterCurrentResolution].height;
- buttonApplyResolution.enabled = true;
- }
- }
- public void ChangeResolutionMinus()
- {
- if (counterCurrentResolution > 0)
- {
- counterCurrentResolution--;
- textResolution.text = resolutions[counterCurrentResolution].width + "x" + resolutions[counterCurrentResolution].height;
- buttonApplyResolution.enabled = true;
- }
- }
- public void SetResolution()
- {
- PlayerPrefs.SetInt("Resolution", counterCurrentResolution);
- Debug.Log("Set Resolution to " + resolutions[counterCurrentResolution].width + " x " + resolutions[counterCurrentResolution].height);
- Screen.SetResolution(resolutions[counterCurrentResolution].width, resolutions[counterCurrentResolution].height, true);
- }
- void WriteThisFunctionInOtherScriptAndPutItOnStartScene()
- {
- List<Resolution> _differentResolutionList;
- Resolution[] _resolutions;
- _resolutions = Screen.resolutions;
- _differentResolutionList = new List<Resolution>();
- resolutions = Screen.resolutions;
- bool different=true;
- for (int i = 0; i < _resolutions.Length; i++)
- {
- for (int j = 0; j < _differentResolutionList.Count; j++)
- {
- if (_resolutions[i].width == _differentResolutionList[j].width && _resolutions[i].height == _differentResolutionList[j].height)
- {
- different = false;
- break;
- }
- else
- {
- different = true;
- }
- }
- if (different)
- {
- _differentResolutionList.Add(_resolutions[i]);
- different = false;
- }
- }
- _resolutions = _differentResolutionList.ToArray();
- if (PlayerPrefs.HasKey("Resolution"))
- {
- int currentResolution = PlayerPrefs.GetInt("Resolution");
- Debug.Log("Current resolution: " + _resolutions[currentResolution].width + " x " + _resolutions[currentResolution].height);
- Screen.SetResolution(_resolutions[currentResolution].width, _resolutions[currentResolution].height, true);
- }
- else
- {
- Screen.SetResolution(_resolutions[_resolutions.Length - 1].width, _resolutions[_resolutions.Length - 1].height, true);
- Debug.Log("Current resolution: " + _resolutions[_resolutions.Length - 1].width + " x " + _resolutions[_resolutions.Length - 1].height);
- }
- }
- }
Advertisement