Unity里面如何判断安全区域,避免刘海遮挡

开发环境, Unity, UI使用的是FairyGUI, 在适配刘海的时候, 需要添加适配

简单的方式如下

1. 发布IOS环境如下

1
2
3
4
5
6
7
8
9
10
    void SetScreen(GComponent w){
#if UNITY_IOS
if (Screen.safeArea.height != Screen.height) {
float t = 1.0f * (Screen.height - 100) / Screen.height;
float t1 = 1 - t;
w.SetPosition(0, w.size.y * t1, 0);
w.SetSize(w.size.x, w.size.y * t);
}
#endif
}

如果输出为空或 swap 行为 0,则说明尚未启用 Swap。

2. 发布Android的环境如下

1
2
3
4
5
6
7
8
    void SetScreen(GComponent w){
#if UNITY_ANDROID
float t = 1.0f * Screen.safeArea.height / Screen.height;
float t1 = 1 - t;
w.SetPosition(0, w.size.y * t1, 0);
w.SetSize(w.size.x, w.size.y * t);
#endif
}

注意事项

  • 如何使用了Window对象, 也需要适配的
  • 即使使用Pannal做弹窗,也需要适配