Skip to content

Instantly share code, notes, and snippets.

@HituziANDO
Created December 6, 2017 03:08
Show Gist options
  • Save HituziANDO/66c42b3cb48317704380cf540a7b739a to your computer and use it in GitHub Desktop.
Save HituziANDO/66c42b3cb48317704380cf540a7b739a to your computer and use it in GitHub Desktop.
[Unity Script] safeAreaInsets of iPhoneX sample code
using UnityEngine;
public struct EdgeInsets {
public float Top { get; }
public float Left { get; }
public float Bottom { get; }
public float Right { get; }
public EdgeInsets(float top, float left, float bottom, float right) {
Top = top;
Left = left;
Bottom = bottom;
Right = right;
}
}
public class UnitySafeArea {
public static Rect safeArea {
get {
// return Screen.safeArea;
#if UNITY_IOS
// for iPhone X
if (Mathf.Min(Screen.width, Screen.height) == 1125 && Mathf.Max(Screen.width, Screen.height) == 2436)
{
if (Screen.width > Screen.height)
{
// Landscape
return new Rect(132, 63, 2172, 1062);
}
else
{
// Portrait
return new Rect(0, 102, 1125, 2202);
}
}
#endif
// for others
return new Rect(0, 0, Screen.width, Screen.height);
}
}
public static EdgeInsets safeAreaInsets {
get {
#if UNITY_IOS
// for iPhone X
if (Mathf.Min(Screen.width, Screen.height) == 1125 && Mathf.Max(Screen.width, Screen.height) == 2436)
{
if (Screen.width > Screen.height)
{
// Landscape
return new EdgeInsets(0, 44, 21, 44);
}
else
{
// Portrait
return new EdgeInsets(44, 0, 34, 0);
}
}
#endif
// for others
return new EdgeInsets(0, 0, 0, 0);
}
}
}
@HituziANDO
Copy link
Author

Unity 2017.2.Op2以上でないと UnityEngine.Screen.safeArea APIは使えないようなのでお手製のスクリプトを書く

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment