Dependencies

VRTemplate has a few dependencies, some of them need to be installed and set up before using it:

  • Unity XR plugins
    • OpenXR plugin
    • XR Interaction Toolkit
  • Photon: Multiplayer platform for Unity
    • Photon PUN
    • Photon Voice
  • Firebase: Google’s web application development platform providing multiple tools.
    • Firebase Auth
    • Firebase Database
    • Firebase Storage
  • Animation rigging
  • TextMeshPro

Setup XR Interaction Toolkit

  1. Go to Project Settings/XRPluginManagement and install XR Plugin Management.
  2. Once it is installed, check the OpenXR option for the required platforms. Be patient, as this will install more packages.
  3. Click Yes if prompted to change the input system
  4. Open OpenXR settings under XR plugin management in the left side menu
  5. Add devices in “Interaction Profiles” section
  6. In the package manager, install XR Interaction Toolkit.
  7. If prompted, click “I Made a BackUp, Go Ahead!” to continue.
  8. Go to XRInteraction Toolkit and install Starter Assets.

Setup Photon

  1. Download and import the assets: https://assetstore.unity.com/packages/tools/network/pun-2-free-119922

and https://assetstore.unity.com/packages/tools/audio/photon-voice-2-130518

If the PUN Wizard window appears , don’t close it:

  1. Now we need two IDs for Photon PUN and Photon Voice.
  2. Enter in the web: https://www.photonengine.com/
  3. Login or Sign In in your photon account and go to your dashboard
  4. Create new app
  5. Select “Pun” as the Photon SDK, and enter an application name.
  6. Repeat steps 5 and 6 but now selecting “Voice” as the Photon SDK.
  7. You will need the App ID of each project:
  8. Add the App ID of the Pun project in the PUN Wizard window in Unity
  9. In Unity, click on Window/Photon Unity Networking/Highlight Server Settings
  10. Add the App IDs of both Photon Projects (PUN and Voice)
  11. Repeat the process in Assets/Photon/PhotonVoice/Resources/VoiceAppsettings as in the step 11.

Setup Firebase

  1. Enter in Firebase: https://firebase.google.com/
  2. Create a Project
  3. Add a Unity app and follow the steps that will show up after that.
  4. Make sure to initialize the services you want to use and after that, re-download the google-service.json from the project configuration / your apps / sdk configuration.
  5. Now install the services you are going to use from the firebase_unity_sdk you downloaded creating the app, do not forget that this project uses FirebaseDatabase, FirebaseAuth and FirebaseStorage, although you can always comment the code that gives you an error if you do not want to use any of the services, although it is recommended not to do it with FirebaseAuth for the security of your data.

Setup VRTemplate

  1. Import Animation rigging
  2. Import TextMeshpro Essentials
  3. Import VRTemplate
  4. Build scenes
  5. Go to VRTemplate/Resources/Player and inside the prefab “VR Player” you will find a child GameObject called Settings with a component called VRSettingsPlayer. In the parameter “Setup Menu Action”  add the action “XRI Left Hand Interaction/Menu”.

Template usage

This template has 2 different template scenes ready to be used and modified to the user’s requirements:

  • LobbyRoom: Offline scene to be used as the lobby where the users can select things like avatars, microphone settings, etc before joining the metaverse.
  • OnlineRoom: Online room where the players can meet other players.

Both scenes can be found in Assets\VRTemplate\Scenes

Template tools

VRTemplate provides 2 prefabs with the necessary elements to work in both the lobby and the online room.

LobbyTemplate Prefab

Elements included in the prefab:

  • EventSystem
    • This GameObject contains the event system from Unity, along with the XR UI Input module. It has the basic elements to rotate and translate the player avatar.
    • If you have your own system for avatar movement, then you can remove this.
  • Firebase
    • This is a singleton with all the necessary functions to make calls to Firebase. The list of functions available is described in the section Firebase calls.
    • There is a demo scene showing the use of this script. The demo scene can be found in Assets\VRTemplate\Demo\FirebasePlug demo
      • Inside the demo scene, the GameObject called Canvas contains a component called FirebasePlugDemoCanvas, which contains all the example code to use Firebase.
    • If you don’t want to use firebase, you can remove this.
  • AvatarSelector:
    • Contains 2 child GameObjects:
      • Avatar Canvas: a ready made UI element that can be used for the selection of the player avatar. You can disable it if you don’t need it.
      • Mirror Avatar: an element that emulates a mirror for the player avatar, replicating its movement and rotation. You can disable it if you don’t want it.
    • Contains a component to control the avatars that can be used and the UI buttons to select each one. 
    • This is an obligatory element.
  • LobbyCanvas
    • It contains the logic to connect to an online room (or create a new one when necessary). It also contains the UI button for the players to use when they want to start a connection.
    • This is an obligatory element.
  • NetworkRoom
    • It contains the logic to keep the different required variables from the lobby to the metaverse.
    • This is an obligatory element

