Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
// Derived from Packer's AutoResolution: https://itch.io/jam/lowrezjam2016/topic/19276/unity-setup-request
//Created by Glenn Bermingham
//Modified by Anton (@feefyefowfum)
using UnityEngine;
public class AutoResolution : MonoBehaviour
{
private int height = 512; // Reused Screen Height
//Debugging
//public Text Test;
private void Update()
{
//if(Screen.height != 64 &&
// Screen.height != 128 &&
// Screen.height != 256 &&
// Screen.height != 512 &&
// Screen.height != 1024 &&
// Screen.height != 2048)
//{
// //print("Not Power of Two: " + Screen.height);
// this.AutoScreenSize();
//}
if(!IsPowerOfTwo((ulong)Screen.height) || Screen.width != Screen.height)
{
this.AutoScreenSize();
}
}
private void AutoScreenSize()
{
//print(Screen.height);
if(Screen.fullScreen == false)
{
height = Screen.height;
}
else
{
height = Screen.currentResolution.height;
}
//Debugging
//Test.text = "Screen: " + h;
while(height >= 64)
{
//if(height == 64 || height == 128 || height == 256 || height == 512 || height == 1024 || height == 2048)
//{
// print(height);
// Screen.SetResolution(height, height, Screen.fullScreen);
// return; //Found nearest screensize, we're done
//}
if(IsPowerOfTwo((ulong)height))
{
//print(height);
Screen.SetResolution(height, height, Screen.fullScreen);
return; //Found nearest screensize, we're done
}
height--;
}
}
public static bool IsPowerOfTwo(ulong x)
{
return (x != 0) && ((x & (x - 1)) == 0);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment