0% found this document useful (0 votes)
32 views21 pages

Tutorial1 BasicVRProgramming

This document provides instructions for a basic VR programming tutorial in Unity. It outlines the required platforms, hardware, software and steps to set up a Unity project, import assets, add scripts to enable animations and movement, build an Android application and deploy it to a phone for testing in VR.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
0% found this document useful (0 votes)
32 views21 pages

Tutorial1 BasicVRProgramming

This document provides instructions for a basic VR programming tutorial in Unity. It outlines the required platforms, hardware, software and steps to set up a Unity project, import assets, add scripts to enable animations and movement, build an Android application and deploy it to a phone for testing in VR.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 21

Unity Tutorial 1: Basic VR Programming

Platform:
● Windows 10
● Android

Hardware:
● Android Phone (Google Pixel 6)
● USB-A to USB-C cable
● BNext VR Headset

Software:
● Unity Hub
● Unity 2020.3

>> Step 0. Download Required Resources.


Please download the file ‘VRBasics.zip’ on the EECS summer camp website - Resources page
to your personal directory, e.g., “C://users//’username’//”. Unzip it by right-clicking the file and
selecting “7-zip > Extract Here”. Note: 7-zip is an open-source file manager that provides a
much faster extraction speed than the default software in Windows.

The zipped file includes the following resources:


● NittanyLion.unitypackage. We modified a 3D model downloaded from Sketchfab. The
original model is designed by VAR Lab. We imported the model to Unity and packed it as
a .prefab file.
● Textures. A folder that contains several textures you may use to make your project more
attractive. The textures are downloaded from Solar System Scope.
● Scripts. A folder that stores all scripts that we will use to add animations to the
GameObjects.
>> Step 1. Activate the Unity Hub.
Double click the “Unity Hub” icon on the desktop to open it. When you open it for the first time,
you will be asked to sign it. By clicking the “Sign in” button, you will be automatically redirected
to the explorer. On this new page, you need to sign in with your Unity account. If you already
have a Google account, you can just click the Google icon to log in with your Google account. If
not, please click the “signup” button to register a new account to continue. Once logged in, you
will be automatically redirected to Unity Hub. If there are any dialog windows, just click “Got it”
or “OK”. When finished, you can find a list at the left of the program, five option lists: Projects,
Installs, Learn, Community, and Downloads. Navigate to Preferences > Licenses > Add
license.

On the pop-up window, click “Get a free personal license”. Then click “Agree and get
personal edition license”.

>> Step 2. Open the downloaded Unity Project.


Download the prebuilt Unity project “VRCampProject.zip” on the EECS summer camp website
- Resources page to your personal directory, e.g., “C://users//’username’//”. Unzip it as in step 0.
Return to Unity Hub, and navigate to “Projects” to manage your Unity projects, it should be
empty if this is the first time you use this program. On the right top you can find two buttons:
“Open” and “New project”. Just click “Open”.
Select the folder that you extracted from the zipped file. Click “Open”.

Wait for Unity to process the project. This may take a few minutes. Once it is done, you should
be able to enter the Unity’s interface, which looks like the following picture.
>> Step 3. Import the Nittany Lion Model.
Double click NittanyLion.unitypackage in the unzipped VRBasics folder. On the pop-up
window, make sure all components are checked and click “Import”.

In the Project Window, navigate to Assets > Prefabs. Drag NittanyLion.prefab and drop on
the Hierarchy Window.
In the Scene View, you are free to adjust the view in the following ways:
● Move the Scene View. You can use the Arrow Keys to move around the Scene as
though “walking” through it. The Up and Down arrow keys move the Camera forward
and backward in the direction it faces. The Left and Right arrow keys pan the view
sideways. Hold down the Shift and an arrow key to move faster. You can also use the
mouse to move the view by holding the middle button of your mouse and dragging it.
● Orbit the Scene View. Hold right-click, then drag.
● Zoom in or zoom out. Use the scroll wheel of your mouse.
Try to find the Nittany Lion in your scene.

You can freely adjust the position and rotation of the Nittany Lion. Click “NittantyLion” in your
Hierarchy Window.

In the Inspector Window, you can adjust the position and rotation of it by adjusting the
corresponding values in different axes. The row of scale is used to enlarge or decrease its size.
The other way to adjust those values is using the Toolbar.

