How can I code my game to work on every resolution of Android devices? (with Unity)

前端 未结 6 1250
梦毁少年i
梦毁少年i 2021-01-30 19:12

I have a game what I made in 480x320 resolution (I have set it in the build settings) in Unity. But I would like to publish my game for every Android device with every resolutio

相关标签:
6条回答
  • 2021-01-30 19:19

    I assume it's 2D instead of 3D, this what I do:

    1. Create a canvas object
    2. Set the Canvas Scaler to Scale with Screen Size
    3. Set the Reference Resolution to for example: 480x320
    4. Set the Screen Match Mode to Match with Or Height
    5. Set the match to 1 if your current screen width is smaller, 0 if height is smaller
    6. Create an Image as background inside the Canvas
    7. Add Aspect Ratio Fitter script
    8. Set the Aspect Mode to Fit in Parent (so the UI anchor can be anywhere)
    9. Set the Aspect Ratio to 480/320 = 1.5

    And add this snippet on main Canvas' Awake method:

    var canvasScaler = GetComponent<CanvasScaler>();
    var ratio = Screen.height / (float) Screen.width;
    var rr = canvasScaler.referenceResolution;
    canvasScaler.matchWidthOrHeight = (ratio < rr.x / rr.y) ? 1 : 0;
    

    For 3D objects you can use any of the answers above

    0 讨论(0)
  • 2021-01-30 19:20

    Here is my script for scaling the ortographic camera in 2D games

    public float screenHeight = 1920f;
    public float screenWidth = 1080f;
    public float targetAspect = 9f / 16f;
    public float orthographicSize;
    private Camera mainCamera;
    
    // Use this for initialization
    void Start () {
    
        // Initialize variables
        mainCamera = Camera.main;
        orthographicSize = mainCamera.orthographicSize;
    
        // Calculating ortographic width
        float orthoWidth = orthographicSize / screenHeight * screenWidth;
        // Setting aspect ration
        orthoWidth = orthoWidth / (targetAspect / mainCamera.aspect);
        // Setting Size
        Camera.main.orthographicSize = (orthoWidth / Screen.width * Screen.height);
    }
    
    0 讨论(0)
  • 2021-01-30 19:30

    A easy way to do this is considering your target, I mean if you're doing a game for Iphone 5 then the aspect ratio is 9:16 v or 16:9 h.

       public float targetRatio = 9f/16f; //The aspect ratio you did for the game.
    void Start()
    {
       Camera cam = GetComponent<Camera>();
       cam.aspect = targetRatio;
    } 
    
    0 讨论(0)
  • 2021-01-30 19:35

    The way i did is to change camera viewport according to device aspect ratio Consider you made the game for 800x1280

    The you can do this in any one of the script

    float xFactor = Screen.width / 800f;
    float yFactor = Screen.height  / 1280f;
    
    
    Camera.main.rect=new Rect(0,0,1,xFactor/yFactor);
    

    and this works like magic

    0 讨论(0)
  • 2021-01-30 19:40

    The answer to your question largely depends on how you've implemented the game. If you've created it using GUI textures, then it largely depends on how you've placed/sized your objects versus screen size, which makes things a little tricky.

    If the majority of your game is done using objects (such as planes, cubes, etc) then there's two methods I usually choose to use.

    1) First method is very easy to implement, though doesn't always look too good. You can simply change the camera's aspect ratio to match the one you've designed your game around. So in your case, since you've designed your game at 4:3, you'd do something like this:

    Camera.aspect = 4f/3f;
    

    However, if someone's playing on a screen meant for 16:9, the game will end up looking distorted and stretched.

    2) The second method isn't as easy, requiring quite a bit of work and calculations, but will give a much cleaner looking result for you. If you're using an orthographic camera, one important thing to keep in mind is that regardless of what screen resolution is being used, the orthographic camera keeps the height at a set height and only changes the width. For example, with an orthographic camera at a size of 10, the height will be set to 2. With this in mind what you'd need to do is compensate for the widest possible camera within each level (for example, have a wide background) or dynamically change the Orthographic Size of the camera until its width matches what you've created.

    If you've done a 3d game with a stereoscopic camera , screen resolution shouldn't really affect how it looks, but I guess that depends on the game, so more info would be required

    0 讨论(0)
  • 2021-01-30 19:41

    The best solution for me is to use the theorem of intersecting lines so that there is neither a cut-off on the sides nor a distortion of the game view. That means that you have to step back or forward depending on the different aspect ratio.

    If you like, I have an asset on the Unity asset store which automatically corrects the camera distance so you never have a distortion or a cut off no matter which handheld device you are using.

    0 讨论(0)
提交回复
热议问题