I assume you’re looking for an event or callback you can handle when the user resizes Unity’s window. The bad news is that there isn’t one. Read replies in the following forum thread:
Replies here suggest checking the resolution every frame to see if it has changed. This is inelegant, but as far as I know, there is no built-in event that is raised for this.
using UnityEngine;
public class WindowManager : MonoBehaviour {
public delegate void ScreenSizeChangeEventHandler(int Width, int Height); // Define a delgate for the event
public event ScreenSizeChangeEventHandler ScreenSizeChangeEvent; // Define the Event
protected virtual void OnScreenSizeChange(int Width, int Height) { // Define Function trigger and protect the event for not null;
if (ScreenSizeChangeEvent != null) ScreenSizeChangeEvent(Width, Height);
}
private Vector2 lastScreenSize;
public static WindowManager instance = null; // Singleton for call just one instance
void Awake() {
lastScreenSize = new Vector2(Screen.width,Screen.height);
instance = this; // Singleton instance
}
void Update() {
Vector2 screenSize = new Vector2(Screen.width, Screen.height);
if (this.lastScreenSize != screenSize) {
this.lastScreenSize = screenSize;
OnScreenSizeChange(Screen.width, Screen.height); // Launch the event when the screen size change
}
}
}
2- Associate this script to any basic GameObject : (Main Camera for example)