Project 3: Barrel Bouncer AR Edition

Part 7: Designing UIs for Phones

In the past, all phone screens had pretty much the exact same size and shape: a simple, flat rectangle. This meant that any UI elements that were contained within the bounds of the screen’s resolution were guaranteed to be displayed. However, in recent years, we have begun to see a much greater variety of phone form factors that affect the screen, from rounded corners to curved screens to notches to camera cutouts and more. These present challenges to designing UIs since a UI element anchored to a corner or the top edge could be partially cut out on some devices (but also not others). In this section, you will learn about some tools that you can use to fix this issue and to simulate testing of many different device form factors in order to ensure that your app’s UI always displays as you intended.

The Device Simulator

Unity has developed a new tool called the Device Simulator, which allows you to preview how your app will run and look on different mobile devices from right within the Unity Editor (as shown in the image below). It contains a database of some of the most common phones’ specifications and characteristics in order to accomplish this. To learn more about the Device Simulator, check out this blog post and the tool’s documentation.

Screenshot of Unity's Device Simulator

The Device Simulator is currently available as a preview package in the Package Manager. Go ahead and install the latest version (2.2.1 as of this writing). If you need a refresher on how to use the Package Manager, refer to part 1.

Once you have installed it, you can access the Device Simulator by going into the Game View and clicking on the dropdown to switch from Game to Simulator. Then you can select from a variety of different devices to simulate your program running on, including all the latest iPads, iPhones, Samsung Galaxies, Google Pixels, Motorolas, Sonys, and more.

Restricting UIs to the “Safe Area”

The Device Simulator described above provides an easy way to see how your UIs would look on different types of devices and even allows you to highlight the “safe area” of each screen, which is the largest rectangular area of the screen that isn’t compromised by physical features or system gestures. In general, you want to keep your UIs within this safe area.

The images below compare a UI that is restricted within the bounds of the safe area with one that just stretches across the entire screen, both in portrait and landscape orientations of an iPhone X.

Portrait Landscape
Image comparing a UI with safe area vs one with no safe area on iPhone X in portrait orientation Image comparing a UI with safe area vs one with no safe area on iPhone X in landscape orientation

Unfortunately, the Device Simulator itself does not do anything to adjust your UI; it merely helps you with visualization. Instead, Unity leaves it up to you to deal with the logic and modifications to your UI yourself, but does provide access to some helpful properties like Screen.safeArea.

Luckily, someone has already written up a safe area helper script and published it to the Asset Store for anyone to download and use in their own projects. You may obtain it here. You should also check out this article written by its creator, as it goes over three case studies on how to use the tool to achieve a few slightly different effects. Note that the portrait and landscape images above also came from this article.

Creating the UIs for This Scene

First, begin by adding a Canvas into your scene. It should be in Screen Space since it will be placed on your screen over everything else. Additionally, you should use the Canvas Scaler to ensure that your UI displays correctly on all resolutions. For a Reference Resolution, you should stick with a fairly common portrait resolution for modern phones, such as 1080 X 1920.

Next, create two empty child GameObjects of this Canvas and rename them to “SetupScreen” and “GameplayScreen”. They should both be on the UI layer and you should use the anchor presets to stretch their positions across the entire screen. Then you should add the Safe Area component onto both of them. This script will automatically create an offset on the relevant sides so that your UI fits inside of the safe area. Note that these adjustments are only made at runtime, which means you need to click the Play button in your Editor in order to actually see the changes applied to the UI.

Next, modify each of the two screen’s UIs to look like the images below. All UI elements should be child GameObjects of their respective screens and you should make sure to use good anchoring practices and apply appropriate offsets to them.

Setup Screen Gameplay Screen
Image showing setup screen UI Image showing gameplay screen UI

Specifically, your UI should have the following components and functionality:

Bonus Task (Optional)

Currently, there is not much instruction on what users should do in the setup scene, which could leave them confused. The AR Foundation Samples contains a scene called SampleUXScene that plays some animations to guide the user on what they should be doing, as shown in the image below.

GIF of guided setup animations

Since these steps also apply to this app’s setup, go ahead and add these same animations to your SetupScreen. You will need to analyze the sample scene to determine what assets you will need to port over into your project in order to get these guided setup animations to work.

Previous Section | Go Home | Next Section