Skip to content
This repository has been archived by the owner on Oct 18, 2021. It is now read-only.

VRDF Multiplayer Extension

Arnaud Briche edited this page Mar 30, 2020 · 14 revisions

The VRDF Multiplayer is a extension to help you create a basic, online, multiplayer scene using the Photon Engine services.

A few features are already implemented (like instantiating the correct controllers for the remote users, or showing there Laser Pointers), but some more features will come in the future once I've got a bit more time to implement them. Have fun !

VRDF Multiplayer Setup

This is a Step-by-Step tutorial on how to create your own VR multiplayer game using VRDF and Photon Engine. If you have any problem during the creation of your multiplayer app, do not hesitate to send me an email at arnaudbriche1994@gmail.com ! :)

First part: Photon Setup

  1. You're going to need a Photon Engine connection to be able to use the multiplayer's features. To do so, go to https://www.photonengine.com/, Create an account / Login, create a new app of type "Photon PUN" and insert the name you want.

  2. Copy the App ID of your app, you gonna need it soon.

  3. Create a new Unity project, and import PUN 2 from the Asset Store as well as Photon Voice 2 (WARNING : Do not use the 1st version of PUN or Photon Voice, you absolutely need the 2nd version !)

  4. In Unity, go to Window > Photon Unity Network > PUN Wizard and click on Setup Project in the newly opened window.

  5. Enter the App ID you copied in step 2, and press the "Setup Project" button.

  6. The PhotonServerSettings should now be opened in your Inspector Window (if not, Window > Photon Unity Network > Highlight Server Settings).

  7. Enter the same App ID in the "App ID Voice" field.

  8. Setup a correct App Version number.

And that's it for Photon! You may need to redo the step 3 to 6 when you clone your project on another computer, but that's it for a basic Photon connection.

Second part: VRDF Setup

  1. Follow the Basic Steps to setup VRDF in your project. You can stop once you're done with step 7.

  2. Import the VRDF_Multiplayer_vX.x.unitypackage from the Release Page into your project

And everything is now ready for you to create your Multiplayer Environment!

The next parts below are here to help you understand what are the components you require to create a connection with the Server and a Multiplayer Scene in VRDF. You can skip those by simply copying the Assets/VRDF/Multiplayer/Samples folder.

WARNING : Don't forget to add the scenes in the Sample folder to the list of scenes in the Build Settings !

Third Part: Multiplayer Scene Setup

The Multiplayer Scene will be the one in which all your player are going to meet. You can of course create multiple connected scenes (for example, a waiting room and a Main Game Scenes), but you should NEVER load them locally if the PhotonNetwork.AutomaticallySyncScene boolean is at true (default value), or only do this if you understand exactly how Photon is working.

To get more info about this, I encourage you to check the PhotonNetwork Documentation; it's a bit unclear sometimes, but basically you only want your Master Client to load the current Active Scene by calling PhotonNetwork.LoadLevel(NameOfScene). Photon will then take care of loading this scene for all your players (you can check the PhotonNetwork.AutomaticallySyncScene parameter as well for more info).

So now, here's the Step-by-step guide on how to create a connected scene for your players to meet:

  1. Create a new Scene: Duh.

  2. Remove the Main Camera GameObject from the Scene: It will be taken over by SetupVR later.

  3. Setup a basic environment: Just throw a plane and some cubes in there, it's just to have something to display.

  4. Right click in the Hierarchy > VRDF > Add SetupVR to Scene: This will setup VR for your Local Player. For more info, check the documentation.

  5. Right click in the Hierarchy > VRDF > Multiplayer > In Room > Add Game Manager: This prefab contains four scripts:

  • the VRDFMultiplayerGameManager: Check if the player is correctly connected on start, and send back the user to the connection scene if not.

  • the VRDFPlayerInstantiater: Instantiate the Local Player Prefab (in DontDestroyOnLoad, Photon requirement) when they are entering the room.

WARNING: This prefab NEEDS to be in a Resources folder. By default, the VRDF Player Prefab is placed under the Assets/VRDF/Multiplayer/Resources/PhotonPrefabs folder, but you can place it anywhere in your project, as long as it's inside a "Resources" folder.

  • the PhotonCallbacksDebugger: Used to display some messages in the console whenever we fetch a callback from Photon. To disable the debug in the console, remove this script, or uncheck the "Show Console Messages" checkbox in the Inspector.

  • the RoomConnectionRecoverer: Used to reconnect to the room if the user is disconnected by anything but your game Logic. It will try to reconnect and rejoin the last room the player was in. If the disconnection lasted longer than the Player Time out variable of your room (PhotonNetwork.CurrentRoom.PlayerTtl), it will send you back to the connection scene.

