aspect ratio / resolution script not changing Canvas Rect Transform

I have a Canvas with UI elements on it. In a script I toggle between 1080p and 4k resolution depending on the video being played on the canvas with video player

so, I have

Screen.SetResolution(1920, 1080, false);
GameObject.Find("Main Camera").GetComponent<Camera>().aspect = 1920f / 1080f;

//toggle

Screen.SetResolution(3840, 2160, false);
GameObject.Find("Main Camera").GetComponent<Camera>().aspect = 3840f / 2160f;

but it doesnt seem to effect the Canvas rect transform until i manually change the circled resolution. I thought setResolution and cam.aspect WAS supposed to be doing that anyway? This is making my script not function correctly when the user changes the resolution of the player as I’m calling the cam.ScreenToWorldPoint to transform some ui position keys

If I manually change the circled aspect ratio at runtime in the editor i get the effect i want, but i want to know how to do this with script. Thanks.

Changing the resolution in the editor does not work the same as in the build. Calling Screen.SetResolution will not work in the editor.

And on a side note, 1920/1080 is the same as 3840/2160 when it comes to the aspect ratio.

Please note the UI forums are here.

But this doesnt work even in built mode

if I have

void Update()
{
    if (Input.GetKeyDown(KeyCode.F3))
    {
        Vector2 myCanvasWH = GameObject.Find("Canvas_withVideo").GetComponent<RectTransform>().sizeDelta;
        print("myCanvasWH.x/y = " + myCanvasWH.x + " " + myCanvasWH.y);

        string myCanvasWH_str = "myCanvasWH.x/y = " + myCanvasWH.x + " " + myCanvasWH.y;

        System.IO.File.WriteAllText(Application.persistentDataPath + "/debugAspectData.txt", myCanvasWH_str);
    }

it will still print / save to file myCanvasWH.x/y = 1920 1080 even after executing Screen.SetResolution(3840, 2160, false);

How can I move the thread?

I apologise as I said this forum is for 2D feature support, it’s actually general scripting. I’ve been moderating a lot of 2D forum posts this morning redirecting them to the correct forums such as scripting and for some reason thought your post was in the 2D forum.

The post is fine here, just wanted to note for the future that there’s a UI forum.

Another thing, when I build and do

            Screen.SetResolution(3840, 2160, false);

            string currRez = "current resolution = " + Screen.currentResolution;

            System.IO.File.WriteAllText(Application.persistentDataPath + "/debugCurrRez.txt", currRez);

it still says that I have a 1920 x 1080p resolution

IIRC, canvas resolution and screen resolution are two different things. Canvas controls its own resolution, which I guess is why it says “Some values driven by Canvas”. Maybe try changing this:

Could be wrong though.