The AvatarSelector GameObject requires some configuration to be used properly. It contains a component called AvatarCanvas, with variables that must be filled:

  • Avatar Prefabs: a list of the prefabs of the avatars that can be used. These avatar prefabs must be generated in a specific way, which is explained in the section Creation of avatar prefabs.
  • Buttons in canvas: the list of all the buttons included in the canvas. The system will temporarily disable these buttons when pressing on any avatar selection button, to prevent issues, and then enable them again.
  • Buttons to change avatar: the list of all the buttons included in the canvas that are used to select a player avatar. The system correlates the list of avatars with this list of buttons to match each avatar with the button in the same index order.
  • Offset for mirror: These are display options for the avatar mirror shown in the lobby.

OnlineTemplate Prefab

This prefab handles the spawn logic for players and the audio from microphones in the online room. It has 2 elements:

  • Pun Voice
    • Uses Photon to share audio between players in the room.
    • This is an obligatory element.
  • Spawn system
    • Handles the spawn of players. It randomly chooses the transform of one of its child GameObjects as a spawn point. You can modify/add its child GameObjects to allow as many spawn points as you want.
    • This is an obligatory element.

Player prefabs

There are 2 types of prefabs to use for the player, which provide the proper camera settings and avatar limb detection, depending on the platform target for your project:

  • NotVR Player: Non-VR camera settings. Contains the UI to use virtual sticks in case the project is built for smartphones.
  • VR Player: VR camera and locomotion settings.

Only one of these prefabs should be used in a build.

Creation of avatar prefabs

You can find a template in Assets\VRTemplate\Resources\Avatar\Avatar_X

You can make a copy of that template and then follow these steps:

  1. Add the 3D model as a child of the prefab.
  2. In the root of the prefab you will find a component called AvatarPlayer
    1. Select the transforms in the 3D model that will act as Head, Left Hand and Right Hand constraints.
    2. If you want parts of the model to be hidden while in VR more, add them to the list of hidden parts for VR.
  3. Save the new prefab. 

Selection of VR/Non-VR modes

In order to select VR/Non-VR mode for your build, 2 things must be taken into account:

  • The proper player prefab must be used (VR or NotVR Player prefabs)
  • In Project settings, the XR Plugin Management must be enabled for a VR build, or disabled for a non-VR build.

All this is independent of what type of build you have selected in build settings.

Folder structure

  • VRTemplate
    • Demo
      • Prefabs
      • Scripts
    • Resources
      • Avatar
      • Networking
      • Player
      • Template
    • Scenes
    • Scripts
      • Firebase
      • Networking
      • Player
      • UI
      • Utility
    • Sprites
      • Controller

Firebase calls

public void StartFirebase(Action callback, Action<string> fallback)

Starts the connection with firebase. This function (or StartFirebaseAndTryAutoLogin) must be called before any other function to work correctly.

Parameters:

  • callback: Action called if the successful connection is confirmed.
  • fallback: Action called if something goes wrong. return an exception in string format.

public void StartFirebaseAndTryAutoLogin(Action callback, Action autoLoginCallback, Action<string> fallback)

Starts the connection with firebase and tries to automatically log in. This function (or StartFirebase) must be called before any other function to work correctly.

Parameters:

  • callback: Action called if the successful connection is confirmed but could not auto login.
  • autoLoginCallback: Action called if the successful connection is confirmed and auto login was successful
  • fallback: Action called if something goes wrong. Return an exception in string format

public void LogIn(string email, string pass, Action callback, Action<string> fallback)#

Authenticate the user by email and password.

Parameters:

  • callback: Action called if the authentication is successful.
  • fallback: Action called if something goes wrong. Return an exception in string format

