Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

aspect ratio / resolution script not changing Canvas Rect Transform

Discussion in 'Scripting' started by abj87, Jul 29, 2020.

  1. abj87

    abj87

    Joined:
    May 21, 2013
    Posts:
    11
    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

    Code (CSharp):
    1. Screen.SetResolution(1920, 1080, false);
    2. GameObject.Find("Main Camera").GetComponent<Camera>().aspect = 1920f / 1080f;
    3.  
    4. //toggle
    5.  
    6. Screen.SetResolution(3840, 2160, false);
    7. 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.

     
  2. jvo3dc

    jvo3dc

    Joined:
    Oct 11, 2013
    Posts:
    1,520
    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.
     
  3. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,623
    Please note the UI forums are here.
     
    Last edited: Jul 29, 2020
  4. abj87

    abj87

    Joined:
    May 21, 2013
    Posts:
    11
    But this doesnt work even in built mode

    if I have

    Code (CSharp):
    1. void Update()
    2. {
    3.     if (Input.GetKeyDown(KeyCode.F3))
    4.     {
    5.         Vector2 myCanvasWH = GameObject.Find("Canvas_withVideo").GetComponent<RectTransform>().sizeDelta;
    6.         print("myCanvasWH.x/y = " + myCanvasWH.x + " " + myCanvasWH.y);
    7.  
    8.         string myCanvasWH_str = "myCanvasWH.x/y = " + myCanvasWH.x + " " + myCanvasWH.y;
    9.  
    10.         System.IO.File.WriteAllText(Application.persistentDataPath + "/debugAspectData.txt", myCanvasWH_str);
    11.     }
    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?
     
  5. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,623
    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.
     
  6. abj87

    abj87

    Joined:
    May 21, 2013
    Posts:
    11
    Another thing, when I build and do

    Code (CSharp):
    1.             Screen.SetResolution(3840, 2160, false);
    2.  
    3.             string currRez = "current resolution = " + Screen.currentResolution;
    4.  
    5.             System.IO.File.WriteAllText(Application.persistentDataPath + "/debugCurrRez.txt", currRez);
    it still says that I have a 1920 x 1080p resolution
     
  7. AnthonySharp

    AnthonySharp

    Joined:
    Apr 11, 2017
    Posts:
    88
    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:

    3.png

    Could be wrong though.