├── Landscape └── Assets │ ├── Editor │ └── UnityIPhoneXSupport.cs │ └── StreamingAssets │ ├── left@3x.png │ └── right@3x.png ├── Portrait └── Assets │ ├── Editor │ └── UnityIPhoneXSupport.cs │ └── StreamingAssets │ ├── footer@3x.png │ └── header@3x.png ├── README.md ├── README_img ├── iphonex1.png └── iphonex2.png ├── UnityIPhoneXSupport_Landscape.unitypackage └── UnityIPhoneXSupport_Portrait.unitypackage /Landscape/Assets/Editor/UnityIPhoneXSupport.cs: -------------------------------------------------------------------------------- 1 | #if UNITY_IOS 2 | 3 | using UnityEditor; 4 | using UnityEditor.Callbacks; 5 | using System.IO; 6 | using UnityEditor.iOS.Xcode; 7 | using UnityEngine; 8 | 9 | public class UnityIPhoneXSupport { 10 | 11 | private const float leftSize = 45.0f; 12 | private const float rightSize = 45.0f; 13 | private static readonly Color leftColor = new Color(1.0f, 1.0f, 1.0f, 1.0f); 14 | private static readonly Color rightColor = new Color(1.0f, 1.0f, 1.0f, 1.0f); 15 | private const string leftImage = "left@3x.png"; // Relative path from "Assets/StreamingAssets/" 16 | private const string rightImage = "right@3x.png"; // Relative path from "Assets/StreamingAssets/" 17 | 18 | 19 | [PostProcessBuild] 20 | public static void OnPostProcessBuild(BuildTarget buildTarget, string path) { 21 | if (UnityEditor.PlayerSettings.statusBarHidden) { 22 | var plistPath = Path.Combine(path, "Info.plist"); 23 | var plist = new PlistDocument(); 24 | plist.ReadFromFile(plistPath); 25 | plist.root.SetBoolean("UIStatusBarHidden", false); 26 | plist.WriteToFile(plistPath); 27 | 28 | string viewControllerPath = Path.Combine(path, "Classes/UI/UnityViewControllerBaseiOS.mm"); 29 | if (!File.Exists(viewControllerPath)) { 30 | viewControllerPath = Path.Combine(path, "Classes/UI/UnityViewControllerBase+iOS.mm"); 31 | } 32 | string viewControllerContent = File.ReadAllText(viewControllerPath); 33 | string vcOldText = " return _PrefersStatusBarHidden;"; 34 | string vcNewText = " CGSize size = [UIScreen mainScreen].bounds.size;\n" + 35 | " if (size.width / size.height > 2.15f) {\n" + 36 | " return NO;\n" + 37 | " } else {\n" + 38 | " return YES;\n" + 39 | " }"; 40 | viewControllerContent = viewControllerContent.Replace(vcOldText, vcNewText); 41 | File.WriteAllText(viewControllerPath, viewControllerContent); 42 | } 43 | 44 | string appControllerPath = Path.Combine(path, "Classes/UnityAppController.mm"); 45 | string appControllerContent = File.ReadAllText(appControllerPath); 46 | string acOldText = " _window = [[UIWindow alloc] initWithFrame: [UIScreen mainScreen].bounds];"; 47 | string acNewText = " CGRect rect = [UIScreen mainScreen].bounds;\n" + 48 | " if (rect.size.width / rect.size.height > 2.15f) {{\n" + 49 | " rect.origin.x = {0:0.0#####}f;\n" + 50 | " rect.size.width -= ({0:0.0#####}f + {1:0.0#####}f);\n" + 51 | " _window = [[UIWindow alloc] initWithFrame: rect];\n" + 52 | " UIView *leftView = [[UIView alloc] initWithFrame:CGRectMake(-{0:0.0#####}f, 0, {0:0.0#####}f, rect.size.height)];\n" + 53 | " [leftView setBackgroundColor:[UIColor colorWithRed:{2:0.0#####}f green:{3:0.0#####}f blue:{4:0.0#####}f alpha:{5:0.0#####}f]];\n"; 54 | if (!string.IsNullOrEmpty(leftImage)) { 55 | string leftImageText = " NSString *leftImagePath = [[NSBundle mainBundle] pathForResource:@\"Data/Raw/{0}\" ofType:nil];\n" + 56 | " UIImageView *leftImageView = [[UIImageView alloc] initWithImage:[UIImage imageWithContentsOfFile:leftImagePath]];\n" + 57 | " [leftView addSubview:leftImageView];\n"; 58 | acNewText += string.Format(leftImageText, leftImage); 59 | } 60 | acNewText += " [_window addSubview:leftView];\n" + 61 | " UIView *rightView = [[UIView alloc] initWithFrame:CGRectMake(rect.size.width, 0, rect.size.width, {1:0.0#####}f)];\n" + 62 | " [rightView setBackgroundColor:[UIColor colorWithRed:{6:0.0#####}f green:{7:0.0#####}f blue:{8:0.0#####}f alpha:{9:0.0#####}f]];\n"; 63 | if (!string.IsNullOrEmpty(rightImage)) { 64 | string rightImageText = " NSString *rightImagePath = [[NSBundle mainBundle] pathForResource:@\"Data/Raw/{0}\" ofType:nil];\n" + 65 | " UIImageView *rightImageView = [[UIImageView alloc] initWithImage:[UIImage imageWithContentsOfFile:rightImagePath]];\n" + 66 | " [rightView addSubview:rightImageView];\n"; 67 | acNewText += string.Format(rightImageText, rightImage); 68 | } 69 | acNewText += " [_window addSubview:rightView];\n" + 70 | " }} else {{\n" + 71 | " _window = [[UIWindow alloc] initWithFrame: rect];\n" + 72 | " }}\n"; 73 | acNewText = string.Format(acNewText, leftSize, rightSize, 74 | leftColor.r, leftColor.g, leftColor.b, leftColor.a, 75 | rightColor.r, rightColor.g, rightColor.b, rightColor.a); 76 | appControllerContent = appControllerContent.Replace(acOldText, acNewText); 77 | File.WriteAllText(appControllerPath, appControllerContent); 78 | 79 | string viewPath = Path.Combine(path, "Classes/UI/UnityView.mm"); 80 | string viewContent = File.ReadAllText(viewPath); 81 | string vOldText = " CGRect frame = [UIScreen mainScreen].bounds;"; 82 | string vNewText = " CGRect frame = [UIScreen mainScreen].bounds;\n" + 83 | " if (frame.size.width / frame.size.height > 2.15f) {{\n" + 84 | " frame.size.width -= {0:0.0#####}f;\n" + 85 | " }}"; 86 | vNewText = string.Format(vNewText, leftSize + rightSize); 87 | viewContent = viewContent.Replace(vOldText, vNewText); 88 | File.WriteAllText(viewPath, viewContent); 89 | } 90 | } 91 | 92 | #endif 93 | -------------------------------------------------------------------------------- /Landscape/Assets/StreamingAssets/left@3x.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tkyaji/UnityIPhoneXSupport/7bda3134d4c9b364ebd44bbe03c9a10d5affc53b/Landscape/Assets/StreamingAssets/left@3x.png -------------------------------------------------------------------------------- /Landscape/Assets/StreamingAssets/right@3x.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tkyaji/UnityIPhoneXSupport/7bda3134d4c9b364ebd44bbe03c9a10d5affc53b/Landscape/Assets/StreamingAssets/right@3x.png -------------------------------------------------------------------------------- /Portrait/Assets/Editor/UnityIPhoneXSupport.cs: -------------------------------------------------------------------------------- 1 | #if UNITY_IOS 2 | 3 | using UnityEditor; 4 | using UnityEditor.Callbacks; 5 | using System.IO; 6 | using UnityEditor.iOS.Xcode; 7 | using UnityEngine; 8 | 9 | public class UnityIPhoneXSupport { 10 | 11 | private const float headerSize = 50.0f; 12 | private const float footerSize = 40.0f; 13 | private static readonly Color headerColor = new Color(1.0f, 1.0f, 1.0f, 1.0f); 14 | private static readonly Color footerColor = new Color(1.0f, 1.0f, 1.0f, 1.0f); 15 | private const string headerImage = "header@3x.png"; // Relative path from "Assets/StreamingAssets/" 16 | private const string footerImage = "footer@3x.png"; // Relative path from "Assets/StreamingAssets/" 17 | 18 | 19 | [PostProcessBuild] 20 | public static void OnPostProcessBuild(BuildTarget buildTarget, string path) { 21 | if (UnityEditor.PlayerSettings.statusBarHidden) { 22 | var plistPath = Path.Combine(path, "Info.plist"); 23 | var plist = new PlistDocument(); 24 | plist.ReadFromFile(plistPath); 25 | plist.root.SetBoolean("UIStatusBarHidden", false); 26 | plist.WriteToFile(plistPath); 27 | 28 | string viewControllerPath = Path.Combine(path, "Classes/UI/UnityViewControllerBaseiOS.mm"); 29 | if (!File.Exists(viewControllerPath)) { 30 | viewControllerPath = Path.Combine(path, "Classes/UI/UnityViewControllerBase+iOS.mm"); 31 | } 32 | string viewControllerContent = File.ReadAllText(viewControllerPath); 33 | string vcOldText = " return _PrefersStatusBarHidden;"; 34 | string vcNewText = " CGSize size = [UIScreen mainScreen].bounds.size;\n" + 35 | " if (size.height / size.width > 2.15f) {\n" + 36 | " return NO;\n" + 37 | " } else {\n" + 38 | " return YES;\n" + 39 | " }"; 40 | viewControllerContent = viewControllerContent.Replace(vcOldText, vcNewText); 41 | File.WriteAllText(viewControllerPath, viewControllerContent); 42 | } 43 | 44 | string appControllerPath = Path.Combine(path, "Classes/UnityAppController.mm"); 45 | string appControllerContent = File.ReadAllText(appControllerPath); 46 | string acOldText = " _window = [[UIWindow alloc] initWithFrame: [UIScreen mainScreen].bounds];"; 47 | string acNewText = " CGRect rect = [UIScreen mainScreen].bounds;\n" + 48 | " if (rect.size.height / rect.size.width > 2.15f) {{\n" + 49 | " rect.origin.y = {0:0.0#####}f;\n" + 50 | " rect.size.height -= ({0:0.0#####}f + {1:0.0#####}f);\n" + 51 | " _window = [[UIWindow alloc] initWithFrame: rect];\n" + 52 | " UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, -{0:0.0#####}f, rect.size.width, {0:0.0#####}f)];\n" + 53 | " [headerView setBackgroundColor:[UIColor colorWithRed:{2:0.0#####}f green:{3:0.0#####}f blue:{4:0.0#####}f alpha:{5:0.0#####}f]];\n"; 54 | if (!string.IsNullOrEmpty(headerImage)) { 55 | string headerImageText = " NSString *headerImagePath = [[NSBundle mainBundle] pathForResource:@\"Data/Raw/{0}\" ofType:nil];\n" + 56 | " UIImageView *headerImageView = [[UIImageView alloc] initWithImage:[UIImage imageWithContentsOfFile:headerImagePath]];\n" + 57 | " [headerView addSubview:headerImageView];\n"; 58 | acNewText += string.Format(headerImageText, headerImage); 59 | } 60 | acNewText += " [_window addSubview:headerView];\n" + 61 | " UIView *footerView = [[UIView alloc] initWithFrame:CGRectMake(0, rect.size.height, rect.size.width, {1:0.0#####}f)];\n" + 62 | " [footerView setBackgroundColor:[UIColor colorWithRed:{6:0.0#####}f green:{7:0.0#####}f blue:{8:0.0#####}f alpha:{9:0.0#####}f]];\n"; 63 | if (!string.IsNullOrEmpty(footerImage)) { 64 | string footerImageText = " NSString *footerImagePath = [[NSBundle mainBundle] pathForResource:@\"Data/Raw/{0}\" ofType:nil];\n" + 65 | " UIImageView *footerImageView = [[UIImageView alloc] initWithImage:[UIImage imageWithContentsOfFile:footerImagePath]];\n" + 66 | " [footerView addSubview:footerImageView];\n"; 67 | acNewText += string.Format(footerImageText, footerImage); 68 | } 69 | acNewText += " [_window addSubview:footerView];\n" + 70 | " }} else {{\n" + 71 | " _window = [[UIWindow alloc] initWithFrame: rect];\n" + 72 | " }}\n"; 73 | acNewText = string.Format(acNewText, headerSize, footerSize, 74 | headerColor.r, headerColor.g, headerColor.b, headerColor.a, 75 | footerColor.r, footerColor.g, footerColor.b, footerColor.a); 76 | appControllerContent = appControllerContent.Replace(acOldText, acNewText); 77 | File.WriteAllText(appControllerPath, appControllerContent); 78 | 79 | string viewPath = Path.Combine(path, "Classes/UI/UnityView.mm"); 80 | string viewContent = File.ReadAllText(viewPath); 81 | string vOldText = " CGRect frame = [UIScreen mainScreen].bounds;"; 82 | string vNewText = " CGRect frame = [UIScreen mainScreen].bounds;\n" + 83 | " if (frame.size.height / frame.size.width > 2.15f) {{\n" + 84 | " frame.size.height -= {0:0.0#####}f;\n" + 85 | " }}"; 86 | vNewText = string.Format(vNewText, headerSize + footerSize); 87 | viewContent = viewContent.Replace(vOldText, vNewText); 88 | File.WriteAllText(viewPath, viewContent); 89 | } 90 | } 91 | 92 | #endif 93 | -------------------------------------------------------------------------------- /Portrait/Assets/StreamingAssets/footer@3x.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tkyaji/UnityIPhoneXSupport/7bda3134d4c9b364ebd44bbe03c9a10d5affc53b/Portrait/Assets/StreamingAssets/footer@3x.png -------------------------------------------------------------------------------- /Portrait/Assets/StreamingAssets/header@3x.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tkyaji/UnityIPhoneXSupport/7bda3134d4c9b364ebd44bbe03c9a10d5affc53b/Portrait/Assets/StreamingAssets/header@3x.png -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # UnityIPhoneXSupport 2 | 3 | ## Overview 4 | 5 | Resize the drawing area of Unity and make margins above and below. 6 | You can set an image or a single color in the margin. 7 | 8 | | Before | After | 9 | |:---:|:---:| 10 | | ![Before](https://github.com/tkyaji/UnityIPhoneXSupport/blob/master/README_img/iphonex1.png?raw=true) | ![After](https://github.com/tkyaji/UnityIPhoneXSupport/blob/master/README_img/iphonex2.png?raw=true) | 11 | 12 | 13 | ## Usage 14 | 15 | Import to `UnityIPhoneXSupport_Portrait.unitypackage` or `UnityIPhoneXSupport_Landscape.unitypackage` 16 | 17 | 18 | ## Settings 19 | 20 | On the variables in `UnityIPhoneXSupport.cs`, you can make the following settings. 21 | 22 | ### headerSize, footerSize / leftSize, rightSize 23 | Sets the size of the margin. 24 | 25 | ### headerColor, footerColor / leftColor, rightColor 26 | Set the color of the margin. 27 | 28 | ### headerImage, footerImage / leftImage, rightImage 29 | Set the image to display in the margin. 30 | If you do not want to display the image, leave it empty. 31 | Images must be under "StreamingAssets" Directory. 32 | -------------------------------------------------------------------------------- /README_img/iphonex1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tkyaji/UnityIPhoneXSupport/7bda3134d4c9b364ebd44bbe03c9a10d5affc53b/README_img/iphonex1.png -------------------------------------------------------------------------------- /README_img/iphonex2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tkyaji/UnityIPhoneXSupport/7bda3134d4c9b364ebd44bbe03c9a10d5affc53b/README_img/iphonex2.png -------------------------------------------------------------------------------- /UnityIPhoneXSupport_Landscape.unitypackage: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tkyaji/UnityIPhoneXSupport/7bda3134d4c9b364ebd44bbe03c9a10d5affc53b/UnityIPhoneXSupport_Landscape.unitypackage -------------------------------------------------------------------------------- /UnityIPhoneXSupport_Portrait.unitypackage: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tkyaji/UnityIPhoneXSupport/7bda3134d4c9b364ebd44bbe03c9a10d5affc53b/UnityIPhoneXSupport_Portrait.unitypackage --------------------------------------------------------------------------------