Skip to content

Instantly share code, notes, and snippets.

@yoko57822
Last active November 28, 2022 10:33
Show Gist options
  • Save yoko57822/ca82d75e2238592b4fb950ad4cf4959a to your computer and use it in GitHub Desktop.
Save yoko57822/ca82d75e2238592b4fb950ad4cf4959a to your computer and use it in GitHub Desktop.
Unity Screen Setting
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using TMPro;
using UnityEngine.SceneManagement;
public class ScreenSettings : MonoBehaviour
{
static bool isFullScreen = true;
public Image IsFullScreenImage;
FullScreenMode Screen_Mode;
List<Resolution> resolutions = new List<Resolution>();
[SerializeField] Vector3[] res_list = new Vector3[30];
[SerializeField] TMP_Dropdown resolutionsDropdown;
int if_x, if_y;
bool if_bool;
[SerializeField] int resolutionNum;
// Start is called before the first frame update
private void Awake()
{
if (Screen.fullScreenMode == FullScreenMode.MaximizedWindow)
{
IsFullScreenImage.gameObject.SetActive(true);
isFullScreen = true;
}
else if(Screen.fullScreenMode == FullScreenMode.Windowed)
{
IsFullScreenImage.gameObject.SetActive(false);
isFullScreen = false;
}
}
void Start()
{
Set_Res();
}
void Set_Res()
{
resolutions.AddRange(Screen.resolutions);
int optionNum = 0;
var r = 0;
Vector3 resolution_check = new Vector3(0, 0, 0);
foreach (Resolution item in resolutions)
{
var MAX = GCD(item.width, item.height);
if (MAX != 0 && item.refreshRate >= 60)
if ((((item.width / MAX == 16) || (item.width / MAX == 8)) || (item.width / MAX == if_x)) && (((item.height / MAX == 9) || (item.height / MAX == 5)) || (item.height / MAX == if_y)))
{
//Debug.Log(item.width + "x" + item.height + " " + item.refreshRate);
res_list[r++] = new Vector3(item.width, item.height, item.refreshRate);
if (resolution_check == new Vector3(0, 0, 0))
resolution_check = new Vector3(item.width, item.height, item.refreshRate);
else if ((item.width == resolution_check.x && item.height == resolution_check.y) && item.refreshRate != resolution_check.z)
r--;
resolution_check = new Vector3(item.width, item.height, item.refreshRate);
if ((resolution_check.x == Screen.width && resolution_check.y == Screen.height) && if_bool != true)
{
if_bool = true;
}
}
}
if (!if_bool)
{
PlayerPrefs.SetInt("Screen_Ratio_X", Screen.width);
PlayerPrefs.SetInt("Screen_Ratio_Y", Screen.height);
}
if_x = PlayerPrefs.GetInt("Screen_Ratio_X", 0);
if_y = PlayerPrefs.GetInt("Screen_Ratio_Y", 0);
resolutionsDropdown.options.Clear();
foreach (Resolution item in resolutions)
{
var MAX = GCD(item.width, item.height);
if (MAX != 0 && item.refreshRate >= 60)
if ((((item.width / MAX == 16) || (item.width / MAX == 8)) || (item.width / MAX == if_x)) && (((item.height / MAX == 9) || (item.height / MAX == 5)) || (item.height / MAX == if_y)))
{
TMP_Dropdown.OptionData option = new TMP_Dropdown.OptionData();
option.text = item.width + "x" + item.height;
resolutionsDropdown.options.Add(option);
if (resolution_check == new Vector3(0, 0, 0))
resolution_check = new Vector3(item.width, item.height, item.refreshRate);
else if ((item.width == resolution_check.x && item.height == resolution_check.y) && item.refreshRate != resolution_check.z)
resolutionsDropdown.options.Remove(option);
resolution_check = new Vector3(item.width, item.height, item.refreshRate);
if (item.width == Screen.width && item.height == Screen.height)
resolutionsDropdown.value = optionNum;
optionNum++;
}
}
if (if_x != 0 && if_y != 0)
{
res_list[r++] = new Vector3(if_x, if_y, 60);
TMP_Dropdown.OptionData option = new TMP_Dropdown.OptionData();
option.text = if_x + "x" + if_y;
resolutionsDropdown.options.Add(option);
if (Screen.currentResolution.width == if_x && Screen.currentResolution.height == if_y)
resolutionsDropdown.value = resolutionsDropdown.options.Count;
}
resolutionsDropdown.RefreshShownValue();
}
public void FullScreenOnOff()
{
if (isFullScreen)
{
isFullScreen = false;
FullScrrenCheck(isFullScreen);
IsFullScreenImage.gameObject.SetActive(false);
Change_Resolution();
}
else
{
isFullScreen = true;
FullScrrenCheck(isFullScreen);
IsFullScreenImage.gameObject.SetActive(true);
Change_Resolution();
}
}
public void DropboxoptionChange(int x)
{
resolutionNum = x;
}
int GCD(int x, int y)
{
if (y == 0)
{
return x;
}
int r = x % y;
return GCD(y, r);
}
public void Change_Resolution()
{
Screen.SetResolution((int)res_list[resolutionNum].x,
(int)res_list[resolutionNum].y,
Screen_Mode);
PlayerPrefs.SetInt("Screen_X", (int)res_list[resolutionNum].x);
PlayerPrefs.SetInt("Screen_Y", (int)res_list[resolutionNum].y);
}
void FullScrrenCheck(bool Full)
{
if (Full)
{
Screen_Mode = FullScreenMode.ExclusiveFullScreen;
// Screen_Mode = FullScreenMode.MaximizedWindow;
}
else
{
Screen_Mode = FullScreenMode.Windowed;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment