Search Unity

Unity UI exception to Canvas Scaler?

Discussion in 'Unity UI (uGUI) & TextMesh Pro' started by DavidBVal, Sep 13, 2019.

  1. DavidBVal

    DavidBVal

    Joined:
    Mar 13, 2017
    Posts:
    82
    I have set up a Canvas Scaler so my whole UI adapts fine to different resolutions. However, there is a certain window that I don't want to "shrink" with the rest of the UI, I want it to remain always with the same absolute size, because it contains small elements that distort badly and it's small enough to fit the screen fine even at the lowest resolution I support.

    Is there a way to make this UI element an "exception" to the canvas scaler?
     
  2. Antistone

    Antistone

    Joined:
    Feb 22, 2014
    Posts:
    1,104
    The canvas scaler works by modifying the scale value of the attached canvas, which is then inherited by all children on that canvas.

    I don't know if there's a built-in way to counter that, but it would be pretty easy to write your own script that sets the localScale of your window to the inverse of its lossyScale. Something along the lines of (not tested):
    Code (CSharp):
    1. void SetGlobalScale(Vector3 targetScale)
    2. {
    3.     Vector3 local = transform.localScale;
    4.     Vector3 global = transform.lossyScale;
    5.     Vector3 desiredLocalScale = new Vector3(
    6.         local.x * targetScale.x / global.x,
    7.         local.y * targetScale.y / global.y,
    8.         local.z * targetScale.z / global.z
    9.     );
    10.     transform.localScale = desiredLocalScale;
    11. }