'In Unity, how to detect if window is being resized and if window has stopped resizing

I wanted my UI to not resize when user is still resizing the game (holding click in the window border) and only when the user has released the mouse the resize event will trigger.

I have tried to achieve it on Unity but so far I only able to detect windows size change, which my script checked every 0.5 second and if detected change it will resize the UI. But of course resizing everything caused a heavy lag, so resizing every 0.5 second is not a good option but resizing every 1 second is not a good idea either because 1 second is considered too long.

The question might be too broad but I have specified the problem as small as possible, how do I detect if user is still resizing the window? And how do I detect if user has stopped resizing the window (stop holding click at window border)?



Solution 1:[1]

You can't tell when someone stops dragging a window, unless you want to code a low level solution for ever desktop environment and every operating system.

Here's what worked for me with any MonoBehavior class, using the OnRectTransformDimensionsChange event:

public class StackOverflow : MonoBehaviour
{
    private const float TimeBetweenScreenChangeCalculations = 0.5f;
    private float _lastScreenChangeCalculationTime = 0;

    private void Awake()
    {
        _lastScreenChangeCalculationTime = Time.time;
    }
    
    private void OnRectTransformDimensionsChange()
    {        
        if (Time.time - _lastScreenChangeCalculationTime < TimeBetweenScreenChangeCalculations)
            return;

        _lastScreenChangeCalculationTime = Time.time;
            
        Debug.Log($"Window dimensions changed to {Screen.width}x{Screen.height}");
    }
    
}

Solution 2:[2]

I have some good news - sort of.

When the user resizes the window on Mac or PC,

Unity will AUTOMATICALLY re-layout everything.

BUT in fact ONLY when the user is "finished" resizing the Mac/PC window.

I believe that is what the OP is asking for - so the good news, what the OP is asking for is quite automatic.

However. A huge problem in Unity is Unity does not smoothly resize elements as the user is dragging the mouse to expand the Mac/PC window.

I have never found a solution to that problem. (A poor solution often mentioned is to check the size of the window every frame; that seems to be about the only approach.)

Again interestingly, what the OP mentions

" ..and if window has stopped resizing .."

is automatically done in Unity; in fact do nothing to achieve that.

Solution 3:[3]

I needed something like this for re generating a line chart, but as it has too many elements, it would be heavy to do it on every update, so I came up with this, which for me worked well:

public class ToRunOnResize : MonoBehaviour
{
    private float screenWidth;
    private bool screenStartedResizing = false;
    private int updateCounter = 0;
    private int numberOfUpdatesToRunXFunction = 15; // The number of frames you want your function to run after, (usually 60 per second, so 15 would be .25 seconds)

    void Start()
    {
        screenWidth = Screen.width; // Identifies the screen width
    }

    private void Update()
    {
        if (Screen.width != screenWidth) // This will be run and repeated while you resize your screen
        {
            updateCounter = 0; // This will set 0 on every update, so the counter is reset until you release the resizing.
            screenStartedResizing = true; // This lets the application know when to start counting the # of updates after you stopped resizing.
            screenWidth = Screen.width;
        }

        if (screenStartedResizing)
        {
            updateCounter += 1; // This will count the updates until it gets to the numberOfUpdatesToRunXFunction
        }

        if (updateCounter == numberOfUpdatesToRunXFunction && screenStartedResizing)
        { // Finally we make the counter stop and run the code, in my case I use it for re-rendering a line chart.
            screenStartedResizing = false;
            // my re-rendering code...
            // my re-rendering code...
        }
    }
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 Fattie
Solution 3 halfer