I though it would be nice to share this script I wrote called CanvasHelper! The script helps your UI adhere to the safe areas of any device using Unity’s built-in Screen.safeArea. Properly integrating safe areas in your UI makes your game feel native to the hardware, so I recommend using CanvasHelper for all your games.
Even though this post’s title says ‘iPhone X’, CanvasHelper will work on iOS and Android (plenty of Android phones now have safe areas too!) and will work on multiple platforms like phones, tablets (eg. iPad and Android Tablets), and even TV’s (eg. Apple TV).
CanvasHelper needs to be added to every Canvas GameObject in your game, and the script will resize a child RectTransform named “SafeArea” (no space!) to fit Screen.safeArea.
Make sure to set the SafeArea’s anchors to Min(0,0) and Max(1,1)!
hey ,
Adriaan
i implemented the script in the game object i am using unity 2017.3 version and xcode 9.2 . but the still getting the same result of full screen not in safe area
@ATMEthan I use wantedReferenceResolution to scale my interface (depending on the screen size, in other code). You can remove it from the code if you don’t want it.
As for your second comment; the whole purpose of this script is to account for those pixel insets. Unity added Screen.safeArea to their API, and this script sets the size and position of a child object named ‘SafeArea’ to that safe area.
Thanks for the explanation on the reference resolution. That makes sense. And I figured out how to add margins to the SafeArea; during the anchor min and max calculations. I find it strange that the SafeArea for the iPhone X doesn’t include the left and right margins but the layout guidelines warn you not to put content in the margins.(At least content that’s meaningful)
What Unity’s API returns for Screen.safeArea is exactly the safe area (or ‘margin’ in your words) you must adhere to according to Apple. No need to add more margins. Just don’t put any buttons and whatnot outside the SafeArea rect.
I was running into a small issue where the UI out of the SafeArea was getting scaled down, but that was fixed by updating wantedReferenceResolution to match our UI Resolution (we’ve been working in 1080 x 1920).
Yes, you are right. My iPhone X simulator was giving me incorrect safe area values which is why I asked about the margins. Now that I have a physical iPhone X out of the box this scrip worked. Thanks for it!
Great work Adriaan, annoyingly seems script is not working for me.
I added the child RectTransform and added my controls to it ran it up, all good controls appear as they should (albiet with iPhoneX cutout still in the way), but just wanted to make sure i had added them correctly.
Added the script into base ui canvas. run the app, the controls disappear completely. I renabled your debug line and got the following :-
@mannyvw Make your SafeArea rect transform set to “Stretch” in both directions, and make sure the top, left, right and bottom values are all 0… (screenshot attached)