Building your Unity game to an Android device for testing
Verificado com a versão: 5.3
In this lesson, we’re going to build a sample Unity project to an Android device for testing.
This lesson doesn’t cover building a game for distribution to other devices, or submitting a game to the Google Play Store.
What you will need
To follow this lesson, you will need:
The latest version of Unity (available here)
An Android device running the latest version of Android
The Simple Mobile Placeholder project (available from the Asset Store)
Setting up the Simple Mobile Placeholder project
First, let’s take a quick look at the Simple Mobile Placeholder project. As the name suggests, this is an intentionally minimal project - it was created simply to test the build process. As such, it’s very easy to test that it has built correctly and that it runs as expected on your device.
Open Unity and create a new project.
Name the project "SimpleMobilePlaceholder".
Choose a location to save it to.
Ensure that "3D" is selected to use the Unity Editor in 3D mode.
Click the Create project button to create the project.
Using the top menu, go to Window > Asset Store.
Search for "Simple Mobile Placeholder".
Download and import this project by navigating to the project page and clicking Download then Import.
You will see a warning that importing a complete project will overwrite your project settings. This is fine, as we have just created a blank project. Click Import to continue.
An import window will appear, showing which files are included in the project.
Click Import to import the project.
In the Project window, navigate to Scenes and then double click the Main scene to open it.
You should now see a red cube in the Scene View. Enter Play Mode to test the project. The cube should spin. When you click the cube, it should spin in the opposite direction and a message will appear in the Console window.
That’s it! Now that we know how the project works, let’s get ready to build it to a device.
Setting up the Android SDK Tools
The first thing we need to install is the Java Development Kit (known as the JDK).
Go to the Java site to download the most recent JDK. It’s labelled as "Java Platform (JDK)". Choose the one with the highest version number.
Simply run the installer and follow instructions in the wizard to install it.
Next, we need to install the Android SDK Tools.
Go to the Android Developer site.
Download the Android SDK Tools (also referred to on the site as "the command line tools"), rather than the full download of Android Studio.
Unzip the downloaded file. The resulting directory is the directory that contains the Android SDK Tools.
Put the directory in a memorable, accessible location - you’ll need to tell Unity where this is directory later.
Open the directory that contains the Android SDK Tools, and navigate to tools.
Double click the file called android to run it.
A popup will appear, showing a list of packages that can be installed. By default, the core packages for building and the package for the latest version of the Android OS are selected.
If the device you’re building to runs the latest version of Android, you don’t need to select anything else. If the device you’re building to runs an earlier version of Android, scroll down the list of packages and select the version you need.
Click Install [x] packages to start the installation process. You will be prompted to accept the licenses for these packages.
For each package that you wish to install, select the license on the left, select Accept License, and then click Install to proceed.
A window will appear that shows the progress of the installation. The installation will take some time to complete, so it’s a good time to take a break.
You may see a popup telling advising you to restart the manager window - if this happens, click OK.
When the installation process has completed, close the window.
Preparing your Unity project for building to Android
We now need to return to Unity and switch platforms so that we can build our game for Android.
Within Unity, open the Build Settings from the top menu (File > Build Settings).
Highlight Android from the list of platforms on the left and choose Switch Platform at the bottom of the window.
Switching platforms sets the build target of our current project to Android. This means that when we build, Unity will create an .apk file. Switching platforms also forces Unity to reimport all assets in the project. This doesn’t take long on a small project like this, but be aware that on a larger project this may take some time.
Next, we need to enter the bundle identifier for our game. A bundle identifier is a string that identifies an app. It’s written in what is known as reverse-DNS style, following the format com.yourCompanyName.yourGameName. Allowed characters are alphanumeric characters, periods and hyphens. We need to change the bundle identifier from the default setting in order to build.
There are restrictions on bundle identifiers. When you release an app, its bundle identifier must be unique to your app, and cannot be changed after your app has been published to the Google Play Store. When we build our game for Android, the bundle identifier becomes what is known as the app’s package name. For more information on package names, see this Android documentation.
Open the Player Settings in the Inspector panel (Edit > Project Settings > Player).
Expand the section at the bottom called Other Settings, and enter your chosen bundle identifier where it says Bundle identifier.
Finally, we need to tell Unity where we installed the Android SDK Tools.
Using the top menu, navigate to Unity > Preferences (on OSX) or Edit > Preferences (on Windows).
When the Preferences window opens, navigate to External Tools.
Where it says Android SDK Location, click Browse, navigate to where you put the directory containing Android SDK Tools and click Choose.
Preparing your Android device
Next, we need to enable developer mode on our Android devices - this gives you access to various options related to building and debugging, and lets you test the game on your device.
On your Android device, navigate to Settings > About phone or Settings > About tablet.
Scroll to the bottom and then tap Build number seven times. A popup will appear, confirming that you are now a developer.
Now navigate to Settings > Developer options > Debugging and enable USB debugging.
Building an Android project using Unity
Now we’re ready to build!
Connect your Android device to your computer using a USB cable.
You may see a prompt asking you to confirm that you wish to enable USB debugging on the device. If so, click OK.
In Unity, open the Build Settings from the top menu (File > Build Settings).
Click Add Open Scenes to add the Main scene to the list of scenes that will be built.
Click Build And Run.
You will be prompted to choose where to build your .apk. A good place to do this is in a dedicated builds directory in the root of your project.
Create a directory called "Builds" in the root of your project (not within your Assets directory, but at the same level as it).
Select that directory as the location to build to.
In the text input field marked Save As, enter "Android" and click Save.
Unity will now create an .apk file called "Android" in the “Builds” folder, build that file to your device and run it.
Testing the game on your Android device
The game will now run on the connected device. It works! The cube spins, and tapping it causes it to change direction.
Simply playing your game on a device is one way of testing it. It’s a good way of checking things like whether the controls work. However, if you want more information - if you’d like to see logs from the device OS while you play, for example - you should use an application called Android Device Monitor.
Let’s do this now.
With the game still running on your device, return to your computer.
Navigate to the directory containing the Android SDK Tools.
Navigate to tools and double click the application called monitor.
Android Device Monitor will now open.
If you look to the bottom of Android Monitor, you’ll see the LogCat console. This contains all of the information that you’d see in the Unity console, along with information about other things that are happening in the OS. You can use this in the same way that you use the console within Unity, to help you debug your game.
The LogCat console contains a lot of information, so it can be useful to filter it.
- Type "tag:Unity" in the textbox at the top of the LogCat console, and you’ll only see messages that relate to Unity.
Try tapping the cube on your device and looking at the debug console. You will see the same log message as you would see in Unity, along with some additional information.
Using the LogCat console to get information is just one of the ways that Android Monitor can help you test, profile and debug your game. It’s a complex subject and we won’t cover all of the options here, but this documentation on the Android site is a good start.
In this lesson, we learned how to build a Unity game to an Android device for testing. We learned how to switch build targets in Unity, what a bundle identifier is, build your Unity game to your device for testing, and how to debug your Unity game on your Android device using Android Monitor.