- Home /
How do i check if the screen/GameWindow has changed size?
is there any event or monobehaviour that is triggered when the game is resized, or even when the game window in the editor is resized? I'd like to make UI elements which can cope with a dynamic screen size
Answer by redeemer · May 18, 2015 at 10:18 PM
I think a quick workaround would be something like this
using UnityEngine;
using System.Collections;
public class ScreenTest : MonoBehaviour {
Resolution res;
// Use this for initialization
void Start () {
res = Screen.currentResolution;
}
// Update is called once per frame
void Update () {
if (res != Screen.currentResolution) {
//Do stuff
res = Screen.currentResolution;
}
}
}
You can't compare Screen.currentResolution, you need to compare the width and height separately.
Not sure why this is a best answer, code doesn't even compile you cannot compare Resolution variables out of the box and .Equals will also not work.
Answer by J3-Gaming · Jan 24, 2018 at 03:57 PM
private Vector2 resolution;
private void Awake()
{
resolution = new Vector2(Screen.width, Screen.height);
}
private void Update ()
{
if (resolution.x != Screen.width || resolution.y != Screen.height)
{
// do your stuff
resolution.x = Screen.width;
resolution.y = Screen.height;
}
}
I am developing an iOS/Android app, and I don't need this code running while on a phone, so consider this:
#if UNITY_EDITOR
private Vector2 resolution;
#endif //UNITY_EDITOR
private void Awake()
{
#if UNITY_EDITOR
resolution = new Vector2(Screen.width, Screen.height);
#endif //UNITY_EDITOR
}
private void Update ()
{
#if UNITY_EDITOR
if (resolution.x != Screen.width || resolution.y != Screen.height)
{
// do stuff
resolution.x = Screen.width;
resolution.y = Screen.height;
}
#endif //UNITY_EDITOR
}
Very late @Nanako hoping to help the next person on google.
You helped me (as the next person from google). Well done!
You are comparing float values directly there! So either use $$anonymous$$athf.Approximately or Vector2Int
why do you convert/store the two values to a Vector2 if than you anyway use the single values again for comparing? you could as well simply use
private int resolutionX; private int resolutionY;
If using
#if UNITY_EDITOR
you should do it on the entire class and not leaving empty Unity messages back likeAwake
,Start
andUpdate
. Reson: If they exist they are called even if they technically do nothing -> causing unnecessary overhead.
So overall it should probably rather be
#if UNITY_EDITOR
private int resolutionX;
private int resolutionY;
private void Awake()
{
resolutionX = Screen.width
resolutionY = Screen.height;
}
private void Update ()
{
if (resolution.x == Screen.width && resolution.y == Screen.height) return;
// do stuff
resolutionX = Screen.width;
resolutionY = Screen.height;
}
#endif //UNITY_EDITOR
Answer by FortisVenaliter · May 18, 2015 at 09:49 PM
Well, with the new UGUI in Unity 5, you can use anchoring which mostly eliminates that problem entirely.
But if that doesn't work for you... I'm not really sure about an event off the top of my head, but you could check the Screen.width and Screen.height variables for changes manually.
anchoring is nice, but i'm doing a layout with variable rows and columns, are there automated tools to help with that?
Answer by unclejey34 · Feb 26, 2020 at 02:07 PM
you can use this event
I've been playing around with OnRectTransformDimensionsChange to detect screen size/orientation changes in both Unity Editor and on my mobile device. What I noticed is that OnRectTransformDimensionsChange does NOT get called when I run my program in Unity Editor and switch between "1280x720 Portrait" and "1280x720 Landscape", which is what I expected to happen. As it turns out, it is because my Canvas had Pixel Perfect disabled. So to ensure that OnRectTransformDimensionsChange detects all screen size/orientation changes, you need to enable Pixel Perfect on your Canvas.
Your answer
Follow this Question
Related Questions
Resolution Dialog missing for Linux? 1 Answer
GUI Resize Mobile 3 Answers
Script for resize aspect ratio for every device (andorid 2D) ?? 0 Answers
how to get Gui in OnGUI function to resize with the window? 1 Answer
Testing orthographic camera at various resolutions -- detect window resize? 1 Answer