By clicking on corresponding icons, you will find new axes appearing inside the model.

Try to play with the model either by modifying values on the Inspector Window or dragging the
corresponding axes. Move the Nittany Lion such that you can find it in the Game View.
You can also create other GameObjects in your Unity project. Navigate to the Hierarchy
Window. Right-click the empty space, and select “3D object”. There will be lots of different
objects you can use. Try to create a cube and a sphere and adjust the transformation as to the
Nittany Lion.
>> Step 4. Change the Color of Your Nittany Lion.
On the Project Window, navigate to “Assets”. Create a folder named “Materials” by right-clicking
the empty space, and selecting “Create > Folder”.

Double-click on the “Materials” folder. Right-click the empty space, and select “Create >
Material”. Name the material as “NittanyLion”. In the Hierarchy Window, click the drop-down
menu triangle adjacent to “NittanyLion”, then click “default”. Drag the material to “Add
Component” in the Inspector Window.
Click the “NittanyLion” material in the Project Window. Then in the Inspector window, click the
annotated area alongside Albedo. On the pop-up window, drag the two circles to see the color
change on your NittanyLion.

>> Step 5. Add Animation to your Nittany Lion.


Navigate to “Assets” in your Project Window. Drag the “scripts” folder to the empty space.

Open the “Scripts” folder. We have prepared several scripts for you with different functionalities.
We will introduce them one by one. Firstly, the script “SimpleMove.cs” will move the
Gameobject in a specific direction. Navigate to the Inspector window of the GameObject
“NittanyLion” by clicking it in the Hierarchy Window. Drag the script “SimpleMove.cs” to the
empty space of the Inspector window.
You will find a new component named “Simple Move (Script)” in the Inspector window. There
are several fields in which you can modify their values. You can control the moving speed and
direction of the Nittany Lion by adjusting the values in these fields. Hit the “Play” button in the
Toolbar. You will find the Nittany Lion shifts in the scene. Try different values in the fields of the
script to see the difference. You can also attach the script to the other objects to test it. Note: If
you cannot find the Nittany Lion in the Game View, switch to Scene View and adjust the view as
mentioned in Step 3. You can either modify the values in the Play mode (by hitting the Play
button) or in the Edit mode. But all changes in the Play mode will be reset when you exit the
Play mode by hitting the “Play” button again. By the way, remember to save your project when
you edit something in the Edit mode.

The second script “SimpleRotate.cs” will rotate the GameObject with a specific rotation angle.
Similarly, drag the script “SimpleRotate.cs” to “NittanyLion” or other objects you like. If you
want to focus on the rotation, you can delete the previous script by clicking the “vertical dots”
icon and selecting “Remove Component”. Or you can simply uncheck the script. Hit the “Play”
button to see what will happen.

Previous scripts focus on the animation of single objects, we also provided some scripts to
connect different objects. The script “SimpleTowards.cs” will move an object toward the other
object. Drag the script “SimpleTowards.cs” to “NittanyLion”. Different from previous scripts,
we need to assign the other GameObject to this script. I have created a cube for this purpose.
Drag the cube to the “Target” field of the script. Hit the “Play” button to test it. You are free to
change the location of the cube and the speed field. Try to attach the “SimpleMove.cs” to an
object and attach “SimpleTowards.cs” to the other objects to see what will happen.

Finally, the script “SimpleOrbit.cs” will make an object spin around the other object. Drag the
script “SimpleOrbit.cs” to “Nittany Lion”. Similar to “SimpleTowards.cs”, we also need to
assign an object to this script. After assigning an object, Hit the “Play” button to see what
happens. Based on those simple scripts, are you able to create a solar system? We have
prepared the textures for you in the folder “Texture”. Just drag them into “Assets”. If you want
to attach those textures to an object, first create a material, and drag the texture to the small
cube alongside Albedo in the Inspector window of the material, as in the following picture.

If you are interested in how those scripts work, just double-click any of the scripts in the
Inspector window, as shown below.
>> Step 6. Build Your Project.
Make sure your smartphone is connected to the computer through the cable. If it pops up a
window related to USB Debugging on your smartphone, tap “Allow”. Navigate to File > Build
Settings. Click “Refresh” and select “Google Pixel 6” under the drop-down menu.