public void SignUp(string email, string pass, Action callback, Action<string> fallback)

Create a new user authentication by email and password and also authenticate the user.

Parameters:

  • callback: Action called if the creation and authentication has been successful.
  • fallback: Action called if something goes wrong. Return an exception in string format.

public void ChangePassword(Action callback, Action<string> fallback)

Sends an email to the currently logged in user’s email address where they can change the account password.

Parameters:

  • callback: Action called if the email was successfully sent.
  • fallback: Action called if something goes wrong. Return an exception in string format.

public void RemoveUserAuth(Action callback, Action<string> fallback)

Removes the current user’s email address from firebase authentication.

Parameters:

  • callback: Action called if the user was successfully removed.
  • fallback: Action called if something goes wrong. Return an exception in string format.        

public void LogOut()

Closes the current user session.        

public void ReadValueDatabase(string path, Action<DataSnapshot> callback, Action<string> fallback)

Reads data from a specific branch of the database

Parameters:

  • path: specific branch of the database.
  • callback: Action called if all goes well. Return the data from the database.
  • fallback: Action called if something goes wrong. Return an exception in string format.

public void WriteRawJsonDatabase(string path, string json, Action callback, Action<string> fallback)

Writes data in a specific branch of the database. The data will be pushed so that it can be used for editing and removing.

Parameters:

  • path: specific branch of the database.
  • json: JSON data to write in the database.
  • callback: Action called if data was successfully written in the database.
  • fallback: Action called if something goes wrong. Return an exception in string format.

public void WriteRawValueDatabase(string path, string value, Action callback, Action<string> fallback)

Writes data in a specific branch of the database. The data will be pushed so that it can be used for editing and removing.

Parameters:

  • path: specific branch of the database.
  • value: new value to write.
  • callback: Action called if data was successfully written in the database.
  • fallback: Action called if something goes wrong. Return an exception in string format.

public void RemoveValueDatabase(string path, Action callback, Action<string> fallback)

Removes a specific branch of the database.

Parameters:

  • path: specific branch of the database.
  • callback: Action called if the branch is successfully removed.
  • fallback: Action called if something goes wrong. Return an exception in string format.

public void EditDatabaseValueSecurely(string path, Action<DataSnapshot> callback, Action<string> fallback, Func<MutableData, TransactionResult> transaction)

Securely edit a database branch using firebase RunTransaction.

Parameters:

  • path: specific branch of the database.
  • callback: Action called if the branch is successfully edited.
  • fallback: Action called if something goes wrong. Return an exception in string format.
  • transaction: Safe mode switching function.

public string CreateUniqueKeyDatabase(string path)

Returns a unique key for a specific branch of the database.

Parameters:

  • path: specific branch of the database.

public void UploadFileStorage(string filePath, string storagePath, Action<StorageMetadata> callback, Action<string> fallback, MetadataChange metadata = null)

Uploads a file into the Firebase Storage and returns its metadata.

Parameters:

  • filePath: Path to upload the file.
  • storagePath: specific branch of the storage
  • callback: Action called if the branch is successfully edited. Returns its metadata.
  • fallback: Action called if something goes wrong. Return an exception in string format.

public void UploadFileStorage(byte[] bytes, string storagePath, Action<StorageMetadata> callback, Action<string> fallback, MetadataChange metadata = null)

Uploads a file into the Firebase Storage and returns its metadata.

Parameters:

  • bytes: the file data to be uploaded.
  • storagePath: specific branch of the storage.
  • callback: Action called if the branch is successfully edited. Returns its metadata
  • fallback: Action called if something goes wrong. Return an exception in string format.

public void ReadFileStorage(string storagePath, Action<Uri> callback, Action<string> fallback)

Read a file from storage, return its uri

Parameters:

  • storagePath: specific branch of the storage.
  • callback: Action called if the file is successfully read. Returns its uri.
  • fallback: Action called if something goes wrong. Return an exception in string format.

public void ReadPictureStorage(string url, Action<Texture2D> callback, Action<string> fallback)

Read a picture from storage and returns it as a texture

Parameters:

  • url: Storage location.
  • callback: Action called if the picture is successfully read. Returns its texture.
  • fallback: Action called if something goes wrong. Return an exception in string format.