Having wrapped up my Unity VR Basics tutorials, it’s time to move on. During my last series of tutorials, I lumped everything into a single project that was hosted on Github, but kept subjects modular by separating them by scene. This eventually creates a lot of bloat after making 15 tutorials.
I want to continue developing tutorials and we need a strong foundation. The new approach will be providing a VR project that is setup with a standard scene and VR functionality. I’ve included some of the basic scripts, models and animations from previous tutorials to help form this foundation. This template can then be used in future tutorial as a starting off point so we don’t need to deal with setup. If there are any additional resources needed in future tutorials, you’ll be able to download them and add them to the project. This will allow us to avoid creating one massive bulky file full of subjects you might not care about.
You can find the template here.
Now I’ll cover what’s inside the template. As a bonus, I’ll also show you how to set this template up so you can launch it as a new project from Unity Hub.
What's Inside the VR Tempalte
Packages:
This template contains all the necessary packages in order to develop VR/AR in Unity using the XR Interaction Toolkit. These packages being the XR Plugin Management, XR Interaction Toolkit, Oculus XR Plugin and OpenXR Plugin. If you have any concerns about what these packages are or what they do specifically, you can find answers in my guide in setting up a VR project found here.
Scene and Asset Organization:
To make this template more versatile, I’ve also added a bit of organization to the project. Starting off with the Scene, I’ve added multiple Objects with names that signify what they should store in them.
We can always add additional Objects to store other items when needed. For example, we may have a group of Objects that handle managing the Scene, so we could add a –Scene_Manager– to keep it organized and separate from the XR Managers.
For asset organization, I’ve made a series of folders to store assets. I want to point out some assets that I’ve added to this project. Opening up folders, you may see some subfolders with FFOS. This is to show specific assets that I’ve added to this template and intend to use in future tutorials.
I also have a ThirdParty folder that will store our plugins/packages that we’ve added. Important items like the XR Interaction Toolkit can be found here. You’ll also find some materials provided by Kenney to help spruce up our prototypes.
If this isn’t how you like to organize you projects, feel free to delete and rearrange how you like. I want this to be a template you’re happy to create and learn with.
Assets in the Project:
For assets that I’ve added into the project, we have our hand models with colliders on them. We also have the hand animations/controllers associated with those hands. To make all of that work, I’ve also included the Hand script from my previous tutorial about Inputs and hand animations found here.
I’ve also included the teleportation reticle and the teleportation controller script used for thumbstick teleportation. If you’re not familiar with that you can find the tutorial related to that here.
XR Origin and XR Managers:
On the XR Origin we have a Character Controller to help with colliding into objects when we’re moving. It also has the Character Controller HMD Updater script. This helps fix a bug concerning when players move without using the thumbstick, allowing them to move through walls since the Character Controller collider won’t follow them until the thumbstick is moved.
We have Direct Interactors that are set to only interact with object on the Interactable and Interactable Ignore Ray layers. They also have the Hand script on it, which will be responsible for spawning our hands, handling their animation and hiding them when items are selected.
We also have a couple of Ray Interactors that can grab items from far away if they are set the the Interactable layer.
There are also Teleporting Ray prefabs that I’ve added so we can just swap out Continuous Movement for Teleportation depending on what the project needs.
Speaking of which, this brings us to the XR Managers. These help with our interactions between objects, movement and inputs from our controllers. If you’re doing Teleportation instead of Continuous Movement, just make sure to turn off the Continuous Move Provider and turn on the Teleportation Provider.
Wrap up before bonus
So that is going to be my foundational project for my Unity VR tutorials here on out. Allowing you to have a starting off point to build from when following whatever tutorials I come up with in the future. If you want to keep different tutorials, consider making separate scenes and specifying what they are for. Don’t like they way I’ve arranged things? Feel free to customize it however you like.
Bonus : Custom Unity Template
You could duplicate this project and add it to Unity Hub whenever you wanted to create a new VR project, but there is a cooler way. Unity allows you to setup your own custom templates so you can boot a new one up straight from Unity Hub.
Locate the Project Template Folder
The path you’re looking for should be similar to this.
Unity\Hub\Editor\2020.3.6f1\Editor\Data\Resources\PackageManager\ProjectTemplates
Side Note : The Unity version after Editor should be the same that you’re using for your template
Open Up a Project Template
Project templates are zipped up as a .tgz . You can open them with various programs like 7-zip, winzip, ect. I’ve always just gone with 7-zip which can be found here.
Inside you’ll find a folder called package and when you click it, you’ll find this! This is what we’ve been looking for. Make sure to go back a level and drag and drop the package folder out into your Project Template folder.
Editing the Package Folder
Locate the package.json file. You’ll need to open it up using an editor. Visual Studio, Notepad or whatever coding dojo you fancy.
Once it’s open we’ll need to change some things to help customize our template.
Underlined in the image above will be the lines that we’re changing.
name : This field you need to make sure you keep the convention that Unity has established.
example:
“name”: “com.unity.template.universal“
would become
“name”: “com.unity.template.awesometemplate“
displayName : You can change this to whatever you want. It is what you’ll see in the Unity Hub when we’re done. If you don’t change it, you’ll end up with multiple templates with the same name.
description : This helps describe what the template is. It’s nice to keep things organized, so make sure the description makes sense to future you 5 years from now.
Boom. That’s it. Save and exit package.json
Copying Your Project to the Template Folder
First: We’ll want to go into the ProjectData~ folder that’s located int the package folder. From there, we can delete the Assets, Packages and Project Settings.
Second: We’ll want to locate the project that we wish to make a template out of. Then we copy the Assets, Packages and Project Settings into the ProjectData~ folder.
Third: We need to go into the ProjectSettings of the folder we just copied. Inside will be a folder called ProjectVersion.txt . If we do not delete this file, the template will not work.
Create .TGZ File
7-Zip comes back to save the day for us here. We’ll be using it to create the TGZ file for Unity Hub to use. Right click on package and go to 7-Zip. Choose “add to archive”.
(If you don’t see 7-Zip when you right click the package folder, you can also open 7-Zip and locate the folder and right click it to do this step.)
At the top it will ask for a name. Use the naming convention from before and finish it with .tgz
Example: com.unity.template.awesometemplate.tgz
Once the new .tgz file is created, make sure to delete the old package folder. If you do not do this, it can cause errors.
Restart Unity Hub and You're Done
Restart Unity Hub and start up a new project. You should see your template as an option to choose from.
Congrats! You just saved a bunch of time on future projects.
Cheers!!!