I want to resize the window size while maintaining the aspect ratio in Unity Wndows

As the title says, I want to resize the window while maintaining the aspect ratio in a resizable window.
I created a GameObject with the following code attached, but when I drag the window, the size changes are not smooth, and it stutters.
My settings are as follows. Any advice would be greatly appreciated.
Thank you.

        int width = Screen.width;
        int height = Screen.height;

        if (lastWidth != width)
        {
            float heightAccordingToWidth = (float)width / 16.0 * 9.0;
            Screen.SetResolution(width, (int)Mathf.Round(heightAccordingToWidth), false, 0);
        }
        else if (lastHeight != height)
        {
            float widthAccordingToHeight = (float)height / 9.0 * 16.0;
            Screen.SetResolution((int)Mathf.Round(widthAccordingToHeight), height, false, 0);
        }

        lastWidth = width;
        lastHeight = height;

9464471--1329761--upload_2023-11-11_13-42-56.png

9464471--1329764--upload_2023-11-11_13-43-14.png

Unfortunately we do not have window size enforcement feature in Unity. As you discovered, calling SetResolution every time the window size is different than what you expect results in a terrible end user experience so I would highly recommend not doing it.