For more info, check the rest of the documentation below !

  1. Check and setup the parameters in the newly created VRDF Multiplayer Game Manager

  2. Make sure that the scene you just created is in the Build Settings' scenes list

  3. Optional : Add a script that teleport the CameraRig on Awake to a random position on a plane, to avoid that all your players start at the same position. You can as well add the (MoveAround package)[https://github.com/Jamy4000/VRDF/wiki/VRDF-MoveAround-Extension] to let your users move in the scene.

That's it for the Multiplayer Scene ! Let's now create the second scene where all the connection magic is happening.

Fourth and Last Part: Connection Scene Setup

The Connection Scene is where you are going to check if the user is connected with the Server, where you create a new Room for your players to meet, or where you can join a Lobby and list the current available rooms.

WARNING: A room does NOT correspond to a scene. A room is just an abstract place for the players to connect to, and where you can load whichever scenes you want. Check the Photon Doc for more info !

  1. Create a new Scene: Don't forget to add it to your Build Settings' Scene List !

  2. Right click in the Hierarchy > VRDF > Multiplayer > Connection Scene > Connection Manager: This will create a new Connection Manager GameObject, which contains only three scripts:

  • the VRDFConnectionManager: This component handles the callbacks from Photon, and make sure that all parameters are present to establish a connection to a room.
  • the MultiplayerSceneLoader: Load the Multiplayer Scene given in the parameters. It's, by default, done automatically when the user created a room, but you can disable that using the provided boolean in the editor and then call the "TryToLoadMultiplayerScene()" method yourself whenever the user is ready to be send to the the specified scene. Or simply delete the script, your call cowboy !
  • the RoomListFetcher: Check when the room list is updated by Photon, and stores a list of available rooms in a static Dictionary. By default, connect to the Lobby automatically, but you can disable this option with the serialized boolean.
  • the PhotonCallbacksDebugger: Used to display some messages in the console whenever we fetch a callback from Photon. To disable the debug in the console, remove this script, or uncheck the "Show Console Messages" checkbox in the Inspector.
  • the ServerConnectionRecoverer: Used to reconnect automatically to the Server if the user is disconnected by anything but your game Logic. You can chose to try to reconnect in a loop by checking the toggle in the Inspector.
  1. Check and Setup the parameters: Just check if all the Serialized Field in the Inspector fits your needs. :)

  2. Create a script to request a connection to a room: This script, strictly speaking, only needs one method, as follow:

public void CreateOrJoinRoom()
{
    // "Room Name" is the name of your room, it does NOT need to be the same name as your multiplayer scene. 
    // It can actually be whatever you, or the user, wants. If the name is already taken by another room,
    // Photon will callback OnCreateRoomFailed with returnCode 0x7FFF - 1. If everything is alright, the Photon Callback "OnCreatedRoom" will be raised.
    VRDF.Multiplayer.VRDFConnectionManager.ConnectOrCreateRoom("Room Name");
}

This method will create a room with the name "Room name" if it doesn't exist yet, or join the room with this exact same name if it does exists.

WARNING: We'll only be able to check if the room name exist if you are connected to a Lobby, meaning that the OnJoinedLobby Callback from Photon has to be raised ! If you do use the VRDF Multiplayer sample scene and didn't change any parameter, a message in your console will be displayed once you've entered the Lobby.

  1. Call the above method: You've got the choice on this one: Create a "Connect" Button and link the OnClick response to the method above, add it to the OnJoinedLobby callback from the MonoBehaviourPunCallbacks, ... This is as you want ! :).

Aaaaand that's it ! Just enter playmode from your ConnectionScene, and enjoy Multiplayer in VR !

Server Connection

What is that ?

The first thing your player will have to do when entering your multiplayer game is connect to the server. To do so, you can either connect to the Photon Cloud servers, to a Self-Hosted, local server, or to a Self-Hosted, public server. I won't go into detail on how to use those, as you can check the Photon Documentation for that.

Tip: You can create a "fake" online session by setting the PhotonNetwork.IsOfflineMode to true. Photon will then fake the connection and callback the OnConnectedToMaster method.

How to use

In VRDF, a connection to the Photon Cloud Server is automatically made on Awake. The only thing you need to do is setup the AppID in the PhotonServerSettings, as explain above in the Setup Photon Part.

VRDFConnectionManager will then call PhotonNetwork.ConnectUsingSettings(), which will try to establish a connection with the Photon Server using the parameters provided in the PhotonServerSettings. Once done, you'll be able to create a room, to join one if you know the name, or to enter a lobby to list all available rooms.

Lobby

What is that ?

The lobby is the place where your player can fetch info on the current available rooms and amount of players online. You can create multiple type of Lobby, depending on your use case. For more info, I would suggest to check the doc from Photon, it's quite easy to understand.

Tip: The lobby isn't a mandatory step to go through: As soon as you are connected to the Servers, you can create a new room.

It's actually recommended by the Photon Documentation to only connect to a Lobby if you want to display more info to the user, like the available rooms or the amount of users. Keep that in mind !

How to use

In VRDF, the Lobby can either be joined automatically or not. To toggle this option, simply click on the checkbox of the RoomListFetcher component of the VRDFConnectionManager prefab.

When using the VRDF Multiplayer Sample Scenes, if you decide to join the Lobby by yourself, you need to click on the "Join Lobby" button to be connected and see the list of available rooms.

If you want to join a basic Lobby, simply call the method PhotonNetwork.JoinLobby(). Keep in mind that the available rooms for a Lobby are only for this specific Lobby. This means that:

  • All rooms created outside a Lobby won't be accessible without their names: If you create a room without being in a Lobby, you won't be able to see it when joining a Lobby on another Client. The creator of the room will have to provide you the specific name of the room to connect to it.
  • All rooms created from inside a Lobby will only be accessible using their names or by connection to this specific Lobby: If you create a room from within a Lobby, you'll only be able to see it if your other clients are in the same Lobby. You can still ask the creator of the room to provide you the specific name of the room and then connect to it.

In Room

What is that ?

The room is an abstract name to say "where your player meets". You can't, for example, communicate with other players (via chat or voice) before you're actually inside a room.

Most of the time, a game's room is made of multiple scenes (your actual game), that are different from the Lobby scene (even though some games do keep the same scene, like Borderlands 2 main menu for example).

How to use

You can enter a room by calling one of those two methods: