首先,我们是通过获取iOS设备的的Device Type,根据特定的型号来判断为是否是刘海屏的,比如如果DeviceType是:iPhone10.3 或 iPhone10.6 或 iPhone11.6,那么就认为是刘海屏的。
我们需要写objective-c接口来获取DeviceType
//IOSGameTools.mm#include <string.h>
#include <sys/types.h>
#include <sys/sysctl.h>
#include <stdlib.h>extend "C" const char* IOSGetDeviceType()
{size_t size;sysctlbyname("hw.machine", NULL, &size, NULL, 0);if(size <= 0)return NULL;char *machine = (char*)malloc(size);if(NULL == machine)return NULL;sysctlbyname("hw.machine", machine, &size, NULL, 0);return machine;}
注:在Unity中如何把objective-c代码打入xcode工程中,
见:Unity与iOS交互(XUPorter的使用)
或见:Unity的IOS导出工程配置工具xcodeapi
Unity的C#中,封装一下接口:
//UnityIOSGameTools.cs#if UNITY_IOS
using UnityEngine;
using System;
using System.Collections;
using System.Runtime.InteropServices;
using System.Text.RegularExpressions;class UnityIOSGameTools
{[DllImport("__Internal")]private static extern string IOSGetDeviceType();private static string UnityIOSGetDeviceType(){return IOSGetDeviceType();}//判断iOS是否是刘海屏public static bool IsIOSBangSreen(){var deviceType = UnityIOSGetDeviceType();bool isLiuhai = false;Match match = Regex.Match(deviceType, @"[^\d]*(\d*)[^\d]*");if(null != match){var version = int.Parse(match.Groups[1].Value);isLiuhai = version >= 11;}return "iPhone10.3" == deviceType || "iPhone10.6" == deviceType || isLiuhai;}
}
#endif
调用:
if(UnityIOSGameTools.IsIOSBangScreen())
{//是刘海屏
}
具体的适配方法,要看工程中用的是NGUI还是uGUI
NGUI的话,可以利用UIWdiget的Anchors,左右贴边往里偏44像素,底部贴边往上偏70像素,顶部贴边往下偏54像素
var w = gameObject.GetComponent<UIWidget>();
w.leftAnchors.absolute += 44;
w.rightAnchors.absolute -= 44;
w.bottomAnchors.absolute += 70;
w.topAnchors.absolute -= 54;
uGUI原理一样,不再赘述
如果你有更好的识别刘海屏的方法,欢迎在评论区留意讨论
附:
iOS设备DeviceType查看:https://support.hockeyapp.net/kb/client-integration-ios-mac-os-x-tvos/ios-device-types
DEVICE TYPE | PRODUCT NAME |
---|---|
iPhone1,1 | iPhone |
iPhone1,2 | iPhone 3G |
iPhone2,1 | iPhone 3GS |
iPhone3,1 | iPhone 4 (GSM) |
iPhone3,3 | iPhone 4 (CDMA) |
iPhone4,1 | iPhone 4S |
iPhone5,1 | iPhone 5 (A1428) |
iPhone5,2 | iPhone 5 (A1429) |
iPhone5,3 | iPhone 5c (A1456/A1532) |
iPhone5,4 | iPhone 5c (A1507/A1516/A1529) |
iPhone6,1 | iPhone 5s (A1433/A1453) |
iPhone6,2 | iPhone 5s (A1457/A1518/A1530) |
iPhone7,1 | iPhone 6 Plus |
iPhone7,2 | iPhone 6 |
iPhone8,1 | iPhone 6s |
iPhone8,2 | iPhone 6s Plus |
iPhone8,4 | iPhone SE |
iPhone9,1 | iPhone 7 (A1660/A1779/A1780) |
iPhone9,2 | iPhone 7 Plus (A1661/A1785/A1786) |
iPhone9,3 | iPhone 7 (A1778) |
iPhone9,4 | iPhone 7 Plus (A1784) |
iPhone10,1 | iPhone 8 (A1863/A1906) |
iPhone10,2 | iPhone 8 Plus (A1864/A1898) |
iPhone10,3 | iPhone X (A1865/A1902) |
iPhone10,4 | iPhone 8 (A1905) |
iPhone10,5 | iPhone 8 Plus (A1897) |
iPhone10,6 | iPhone X (A1901) |
iPad1,1 | iPad |
iPad2,1 | iPad 2 (Wi-Fi) |
iPad2,2 | iPad 2 (GSM) |
iPad2,3 | iPad 2 (CDMA) |
iPad2,4 | iPad 2 (Wi-Fi, revised) |
iPad2,5 | iPad mini (Wi-Fi) |
iPad2,6 | iPad mini (A1454) |
iPad2,7 | iPad mini (A1455) |
iPad3,1 | iPad (3rd gen, Wi-Fi) |
iPad3,2 | iPad (3rd gen, Wi-Fi+LTE Verizon) |
iPad3,3 | iPad (3rd gen, Wi-Fi+LTE AT&T) |
iPad3,4 | iPad (4th gen, Wi-Fi) |
iPad3,5 | iPad (4th gen, A1459) |
iPad3,6 | iPad (4th gen, A1460) |
iPad4,1 | iPad Air (Wi-Fi) |
iPad4,2 | iPad Air (Wi-Fi+LTE) |
iPad4,3 | iPad Air (Rev) |
iPad4,4 | iPad mini 2 (Wi-Fi) |
iPad4,5 | iPad mini 2 (Wi-Fi+LTE) |
iPad4,6 | iPad mini 2 (Rev) |
iPad4,7 | iPad mini 3 (Wi-Fi) |
iPad4,8 | iPad mini 3 (A1600) |
iPad4,9 | iPad mini 3 (A1601) |
iPad5,1 | iPad mini 4 (Wi-Fi) |
iPad5,2 | iPad mini 4 (Wi-Fi+LTE) |
iPad5,3 | iPad Air 2 (Wi-Fi) |
iPad5,4 | iPad Air 2 (Wi-Fi+LTE) |
iPad6,3 | iPad Pro (9.7 inch) (Wi-Fi) |
iPad6,4 | iPad Pro (9.7 inch) (Wi-Fi+LTE) |
iPad6,7 | iPad Pro (12.9 inch, Wi-Fi) |
iPad6,8 | iPad Pro (12.9 inch, Wi-Fi+LTE) |
iPad6,11 | iPad 9.7-Inch 5th Gen (Wi-Fi Only) |
iPad6,12 | iPad 9.7-Inch 5th Gen (Wi-Fi/Cellular) |
iPad7,1 | iPad Pro (12.9 inch, A1670) |
iPad7,2 | iPad Pro (12.9 inch, A18219) |
iPad7,3 | iPad Pro (10.5 inch, A1701) |
iPad7,4 | iPad Pro (10.5 inch, A1709) |
iPad7,5 | iPad (6th gen, A1893) |
iPad7,6 | iPad (6th gen, A1954) |
iPod1,1 | iPod touch |
iPod2,1 | iPod touch (2nd gen) |
iPod3,1 | iPod touch (3rd gen) |
iPod4,1 | iPod touch (4th gen) |
iPod5,1 | iPod touch (5th gen) |
iPod7,1 | iPod touch (6th gen) |
新出的iPhone10s的DeviceType是 iPhone11.6
补充:
后面发现unity可以直接用SystemInfo来判断
using UnityEngine.SystemInfo;if(SystemInfo.deviceModel.Contains("iPhone10,3") || SystemInfo.deviceModel.Contains("iPhone10,6"))
{// 是iOS刘海屏var wr = topLeft.GetComponent<UIWidget>();wr.leftAnchor.absolute += 30;
}