dcsimg
October 21, 2018
Hot Topics:

Virtual Reality 101: Building an Android VR App

  • September 26, 2018
  • By Bradley L. Jones
  • Send Email »
  • More Articles »

If you are an existing developer, building your first Virtual Reality app can be relatively easy if you grab the right tools. In this article, you'll get a quick start towards building a VR app that will run on an Android phone.

Before you build your own first-person VR shooter or racing game, you need to start with something a bit less complex. In this article, we'll help you take that first step of getting a simple VR environment created.

Step 1: Getting the Tools

There are a multitude of tools that can be used to create VR worlds. Although you are free to use any tool you want, Unity3D is one of the most popular tools for building virtual reality solutions, so it is the one that will be used here. It's also free to use until you start generating money from your solutions; that is another great reason to pick it!

By using a tool such as Unity, you'll be not only be able to create a VR app for Android, but with a few tweaks of the build settings, you'll be able to target a variety of other devices as well. Another key selling point for many developers is the option to integrate Unity with Visual Studio for scripting solutions.

You can find and download Unity3D at this link.

Running Unity3D

Once installed, you should run Unity3D. When you do, you'll be greeted with the intro page similar to what is shown in Figure 1.

The Unity3D Intro screen
Figure 1: The Unity3D Intro screen

From the intro screen, you can click the New icon, which is marked in Figure 1. This will display a dialog for naming and saving your project, as shown in Figure 2.

Naming your Project in Unity
Figure 2: Naming your Project in Unity

You should give your Project a name as well as set the location where you want to store the files. In Figure 2, you can see that I've set the name for the project as VR-Android-Style. I'm storing my project in a Unity project folder. When the project is created, a sub-folder will be created within \Project\Unity that uses the project name.

You should confirm that the Template setting shows 3D. Unity can be used for 2D and other style projects as well. For the Android VR solution you are building from this article, you'll obviously be targeting 3D.

Once your settings are entered, click the Create project button to create your empty project. Depending on the power of your system, this can take a few seconds or minutes to load. Once it is loaded, you should see the defaulted Unity layout similar to Figure 3. There is a lot presented in the editor; however, you'll only need to work with some of this initially!

The default Unity layout for your project
Figure 3: The default Unity layout for your project

Targeting Android with Unity

By default, Unity is going to target a desktop solution. As such, you'll need to adjust a few build settings to target Android. In the editor, Select File -> Build Settings…. This will pull up the Build Settings dialog.

The Unity build settings
Figure 4: The Unity build settings

In the build settings dialog, you'll see a list of platforms that can be targeted. The PC, Mac & Linux Standalone setting will likely be highlighted. To target Android, click Android in the platforms list, then click the Switch Platform button. The result should be like what you see in Figure 4. If you wanted to target the iPhone, you'd click iOS.

You then can close the Build Settings dialog or move it out of the way for now.

Adding to Your VR Project

If you look at your Unity editor, it should still look like Figure 3. In the upper center section of the screen, you can see the work area, which we'll call the Scene, just as the tab indicates. The scene area includes a sunburst and a camera icon. The camera provides the location for the view of your VR world. The sunburst is simply a light, which will help to allow things to be seen. Without a default light, your resulting application would be similar to standing in a dark room with no windows or light—you wouldn't see anything!

You will create your VR world by adding game objects to this scene.

To add a game object, click the GameObject menu item in the Unity navigation. From there, click the 3D Object sub-menu because you are building a 3D solution. This will list several 3D GameObjects you can add to your project by selecting one.

Unity 3D GameObjects
Figure 5: Unity 3D GameObjects

To keep things simple, select the Sphere. This will add a simple sphere to your scene, as shown in Figure 6.

Adding a sphere to your virtual world
Figure 6: Adding a sphere to your virtual world

Congratulations! You have now created a virtual world that has a sphere in the middle of it.

If you look in the upper-left area of the Unity screen, you'll see that the Sphere is listed in the SampleScene as well. The properties (settings) for the sphere can be seen on the right side of the page in the Inspector dialog.

Odds are you will want to move or adjust your sphere. In the upper left of the Unity editor, are a number of icons that can be selected that will then allow you to manipulate your object in the Scene window. By default, the move icon is likely highlighted. Figure 7 shows the list of tools.

Unity Tools for manipulating objects
Figure 7: Unity Tools for manipulating objects

With the move tool highlighted, you can use the arrows in the scene view to move the sphere in any of the three axes by using your input device (such as a mouse). If you look closely, you'll see a box within the sphere. You can click and drag the sides of the box to shift the sphere across two axes. If you switch to the scale tool, you can use any of the three colored boxes to stretch or squish the object in that direction, or you can click and move the box in the middle to scale the entire game object proportionally.

