Hyluss

[Unity] Change resolution in game

Oct 15th, 2017
418
0
Never
1
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.47 KB | None | 0 0
  1. // This script change resolution in game.
  2. // Create a list to reject the same resolutions which "Screen.resolutions" gets.
  3. // Do not use PlayerPrefs on void OnEnable or Start because when we link other monitor it is possible we get different
  4. // resolutions and this may appear unwanted problems.
  5.  
  6. using System.Collections;
  7. using System.Collections.Generic;
  8. using UnityEngine;
  9. using UnityEngine.UI;
  10.  
  11. public class ChangeResolutionInGame : MonoBehaviour {
  12.  
  13.     [SerializeField] Button buttonBack;
  14.     [SerializeField] Button buttonApplyResolution;
  15.     [SerializeField] Text textResolution;
  16.  
  17.     List<Resolution> differentResolutionList;
  18.     Resolution[] resolutions;
  19.     int counterCurrentResolution = -1;
  20.  
  21.     void OnEnable() // or void Start()
  22.     {
  23.         differentResolutionList = new List<Resolution>();
  24.         resolutions = Screen.resolutions;
  25.        
  26.         bool different=true;
  27.         for (int i = 0; i < resolutions.Length; i++)
  28.         {
  29.             for (int j = 0; j < differentResolutionList.Count; j++)
  30.             {
  31.                 if (resolutions[i].width == differentResolutionList[j].width && resolutions[i].height ==        differentResolutionList[j].height)
  32.                 {
  33.                     different = false;
  34.                     break;
  35.                 }
  36.                 else
  37.                 {
  38.                     different = true;
  39.                 }
  40.             }
  41.             if (different)
  42.             {
  43.                 differentResolutionList.Add(resolutions[i]);
  44.                 different = false;
  45.             }
  46.         }
  47.         resolutions = differentResolutionList.ToArray();
  48.         buttonApplyResolution.GetComponent<Button>().enabled = false;    
  49.         for (int i = 0; i < resolutions.Length; i++)
  50.         {
  51.             if (resolutions[i].width == Screen.currentResolution.width && resolutions[i].height == Screen.currentResolution.height)
  52.             {
  53.                 Debug.Log(Screen.currentResolution.width);
  54.                 Debug.Log(Screen.currentResolution.height);
  55.                 counterCurrentResolution = i;
  56.             }
  57.             Debug.Log(resolutions[i].width + "x" + resolutions[i].height);
  58.         }
  59.            
  60.         if (counterCurrentResolution == -1)
  61.         {
  62.             Screen.SetResolution(resolutions[0].width, resolutions[0].height, true);
  63.             counterCurrentResolution = 0;
  64.         }
  65.         textResolution.text = Screen.currentResolution.width + " x " + Screen.currentResolution.height;
  66.     }
  67.  
  68.     [ContextMenu("ChangeResolution()")]
  69.     void ChangeResolution()
  70.     {
  71.         Screen.SetResolution(resolutions[1].width, resolutions[1].height, true);
  72.     }
  73.  
  74.     public void ChangeResolutionPlus()
  75.     {
  76.         if (counterCurrentResolution < resolutions.Length-1)
  77.         {
  78.             counterCurrentResolution++;
  79.             textResolution.text = resolutions[counterCurrentResolution].width + "x" + resolutions[counterCurrentResolution].height;
  80.             buttonApplyResolution.enabled = true;
  81.         }
  82.     }
  83.  
  84.     public void ChangeResolutionMinus()
  85.     {
  86.         if (counterCurrentResolution > 0)
  87.         {
  88.             counterCurrentResolution--;
  89.             textResolution.text = resolutions[counterCurrentResolution].width + "x" + resolutions[counterCurrentResolution].height;
  90.             buttonApplyResolution.enabled = true;
  91.         }
  92.     }
  93.  
  94.     public void SetResolution()
  95.     {
  96.         PlayerPrefs.SetInt("Resolution", counterCurrentResolution);
  97.         Debug.Log("Set Resolution to " + resolutions[counterCurrentResolution].width + " x " + resolutions[counterCurrentResolution].height);
  98.         Screen.SetResolution(resolutions[counterCurrentResolution].width, resolutions[counterCurrentResolution].height, true);
  99.     }
  100.  
  101.     void WriteThisFunctionInOtherScriptAndPutItOnStartScene()
  102.     {
  103.         List<Resolution> _differentResolutionList;
  104.         Resolution[] _resolutions;
  105.         _resolutions = Screen.resolutions;
  106.         _differentResolutionList = new List<Resolution>();
  107.         resolutions = Screen.resolutions;
  108.         bool different=true;
  109.         for (int i = 0; i < _resolutions.Length; i++)
  110.         {
  111.             for (int j = 0; j < _differentResolutionList.Count; j++)
  112.             {
  113.                 if (_resolutions[i].width == _differentResolutionList[j].width && _resolutions[i].height == _differentResolutionList[j].height)
  114.                 {
  115.                     different = false;
  116.                     break;
  117.                 }
  118.                 else
  119.                 {
  120.                     different = true;
  121.                 }
  122.             }
  123.             if (different)
  124.             {
  125.                 _differentResolutionList.Add(_resolutions[i]);
  126.                 different = false;
  127.             }
  128.         }
  129.         _resolutions = _differentResolutionList.ToArray();
  130.  
  131.         if (PlayerPrefs.HasKey("Resolution"))
  132.         {
  133.             int currentResolution = PlayerPrefs.GetInt("Resolution");
  134.             Debug.Log("Current resolution: " + _resolutions[currentResolution].width + " x " + _resolutions[currentResolution].height);
  135.             Screen.SetResolution(_resolutions[currentResolution].width, _resolutions[currentResolution].height, true);
  136.         }
  137.         else
  138.         {
  139.             Screen.SetResolution(_resolutions[_resolutions.Length - 1].width, _resolutions[_resolutions.Length - 1].height, true);
  140.             Debug.Log("Current resolution: " + _resolutions[_resolutions.Length - 1].width + " x " + _resolutions[_resolutions.Length - 1].height);
  141.         }
  142.     }
  143.  
  144. }
Advertisement
Comments
  • User was banned
Add Comment
Please, Sign In to add comment