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

4 Answers

4

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!

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?

Like [this][1]? [1]: http://docs.unity3d.com/Manual/script-GridLayoutGroup.html

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.

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.