If you move or scale the object, you'll see the changes also reflected in the Inspector window on the right. In Figure 6, the sphere was set to position 0,0,0 and was scaled to 1,1,1. In Figure 8, you can see that I've moved the object up by 1 unit and have scaled the entire sphere by 2. (Note that the image shows I've moved it up by .99 units, so almost 1!)

A manipulated sphere in Unity
Figure 8: A manipulated sphere in Unity

You can manipulate an object through the Scene or in the inspector. Changing the values in the inspector gives you greater control of the values; however, adjusting in either location will change the other automatically.

You can add additional objects to the scene. If you want to group objects, you can do so by using the hierarchy panel on the left. In Figure 8, you can see that the sphere is listed within the scene. As you add additional game objects, they will be added as well. Figure 9 shows my scene with three cubes added.

Adding more objects to a scene
Figure 9: Adding more objects to a scene

The second cube—named Cube(1)—had its y position changed to 2 in the Inspector window, which shifted it above the sphere. The third cube—Cube(2)—had its positions values changed so that x was 2 and z was -2. You can see these values by selecting the objects and reviewing the Inspector values listed. You can select the objects by clicking them in the scene or in the hierarchy list on the left.

Even though this scene would make a horrible overall virtual world, it is enough to get a feel for adding items and seeing them in VR. Whether your virtual world is as simple as the one in Figure 9 or more complex, the next step is to build the project.

Building the Android VR App

To build the Android VR app, you will go back to the Build setting screen you saw in Figure 4. This was done by selecting File -> Build settings….

In the Build Settings screen, make sure your scene is included in the box at the top where it says "Scenes In Build". In Figure 4, you see that the SampleScene is included (which is the name of the scene that is shown in the hierarchy menu as well. If you are using an older version of Unity, your default scene name might be a different name.

If no scenes are listed in the dialog, click the "Add Open Scenes" button to add the scene you had been creating. Once added, make sure the box is checked so that it is included in the build.

Because you are targeting Android, before compiling you will need to have the Android SDK installed as well the JDK. You can confirm that you have these from within Unity. Start by clicking Edit and then Preferences in the Unity navigation menus at the top. This will display the Unity Preferences dialog. Select "External Tools" and scroll down to the Android section. This will list the paths to both the Android SDK and JDK, as shown in Figure 10. If you don't have these, there should be a download button that you can use from the dialog to get them.

The Unity Preferences dialog
Figure 10: The Unity Preferences dialog

Once you've confirmed the paths, you can return to the Build Settings dialog.

At the bottom, to the right of the Build Settings dialog, you will see two buttons. The Build button will generate a file for the platform you have selected. In the case of Android, clicking Build will attempt to build a .apk file that can be used to install on an Android device (and .apk file is an Android program file, similar to an .exe on Windows). If you click Build and Run, the app will be built and, if your phone is connected, the app will be downloaded and run on the phone.

Additional Android Settings

Before building, there are a couple of additional settings you should review to avoid getting errors. For example, the first time you build, it is likely you will be asked to provide a name for the .apk file. After entering a name for your app, you will likely be shown a dialog asking you to enter a Bundle Identifier. If you get this message, your build process will stop. You will need to click OK to close the message.

You can add this required identifier by clicking the Player Settings… button in the Build Settings dialog. This will display the Player Settings dialog. Within the Player Settings dialog, click Other Settings, which will be expanded to show the Identification settings including the package name that as shown in Figure 11. These are the settings for the Bundle Identifier. Note that you might have to scroll down in the player settings to find the Other Settings section.

The Unity Player Settings…
Figure 11: The Unity Player Settings…

You will need to provide a value for the Package Name. In general, this would be "com," followed by your company name, followed by the product name. For my sample, I entered "com.DeveloperCom.VR_Android_Style", where DeveloperCom is for the company and VR_Android_Style is for the product name. You should enter something more relevant to you, although the same values I entered will work for your testing!

In addition to changing the Package Name, you should also review the Minimum API Level value that can be set in the player settings under the Other options as well. The API settings are a couple of lines down from the Package Name setting you just changed. In Figure 11, you can see the value set on my system is Android 4.1, 'Jelly Bean'. This value will actually cause an error as well because, for VR, you need to change the Minimum API Level to at least 19.

The third setting you need to modify to avoid errors is also in the Player Settings dialog, but at the very bottom. Click he XR Settings to expand that area of the dialog. This will show the VR settings. You should check the value for "Virtual Reality Supported". This will expand the virtual reality options to show the Virtual Reality SDKs that are assigned to your projects. Odds are, the list will be empty, as shown in Figure 12.

XR Settings in Unity
Figure 12: XR Settings in Unity

If you click the plus sign under the area where it states the list is empty, you should be shown several available SDKs. Most of the Android phone VR devices are compatible with Cardboard, so go ahead and select it.

The VR SDK options
Figure 13: The VR SDK options

With the three updates to the player settings, you should be ready to click Build or Build and Run. Remember, if you click Build, you'll then need to copy the .apk file to your device to install the app. If you do Build and Run, you will need to have an Android device tethered to your machine so that the App can be downloaded to it.

The first time you build for Android, the process will generally take much longer than future builds. Once the build is complete, you should be able to run the app on an Android phone. Figure 14 shows two screen shots of the running app. Clearly, the phone is being tilted in the lower screen shot!

The app running on Android
Figure 14: The app running on Android

If you have a Google Cardboard or similar device for your phone, the two images you see on the screen will be merged into a 3D view. Additionally, you'll see that you can move your head to cause the environment to tilt as well.

One thing you will notice that doesn't happen, is that if you move your location, the view doesn't move. This requires adjusting the camera in the scene. That, however, is a topic for the next article—which will be much shorter!

In Conclusion

This introductory article covered a huge amount of ground in building an Android VR app. You were thrown into the Unity development environment and shown the basics of adding an object. From there, you learned the critical settings that needed to be adjusted to build a basic Android VR app.

In the next article, I'll build on what you've done here to show you how to move the view. This will allow you to move around the objects you created!






Comment and Contribute

 


(Maximum characters: 1200). You have characters left.

 

 


Enterprise Development Update

Don't miss an article. Subscribe to our newsletter below.

By submitting your information, you agree that developer.com may send you developer offers via email, phone and text message, as well as email offers about other products and services that developer believes may be of interest to you. developer will process your information in accordance with the Quinstreet Privacy Policy.

Sitemap

Thanks for your registration, follow us on our social networks to keep up-to-date