Search Unity

About 2D Sprite Backgrounds, Resolutions....

Discussion in '2D' started by hedgeh0g, Feb 21, 2017.

  1. hedgeh0g

    hedgeh0g

    Joined:
    Jul 18, 2014
    Posts:
    102
    Hello folks,

    I'm builing a shmup and my artist made me a background 1280x1024 px. So I created a "Background" gameobject with a Sprite Renderer component and put this background on. But, it doesn't fill the entire scene.

    Now, given that my camera is at Z -5 and my actors are at Z +5, I set the Background to Z +6. Now this is definitely the reason why the background is not filling the scene entirely. But still, 1280x1024 is awfully big for a 2D game. What am I doing wrong?
    • Since I don't know exactly about how Unity's rendering order works, if I put my background at, say Z 0 but in a lower layer than my actors, am I overdrawing? Putting the background at Z 0 will help me filling the scene, but I don't feel like it is the correct solution.
    • Do I need some coding trick to adapt the background to the resolution the player chooses? If so, could you suggest me an example?
    I have already watched Unity's tutorials on 2D games, but they didn't help with my problem. :\
    Thanks in advance.
     
  2. LiterallyJeff

    LiterallyJeff

    Joined:
    Jan 21, 2015
    Posts:
    2,807
    If you're going to be using a Perspective camera, then depth will affect render size. If you want your background to be physically behind everything in the scene, and also take up the full screen, you will need to scale it up.

    You should consider an orthographic camera for a more standard 2D experience.

    For perspective cameras, there are a few options:

    If you're good at math, you can calculate the scale required for any sprite to be native size on screen.

    If you don't mind hacky solutions, you can use Camera.main.WorldToScreenPoint to find the min and max position of your spriteRenderer bounds to get the min and max positions of your object on screen, and then scale it to a target size. (currentSize / targetSize = scale needed to be target sized)

    There's also assets like Pixel Perfect that can do that for you.

    https://www.assetstore.unity3d.com/en/#!/content/14498