How to add the iPhone X screen dimensions to the mix of different iPhone dimensions in Unity

允我心安 提交于 2019-12-04 22:37:55

As My experience, you don't have to do many things, the gameobject in a scene will just fit well, Unity has done that for you the only thing you maybe need to do is adjust the UI canvas if you are using UGUI, and it pretty easy too, why? because most phones have the similar width/height ratio, and in our team, we do the fix thing like this:

  1. design all the UI stuff based on 1080p(1080x1920) (720p is fine too, but we presume there will be more 1080p device in the future)

  2. attach following scripts to all the canvas to make an auto-fix thing:

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    using UnityEngine.UI;
    
    public class CanvasScreenAutoFix : MonoBehaviour
    {
        public static CanvasScreenAutoFix instance;
    
        private static float DEPEND_W = 1080;
        private static float DEPEND_H = 1920;
    
        public float scaleRatio = 1.0f;
    
    
        private void ResizeCanvas()
        {
            int screenW = Screen.width;
            int screenH = Screen.height;
            if (DEPEND_W == screenW && DEPEND_H == screenH)
            {
                scaleRatio = 1.0f;
                return;
            }
            else
            {
                float W_scale = screenW / (float)DEPEND_W;
                float H_scale = screenH / (float)DEPEND_H;
                float scale = W_scale < H_scale ? W_scale : H_scale;
    
                GetComponent<CanvasScaler>().scaleFactor = scale;
    
                scaleRatio = scale;
            }
        }
    
        private void Awake()
        {
            ResizeCanvas();
            if(instance == null)
           {
               instance = this;
           }
        }
    
    }
    

And the result turns to be pretty well, actually, we don't have to care about the scale things with the iPhone Family anymore after this(there may be some issue with iphone4 cause they have a different aspect ratio, but as there a rarely that devices anymore ...).

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!