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

前端 未结 6 1259
梦毁少年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();
    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

提交回复
热议问题