IOS刘海屏的适配

一:IPhone

IPhoneX/XS的高宽比:2.165,iphoneXR/XSmax的高宽比:2.164

using UnityEngine;

public class GameAdaptation : MonoBehaviour
{
    public Transform[] uis;

    private void Awake()
    {
        //禁止屏幕锁屏
        Screen.sleepTimeout = SleepTimeout.NeverSleep;

        if (Mathf.Abs((Screen.height * 1.0f / Screen.width) - 2.16f) < 0.01f)
        {
            for (int i = 0; i < uis.Length; i++)
            {
                uis[i].localPosition -= new Vector3(0, Screen.height * 0.035f, 0);
            }
        }
    }
}

二:IPad

IPad的高宽比为1.333333

using UnityEngine;
public class GameAdaptation : MonoBehaviour
{
    public Transform[] uis;
    private void Awake()
    {
        //禁止屏幕锁屏
        Screen.sleepTimeout = SleepTimeout.NeverSleep;
        if (Mathf.Abs(((Screen.height * 1f) / Screen.width) - 1.33f) < 0.1f)
        {
            {
                for (int i = 0; i < uis.Length; i++)
                {
                    uis[i].localPosition -= new Vector3(0, Screen.height * 0.035f, 0);
                }
            }
        }
    }
}

你可能感兴趣的:(Unity开发实战)