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. Dismiss Notice

Question Scaling with screen size, but then also applying some constant scalar on top of that

Discussion in 'UGUI & TextMesh Pro' started by slims, Mar 21, 2023.

  1. slims

    slims

    Joined:
    Dec 31, 2013
    Posts:
    86
    I'm trying to support UI scaling. For a long time, I've just relied on Scale With Screen Size, with a ref resolution of 1920x1080.

    However, many users want proper UI scaling. I would love a solution that lets you use "Scale with screen size" as the base, then you can apply a constant scalar on top of that. Is there a way to achieve this?

    Otherwise it seems like you'll have to manually write code to figure out good constant scales for all potential resolutions, which I'm guessing the Canvas scalar is already doing under the hood when you are selecting Scale With Screen Size.
     
  2. slims

    slims

    Joined:
    Dec 31, 2013
    Posts:
    86
    One way I solved this was literally copy and pasting the code from the Canvas Scalar and applying my own scalar value to it:


    Code (CSharp):
    1.         var logWidth = Mathf.Log(screenSize.x / 1920, kLogBase);
    2.         var logHeight = Mathf.Log(screenSize.y / 1080, kLogBase);
    3.         var logWeightedAverage = Mathf.Lerp(logWidth, logHeight, .5f);
    4.         var scaleFactor = Mathf.Pow(kLogBase, logWeightedAverage);
    5.  
    6.         canvas.scaleFactor = scaleFactor * scale;
    This seems silly though. This should obviously be built in functionality. Everyone wants UI scaling based on screen size.