On the Photon dashboard, click the App ID field to reveal the app ID, then copy it to your clipboard: In the Unity menu, select Window Photon Unity Networking PUN Wizard to open the Pun Wizard window, click the Setup Project button to open the PUN Setup menu, and configure it as follows.
- Photon Unity Networking (PUN) Questions, feedback, feature requests for Photon Unity Networking Plugin 9K discussions 24.2K comments Most recent: Player reconnects to the game, player does not spawn at the last location. By Hunel 10:12AM.
- Your Photon configuration is created by the PUN Wizard and contains the AppId, region for Photon Cloud games, the server address among other things. To ignore the settings file, set the relevant values and connect by calling ConnectToMaster, ConnectToRegion.
In this tutorial, you will prepare for creating a shared experience using Photon Unity Networking (PUN). You will learn how to create a PUN app, import PUN assets into your Unity project, and connect your Unity project to the PUN app.
Objectives
- Learn how to create a PUN app
- Learn how to find and import the PUN assets
- Learn how to connect your Unity project to the PUN app
Creating and preparing the Unity project
In this section, you will create a new Unity project and get it ready for MRTK development.
First, follow the Initializing your project and deploying your first application, excluding the Build your application to your device instructions, which includes the following steps:
- Creating the Unity project and give it a suitable name, for example, MRTK Tutorials
- Creating and configuring the scene and give the scene a suitable name, for example, MultiUserCapabilities
Then follow the Changing the Spatial Awareness Display Option instructions to:
- Change the MRTK configuration profile for to the DefaultHoloLens2ConfigurationProfile
- Change the spatial awareness mesh display options to Occlusion.
Enabling additional capabilities
In the Unity menu, select Edit > Project Settings... to open the Player Settings window, then locate the Player > Publishing Settings section:
In the Publishing Settings, scroll down to the Capabilities section and double-check that the InternetClient, Microphone, SpatialPerception, and GazeInput capabilities, which you enabled during the Configuring the Unity project step above, are enabled.
Then enable the following additional capabilities:
- InternetClientServer capability
- PrivateNetworkClientServer capability
Installing inbuilt Unity packages
In the Unity menu, select Window > Package Manager to open the Package Manager window, then select AR Foundation and click the Install button to install the package:
Note
You are installing the AR Foundation package because it is required by the Azure Spatial Anchors SDK you will import in the next section.
Importing the tutorial assets
Add AzurespatialAnchors SDK V2.7.1 into your unity project, to add the packages please follow this tutorial
Download and import the following Unity custom packages in the order they are listed:
After you have imported the tutorial assets your Project window should look similar to this:
Tip
For a reminder on how to import a Unity custom package, you can refer to the Importing the tutorial assets instructions.
Note
After importing the MultiUserCapabilities tutorial assets package, you will see several CS0246 errors in the Console window stating that the type or namespace is missing. This is expected and will be resolved in the next section when you import the PUN assets.
Importing the PUN assets
In the Unity menu, select Window > Asset Store to open the Asset Store window, search for and select PUN 2 - FREE from Exit Games, click the Download button to download the asset package to your Unity account.
When the download is complete, click the Import button to open the Import Unity Package window:
In the Import Unity Package window, click the All button to ensure all the assets are selected, then click the Import button to import the assets:
Once Unity has completed the import process, the Pun Wizard window will appear with the PUN Setup menu loaded, you can ignore or close this window for now:
Creating the PUN application
In this section, you will create a Photon account, if you don't already have one, and create a new PUN app.
Navigate to the Photon dashboard and sign in if you already have an account you want to use, otherwise, click the Create One link and follow the instructions to register a new account:
Once signed in, click the Create a New App button:
On the Create a New Application page, enter the following values:
- For Photon Type, select PUN
- For Name, enter a suitable name, for example, MRTK Tutorials
- For Description, optionally enter a suitable description
- For Url, leave the field empty
Then click the Create button to create the new app:
Once Photon has finished the creation process, the new PUN app will appear on your dashboard:
Connecting the Unity project to the PUN application
In this section, you will connect your Unity project to the PUN app you created in the previous section.
On the Photon dashboard, click the App ID field to reveal the app ID, then copy it to your clipboard:
In the Unity menu, select Window > Photon Unity Networking > PUN Wizard to open the Pun Wizard window, click the Setup Project button to open the PUN Setup menu, and configure it as follows:
- In the AppId or Email field, paste the PUN app ID you copied in the previous step
Then click the Setup Project button to apply the app ID:
Once Unity has finished the PUN setup process, the PUN Setup menu will display the message Done! and automatically select the PhotonServerSettings asset in the Project window, so its properties are displayed in the Inspector window:
Congratulations
You have successfully created a PUN app and connected it to your Unity project. Your next step is to allow connections with other users so that multiple users can see each other.
Photon Unity Networking (PUN) is a Unity package for multiplayer games. Flexible matchmaking gets your players into rooms where objects can be synced over the network. RPCs, Custom Properties or 'low level' Photon events are just some of the features. The fast and (optionally) reliable communication is done through dedicated Photon server(s), so clients don't need to connect one to one.
PUN exports to basically all platforms supported by Unity and has two options:
PUN FREE
No-cost package with various demos, pre-made scripts and reference documentation. Exports to basically all platforms.
PUN PLUS
Same content as PUN FREE, plus a 100 concurrent user plan for the Photon Cloud (approx. 40k MAU, valid 12 months).
Join our discord server to chat with the community or one of our team members when available.
PUN's Structure
Photon Pun 2 Documentation
Usually, you don't have to mind the structure of the PUN package but just for the overview, here's how things stack up. The PUN package wraps up three layers of APIs:
- The highest level is the PUN code, which implements Unity-specific features like networked objects, RPCs and so on.
- The second level contains the logic to work with Photon servers, do matchmaking, callbacks and such. This is the Realtime API. This can be used on it's own already. You will notice a lot of overlap of topics between PUN and the Realtime API (a.k.a. LoadBalancing API) but that's fine.
- The lowest level is made up of DLL files, which contain the de/serialization, protocols and such.
Photon Pun 2
Get Started
To get the best out of PUN, you will need to do some programming.This page shows several important code snippets as an overview.
To get properly started, work through the 'PUN Basics Tutorial'.
Connect And Callbacks
ConnectUsingSettings
gets you online in no time: It grabs all important settings from the PhotonServerSettings
asset and off you go.
PUN uses callbacks to let you know when the client established the connection, joined a room, etc..
For example: IConnectionCallbacks.OnConnectedToMaster
.
For convenience, PUN has the MonoBehaviourPunCallbacks
MonoBehaviour. It implements important callback-interfaces and registers itself automatically, so you can inherit it and just override specific callback-methods.
Alternatively implement IConnectionCallbacks
in any class and register instances for callbacks via PhotonNetwork.AddCallbackTarget
.
Matchmaking
Within OnConnectedToMaster
you could try to join an existing room or create your own.The following code snippets show possible method calls to start or join games.
When friends want to play together and have a way to communicate outside of PUN (e.g. with Photon Chat, Facebook), they can make up a room name and use JoinOrCreateRoom
. If nobody else should be matched into this room, make it invisible for matchmaking:
With JoinOrCreateRoom
, the room gets created on demand, so it doesn't matter who is first.If it's full, IMatchmakingCallbacks.OnJoinRoomFailed
gets called (if you implemented and registered it somewhere).
Read more about matchmaking in our guide.
Game Logic
GameObjects can be instantiated as 'networked GameObjects' with a PhotonView
component. It identifies the object and the owner (or controller). The player who's in control, updates everyone else.
Typically, you would add a PhotonView
to a prefab, select the Observed
component for it and use PhotonNetwork.Instantiate
to create an instance.
The observed component of a PhotonView
is in charge of writing (and reading) the state of the networked object several times a second. To do so, a script must implement IPunObservable
, which defines OnPhotonSerializeView
. It looks like this:
Photon Pun
Clients can do Remote Procedure Calls on specific networked objects for anything that happens infrequently:
Photon Pun Rpc
Independent from GameObjects, you can also send your own events:
Photon Pun 2
Read more about PUN's RPCs and RaiseEvent.
Demos And Tutorials
In the PUN packages, you find several demos and useful scripts, which you can reuse and or dissect and redo.
To get properly started, take your time to read and code the 'PUN Basics Tutorial'.
To Document Top