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
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Pixel Perfect while Scale With Screen Size

Discussion in 'UGUI & TextMesh Pro' started by Natrem, Sep 16, 2015.

  1. Natrem

    Natrem

    Joined:
    Feb 24, 2015
    Posts:
    31
    Hello!

    Is there a way to make the "Pixel Perfect" mode of a root canvas set to "Scale With Screen Size" work as if it was set to "Constant Pixel Size"?

    I explain: I want the behavior of "Scale With Screen Size" (having elements that keep 100% of the width of the parent and still keep the same ratio width/height without using additional scripts) but I also want that same element (or a child) to have a border of 1 real pixel (through an outline or a sprite border) independently of the window resolution.

    Also I wonder if it is a bug that enabling "Pixel Perfect" has an effect only with "Constant Pixel Size".

    Thanks.
    Unity 5.1.3f1
     
  2. runevision

    runevision

    Joined:
    Nov 28, 2007
    Posts:
    1,877
    This is not the regular behaviour, but with some simple scripting (a few lines) you can get this result by setting the referencePixelsPerUnit property of the CanvasScaler to compensate for the Canvas scaleFactor.

    Something like referencePixelsPerUnit = 100 / canvas.scaleFactor;

    Hope that helps!
     
  3. Natrem

    Natrem

    Joined:
    Feb 24, 2015
    Posts:
    31
    Thank you for your reply!

    In fact modifying the scale is the problem, and compensating with the referencePixelsPerUnit doesn't work.
    With the root canvas scale set to 1, and 100 referencePixelsPerUnit, the child Image (with a 1px border of a sliced Sprite) looks pixel perfect:
    upload_2015-9-17_15-14-46.png
    With the root canvas scale set to 2, referencePixelsPerUnit set to 50:
    upload_2015-9-17_15-13-55.png
    With the root canvas scale set to 2, referencePixelsPerUnit set to 50.1:
    upload_2015-9-17_15-14-12.png
    These results were obtained inside the Editor (Game tab during Play). All values are round.
    Components of root canvas:
    upload_2015-9-17_15-15-19.png
    RectTransform of immediate Image child:
    upload_2015-9-17_15-16-10.png
    The Sprite asset has 100 Pixels Per Unit.
     
  4. runevision

    runevision

    Joined:
    Nov 28, 2007
    Posts:
    1,877
    Well, I assume it's better than not using it? E.g. using Canvas scale factor of 2 while keeping referencePixelPerUnit at 100 I'd expect your lines would be twice as thick as they should be.

    The fact they they still don't align perfectly is unfortunate though. Maybe it's a bug. The best option is to file a bug report including repro project and reproduction steps.