using UnityEngine; public enum ScreenRotateState { Free, LandscapeOnly, PortraitOnly } public enum SimpleDeviceOrientation { Unkown, Portrait, Landscape, } public static class ScreenExtensions { public static bool IsNarrowScreen(){ float height = Screen.height; float width = Screen.width; if(height > width){ return 16.1f / 9.0f < height / width; }else{ return 16.1f / 9.0f < width / height; } } public static bool HasSafeArea(){ float height = Screen.height; float width = Screen.width; if(height > width){ #if UNITY_EDITOR return Screen.height != (int)GetSafeArea().height; #else return Screen.height != (int)Screen.safeArea.height; #endif }else{ #if UNITY_EDITOR return Screen.width != (int)GetSafeArea().width; #else return Screen.width != (int)Screen.safeArea.width; #endif } } public static Rect GetSafeArea(){ #if UNITY_EDITOR if((Screen.width == 1125 && Screen.height == 2436) || (Screen.width == 2436 && Screen.height == 1125)){ var safeArea = Screen.safeArea; if(safeArea.height > safeArea.width){ safeArea.height = 2202; safeArea.y = 102; safeArea.width = 1125; }else{ safeArea.height = 1062; safeArea.y = 63; safeArea.width = 2172; safeArea.x = 132; } return safeArea; }else{ return Screen.safeArea; } #else return Screen.safeArea; #endif } public static int GetSafeAreaTop(){ var safeArea = GetSafeArea(); return (int)((Screen.height - safeArea.height - safeArea.y) / safeArea.height * 1136.0f); } public static int GetSafeAreaBottom(){ var safeArea = GetSafeArea(); return (int)(safeArea.y / safeArea.height * 1136.0f); } public static bool IsiPhoneX(){ return (NativeUtils.getWidth() == 1125 && NativeUtils.getHeight() == 2436) // iPhone X XS || (NativeUtils.getWidth() == 2436 && NativeUtils.getHeight() == 1125) // iPhone X XS || (NativeUtils.getWidth() == 828 && NativeUtils.getHeight() == 1792) // iPhone XR || (NativeUtils.getWidth() == 1792 && NativeUtils.getHeight() == 828) // iPhone XR || (NativeUtils.getWidth() == 1242 && NativeUtils.getHeight() == 2688) // iPhone XSMax || (NativeUtils.getWidth() == 2688 && NativeUtils.getHeight() == 1242); // iPhone XSMax } public static void ChangeScreenRotateState(MonoBehaviour behaviour, ScreenRotateState state){ switch(state){ case ScreenRotateState.Free: Screen.autorotateToLandscapeLeft = true; Screen.autorotateToLandscapeRight = true; Screen.autorotateToPortrait = true; Screen.autorotateToPortraitUpsideDown = true; break; case ScreenRotateState.LandscapeOnly: Screen.autorotateToLandscapeLeft = true; Screen.autorotateToLandscapeRight = true; Screen.autorotateToPortrait = false; Screen.autorotateToPortraitUpsideDown = false; #if UNITY_ANDROID // Portrait時に自動でLandscapeにならないので強制変更してからの自動化 if(Screen.orientation == ScreenOrientation.Portrait || Screen.orientation == ScreenOrientation.PortraitUpsideDown){ Screen.orientation = ScreenOrientation.LandscapeLeft; behaviour.CallWaitForOneFrame(() => Screen.orientation = ScreenOrientation.AutoRotation); } #endif break; case ScreenRotateState.PortraitOnly: Screen.autorotateToLandscapeLeft = false; Screen.autorotateToLandscapeRight = false; Screen.autorotateToPortrait = true; Screen.autorotateToPortraitUpsideDown = true; #if UNITY_ANDROID // Landscape時に自動でPortraitにならないので強制変更してからの自動化 if(Screen.orientation == ScreenOrientation.LandscapeLeft || Screen.orientation == ScreenOrientation.LandscapeRight){ Screen.orientation = ScreenOrientation.Portrait; behaviour.CallWaitForOneFrame(() => Screen.orientation = ScreenOrientation.AutoRotation); } #endif break; } } public static SimpleDeviceOrientation DeviceOrientationToSimpleDeviceOrientation(DeviceOrientation deviceOrientation){ switch(deviceOrientation){ case DeviceOrientation.Portrait: case DeviceOrientation.PortraitUpsideDown: return SimpleDeviceOrientation.Portrait; case DeviceOrientation.LandscapeLeft: case DeviceOrientation.LandscapeRight: return SimpleDeviceOrientation.Landscape; default: return Screen.width < Screen.height ? SimpleDeviceOrientation.Portrait : SimpleDeviceOrientation.Landscape; } } }