蘋果的IphoneX新增一個安全區的概念,如下圖
圖中綠色區域爲安全區,所有可交互的組件全部放在安全區內,只有背景可以延伸出去,達到全面屏的效果
我們適配IphoneX採用NGUI通用的錨點去適配。默認的NGUI屏幕適配方案不支持安全區這個概念,我在NGUI每個繼承UIRect的組件下新增一個Bool變量InSafeArea,這個變量如果爲True則表示這個組件是放置在安全區內的,如果爲false則表示這個組件是全屏的,一般爲True,只有背景圖才需要去掉這個勾
原理:
所有的NGUI錨點的適配最終都會調用NGUITools.GetSides這個方法,這個方法實際上是NGUI爲Camera寫的擴展方法,如下,新增了一個inSafeArea參數,備註內是我新增的
static public Vector3[] GetSides (this Camera cam, float depth, Transform relativeTo, bool inSafeArea = true)
{
#if UNITY_4_3 || UNITY_4_5 || UNITY_4_6 || UNITY_4_7
if (cam.isOrthoGraphic)
#else
if (cam.orthographic)
#endif
{
// for iphonex by xiaobai
float xOffset = 1f;
float yOffset = 1f;
#if UNITY_IOS
//iphone x 適配
if (SystemInfo.deviceModel.Contains("iPhone10,3") || SystemInfo.deviceModel.Contains("iPhone10,6") && inSafeArea)
{
xOffset = 734f / 812f;
yOffset = 340f / 375f;
}
#elif UNITY_EDITOR
//測試代碼
if (inSafeArea)
{
Vector2 ssize = screenSize;
if (ssize.x == 1624f && ssize.y == 750f) //自動識別
{
xOffset = 734f / 812f;
yOffset = 340f / 375f;
}
}
#endif
// end for iphonex by xiaobai
float os = cam.orthographicSize;
float x0 = -os * xOffset;
float x1 = os * xOffset;
float y0 = -os * yOffset;
float y1 = os;
Rect rect = cam.rect;
Vector2 size = screenSize;
float aspect = size.x / size.y;
aspect *= rect.width / rect.height;
x0 *= aspect;
x1 *= aspect;
// We want to ignore the scale, as scale doesn't affect the camera's view region in Unity
Transform t = cam.transform;
Quaternion rot = t.rotation;
Vector3 pos = t.position;
mSides[0] = rot * (new Vector3(x0, 0f, depth)) + pos;
mSides[1] = rot * (new Vector3(0f, y1, depth)) + pos;
mSides[2] = rot * (new Vector3(x1, 0f, depth)) + pos;
mSides[3] = rot * (new Vector3(0f, y0, depth)) + pos;
}
else
{
mSides[0] = cam.ViewportToWorldPoint(new Vector3(0f, 0.5f, depth));
mSides[1] = cam.ViewportToWorldPoint(new Vector3(0.5f, 1f, depth));
mSides[2] = cam.ViewportToWorldPoint(new Vector3(1f, 0.5f, depth));
mSides[3] = cam.ViewportToWorldPoint(new Vector3(0.5f, 0f, depth));
}
if (relativeTo != null)
{
for (int i = 0; i < 4; ++i)
mSides[i] = relativeTo.InverseTransformPoint(mSides[i]);
}
return mSides;
}
原理是如果檢測到機型爲iphonex,並且錨點限制在安全區內,就自動將屏幕大小縮小爲安全區大小,否則就全屏。爲了方便測試 在編輯器下,設置分辨率爲1624x750就可以方便的看到iphonex的效果了
這段代碼只展示原理,具體如何去應用,還需要改動UIRect和UIRectEditor的相關方法