Skip to content

Instantly share code, notes, and snippets.

@syl3n7
Created January 10, 2023 16:24
Show Gist options
  • Save syl3n7/f0c35b0a6603daca5e2469898c0a568e to your computer and use it in GitHub Desktop.
Save syl3n7/f0c35b0a6603daca5e2469898c0a568e to your computer and use it in GitHub Desktop.
redimension unity screen / canvas with 9:16 proportions
using System;
using UnityEngine;
using UnityEngine.UI;
public class Redimension : MonoBehaviour
{
private float lastWidth;
private float lastHeight;
void Start()
{
Screen.SetResolution(1080, 1920, false, 60);
}
void Update()
{
SetRatio(9, 16);
}
void SetRatio(float w, float h) //function to set the ratio of the screen to maintain the aspect of the resolution asked by the teacher (1080x1920)
{
if ((((float)Screen.width) / ((float)Screen.height)) > w / h)
{
Screen.SetResolution((int)(((float)Screen.height) * (w / h)), Screen.height, false);
}
else
{
Screen.SetResolution(Screen.width, (int)(((float)Screen.width) * (h / w)), false);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment