Search Unity

Unity UI Get the true size in pixels of image that uses canvas scaler.

Discussion in 'UGUI & TextMesh Pro' started by chris_sarama, Aug 12, 2017.

  1. chris_sarama

    chris_sarama

    Joined:
    Jul 29, 2017
    Posts:
    30
    I wanted to give an update to this thread. I found the definite issue. The problem is in the Unity editor screen scaling is automatically applied if you're using the stand alone mode.

    The issue is I wanted the true size of the image according to it's actual pixel size on screen. The actual pixel size and what's being returned by multiplying the image width by the canvas X scale differ because the Unity editor applies scaling.

    That's why actual image width was 312 but showing as 960 according to the formula. A simple mistake on my behalf but it through me for a loop.
     
    Last edited: Oct 22, 2017
  2. RichardNemeth

    RichardNemeth

    Joined:
    Oct 6, 2017
    Posts:
    1
    Hi, I thinked about the same.

    Here is what I'm using and it's working perfectly:
    Vector2 sizeDelta = GetComponent<RectTransform>().sizeDelta;
    Vector2 canvasScale = new Vector2(canvas.transform.localScale.x, canvas.transform.localScale.y);

    Vector2 finalScale = new Vector2(sizeDelta.x * canvasScale.x, sizeDelta.y * canvasScale.y);

    If the object also have a scale I don't know if it should be multiplied. Hope it helped.
     
  3. chris_sarama

    chris_sarama

    Joined:
    Jul 29, 2017
    Posts:
    30
    I actually figured it out. It has to do with the scale mode setting as well. There are several different options so the formula changes for each one. You have to multiply the x and y by the scale factor and if you have it set to match width/height you need to scale in the percent to whatever the current settings are.

    It's definitely not direct. There should be a more direct way to do this.
     
  4. User340

    User340

    Joined:
    Feb 28, 2007
    Posts:
    3,001
    Multiplying by Canvas.scaleFactor should be all that is needed, bypassing CanvasScaler entirely.
     
  5. Bip901

    Bip901

    Joined:
    May 18, 2017
    Posts:
    71
    Exactly what I was missing to make my tooltip work, thank you!
     
  6. Robert7501

    Robert7501

    Joined:
    Aug 19, 2019
    Posts:
    2
    that is not entirely true, as this works only if the aspect ratio stays the same between reference resolution and the target-display.
    If your application runs on different mobile devices, the aspect ratio is different for many devices and the reported scaling-factor only matches one dimension. Especiall between devices like iPad and something like Pixel3 the aspect ratio differs a lot and causes serious scaling problems, and Im still looking for a good solution to get around this problem.
     
    NelsonChristensen likes this.
  7. chaurasiyapawan

    chaurasiyapawan

    Joined:
    May 8, 2014
    Posts:
    1
    @RichardNemeth This is working fine. Thanks for your reply i really appreciate for you work
     
  8. spiderman20084

    spiderman20084

    Joined:
    Sep 1, 2015
    Posts:
    2
    That did the trick. Thanks dude. I've been searching for hours. :D
     
  9. Azizbek4293

    Azizbek4293

    Joined:
    Jan 31, 2021
    Posts:
    1
    so what is the resulting answer guys?
     
  10. SpicyCatGames

    SpicyCatGames

    Joined:
    Sep 30, 2019
    Posts:
    14
    This will only work if all your anchors are at the same coordinate, if they're not, you'll get an unusable sizedelta value, or even 0 if anchors are at the corners of the rect.
     
  11. samizafar

    samizafar

    Joined:
    Oct 20, 2021
    Posts:
    2
    here you go, this method will give you bounds of UI.

    public static Vector2 GetBoundsOfUI(RectTransform UI)
    {
    Vector2 MaxPoint;
    MaxPoint.x = Mathf.Lerp(0, Camera.main.pixelHeight,UI.anchorMax.x);
    MaxPoint.y = Mathf.Lerp(0, Camera.main.pixelHeight,UI.anchorMax.y);

    Vector2 MinPoint;
    MinPoint.x = Mathf.Lerp(0, Camera.main.pixelHeight,UI.anchorMin.x);
    MinPoint.y = Mathf.Lerp(0, Camera.main.pixelHeight,UI.anchorMin.y);

    return MaxPoint-MinPoint;
    }
     
  12. paper_man86210

    paper_man86210

    Joined:
    Apr 27, 2022
    Posts:
    1
    I think i found a batter way, if you simply want the size in pixel you can get those by RectTranform.rect.size.
    It worked well for me.