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
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.
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.
you can use this event
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;
}
}
}