Search Unity

Device Background Fit

Discussion in 'Scripting' started by DaRealMik, Jan 22, 2020.

  1. DaRealMik

    DaRealMik

    Joined:
    Jul 27, 2019
    Posts:
    69
    I'm making a game for IOS and Android and am having a hard time trying to make a script that will fit all my elements in the screen. I want my game to run on any device, so this is essential. I have watched a couple Youtube videos, but none work. Please help! (I'm so close to releasing my game so I would really appreciate a quick response, thanks!)
     
  2. DaRealMik

    DaRealMik

    Joined:
    Jul 27, 2019
    Posts:
    69
    Note: I want my game to only be in landscape mode
     
  3. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,697
    You will need to come up with a combination of parenting and anchors that gives you what you need. Luckily you are only landscape, because it gets a lot more ticklish if you want to do landscape and portrait, and sometimes it's easier to make separate screens for those two modes.

    Stuff you want against the left edge, anchor it there. Stuff you want against the right edge, anchor it there. Stuff you want dead center, anchor it dead center and make sure it fits at the narrowest screen, which is probably 4:3 ratio.

    If you want the middle of the screen to shrink and grow, that's possible too, by putting anchors that are at the lateral limits of your edge elements, and possibly some ContentSizeFitter scripts in the middle, but that gets complicated quickly.

    ALTERNATELY, you can put the entire UI hierarchy into a single GameObject, then put an AspectRatioFitter component on that object and set it to Fit Inside Parent at the aspect ratio you currently have the screen laid out with. If you do this, you will have black bars below and above on narrower screens, and black bars on the side on wider screen, and this MAY cause your app to fail certain appstore cehcks.

    Obviously the top option, which requires a bit more work, results in a superior layout experience.
     
    Last edited: Jan 23, 2020
    Joe-Censored likes this.
  4. DaRealMik

    DaRealMik

    Joined:
    Jul 27, 2019
    Posts:
    69
    I'm pretty confused on how to do the top option. I tried anchoring my objects, but it resulted in blank space when I changed the aspect ratio. Can you provide a video or example to help me?
     
  5. Antistone

    Antistone

    Joined:
    Feb 22, 2014
    Posts:
    2,836
    This is a good introduction for making a UI that works with different aspect ratios or resolutions.
     
  6. DaRealMik

    DaRealMik

    Joined:
    Jul 27, 2019
    Posts:
    69
    Thanks for the example! Will check it out and see if I encounter any problems.
     
  7. DaRealMik

    DaRealMik

    Joined:
    Jul 27, 2019
    Posts:
    69
    I tried doing this using the example, and it worked fine for one of my scenes. However, the other scenes didn't. This is how it looked.
    Before changing aspect ratio: upload_2020-1-26_14-38-29.png

    After changing aspect ratio: upload_2020-1-26_14-39-40.png

    It seems like the background doesn't stay in the main camera when the camera changes ratios. I tried moving the background to the canvas, but then the background doesn't appear even when I don't change the ratio. Help!
     

    Attached Files:

  8. DaRealMik

    DaRealMik

    Joined:
    Jul 27, 2019
    Posts:
    69
    I tried to make my canvas scale with screen size, however that didn't work either. Please help!
     
  9. Antistone

    Antistone

    Joined:
    Feb 22, 2014
    Posts:
    2,836
    Your object "BACKGROUND" is a child of your Canvas, but doesn't look like a UI object? Its Transform is not a RectTransform (it has no anchors), and you are using a SpriteRenderer component (from Unity 2D) rather than an Image component (from Unity UI).
     
  10. DaRealMik

    DaRealMik

    Joined:
    Jul 27, 2019
    Posts:
    69
    I tried this, but there's a lot of space that still needs to be filled up.
    upload_2020-1-27_19-22-36.png
     
  11. Antistone

    Antistone

    Joined:
    Feb 22, 2014
    Posts:
    2,836
    Set the anchors and size to fill the parent.
     
  12. DaRealMik

    DaRealMik

    Joined:
    Jul 27, 2019
    Posts:
    69
    It worked! However, now my player is not visible. When I play my game the money prefabs also don't appear.
    upload_2020-1-28_19-48-21.png
     
  13. DaRealMik

    DaRealMik

    Joined:
    Jul 27, 2019
    Posts:
    69
    It seems like the background is being drawn on top of my player and money prefabs. Is there a way to fix this?
     
  14. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,697
    Check into SortingLayers and SortingOrder, or alternatively Camera depth and/or canvas sorting orders and render types. With correct configuration you stack anything however you want.