Make sure your content under the blue circle is the same as the following picture. Click “Build”.
You will be asked to select a path to store the APK file, which is the file format used by the
Android operating system. You are free to use the default path. Just enter any filename you like
alongside the “File name” and click “Save”. Note: It is not required to keep the filename the
same as your project name or package name.

Find the Pixel icon on your computer.

Double click on it and navigate to Pixel 6 > Internal shared storage > Download. Copy the
apk file by either using Ctrl + C and Ctrl + V or dragging them as in the following screenshot.
On your smartphone, find the “Files” app. Open it and navigate to the “Downloads” folder. You
will find the apk file here. Click it. Click “install” on the pop-up window. Open the app. If you
cannot find it on the home screen, swipe from the bottom of the screen.

If it pops up a window that it cannot find the cardboard code, just click “Skip”. If you successfully
run the app, your screen should be similar to the following picture.

Put your smartphone into the BNext headset and play with the application that you built!
Appendix (Optional):
We prebuilt a project for you to simplify the configuration. If you are interested in building the
project from scratch, you can refer to the following steps.
>> Step 1. Create a New Project.
Go back to “Projects” to manage your Unity projects, it should be empty if this is the first time
you use this program. On the right top you can find two buttons: “Open” and “New project”. Just
click “New project”

Make sure your Unity version is 2020.3.*f1, and select the 3D template. You are free to change
the project name in Project Settings. The default name is fine. Click “Create project” to create
a new project. Then Unity will take some time to build the necessary resources for the new
project. Wait for a while until it is done.

After the building process is done, you will automatically be redirected to the Unity interface,
which should be like the following picture.
>> Step 2. Import the Google Cardboard SDK.
Go back to the file explorer and open the path where we downloaded the file ‘VRBasics.zip’.
Recall that we already unzipped this file and get several files. Please unzip the file
‘cardboard-xr-plugin-1.15.0.zip’ using the same approach as mentioned in step 1. Just
right-clicking the file, click “Extract All…”, and click “Extract” on the pop-up window.
Return to Unity, go to the Menubar on top of Unity’s interface. Click Window > Package
Manager. Note: make sure the file path is under the current user’s directory, for example,
“C://users//’username’//”.

On top of the pop-up window, select the plus sign > Add package from disk.

Navigate to the path where you unzipped the file. Select package.json and click “Open”.
Then Unity will automatically start the import process. Once the import process is done, you will
find a new package added to the package manager. Click “Google Cardboard XR Plugin for
Unity”, click “samples”, then click “Import” alongside the word “Hello Cardboard”.

After importing the samples, the path of the project window will be automatically redirected to
Assets > Samples > Google Cardboard XR Plugin for Unity > 1.15.0.

Navigate to Hello Cardboard > Scenes > HelloCardboard.unity by double-clicking


corresponding icons. Open the demo scene by double-clicking “HelloCardboard.unity”.

After that, your Hierarchy Window and Scene View should be similar to the following picture.
>> Step 3. Configure Android Project Setting.
Navigate to File > Build Setting. Click “Add Open Scenes”.

Click “Android” under the Platform menu. Click “Switch Platform” to switch the platform to
Android.
Navigate to Player Settings > Player > Resolution and Presentation.
● Set the Default Orientation to Landscape Left.
● Disable Optimized Frame Pacing.

Navigate to Project Settings > Player > Other Settings.


● Check Auto Graphics API.

● Select Android 7.0 'Nougat' (API level 24) in Minimum API Level.
● Select IL2CPP in Scripting Backend.
● Check ARM64 in Target Architectures.

Navigate to Project Settings > Player > Publishing Settings. In the Build section, check
Custom Main Gradle Template and Custom Gradle Properties Template.

Navigate to Assets > Plugins > Android. Delete gradleTemplate.properties and


mainTemplate.gradle by right-clicking on corresponding icons and clicking “Delete”. Drag
gradleTemplate.properties and mainTemplate.gradle from the unzipped folder to the Unity
Project Window.

Navigate to Project Settings > XR Plug-in Management. Check Cardboard XR Plugin under
Plug-in Providers.

You might also like