Search Unity

  1. Click here to see what's on sale for the "Best of Super Sale" on the Asset Store
    Dismiss Notice
  2. Good news ✨ We have more Unite Now videos available for you to watch on-demand! Come check them out and ask our experts any questions!
    Dismiss Notice

Pixel perfect background resize possible?

Discussion in '2D' started by Avalin, Oct 6, 2020.

  1. Avalin


    Oct 12, 2018

    I am creating a game that should be playable from multiple resolutions.
    I made an image 1920x1080 pixels, it looks like this:
    (2x upscaled for preview)

    What I get in my (otherwise pixel perfect game) is this:

    (Also 2x upscaled for preview)

    RIP dither.

    I resize the background with this method:

    Code (CSharp):
    1.     void ResizeSpriteToScreen()
    2.     {
    3.         var sr = GetComponent<SpriteRenderer>();
    4.         if (sr == null) return;
    6.         transform.localPosition = new Vector3(0, 0, 0);
    7.         Vector2 offset = new Vector3(0.2F, 0.2F, 0.2F);
    9.         var width = sr.sprite.bounds.size.x;
    10.         var height = sr.sprite.bounds.size.y;
    12.         var worldScreenHeight = Camera.main.orthographicSize * 2.0;
    13.         var worldScreenWidth = worldScreenHeight / Screen.height * Screen.width;
    15.         Vector2 newLocalScale = new Vector2((float) (worldScreenWidth / width), (float) (worldScreenHeight / height));
    16.         transform.localScale = newLocalScale + offset;
    17.     }
    I can fix it to be pixel perfect by adjusting the offset to 1 or 0.5f, but the background will be far too upscaled.
    I have set the background to repeat, point (no filter) and no compression.
    In the sprite editor I adjusted the green line to be just below my dither. As such:

    I have a zoom feature in my game, and I need to keep my background the same size but also fill out the viewport which changes when I zoom.

    Anyone with ideas to crispify the background?

    Attached Files: