Migration Notes
PUN 2 aggregates a lot of breaking changes and updates in one package but the overall usage will be similar to PUN as you know it.
It is a separate package from PUN 1.x so you can choose when to update. Also, you can go back anytime.
If you use PUN Plus (v1.x) and want to update to PUN 2, use your existing AppId with the PUN 2 Free package.
All benefits carry over.
To update, it's best to completely remove PUN and do a clean install of PUN 2 to make the necessary API and logic changes. Below, you will find the biggest changes but the list may be incomplete.
Overview
- PUN 2 now includes and makes use of the Realtime API, which is less Unity specific.
- There are two distinct namespaces for the two layers:
Photon.PunandPhoton.Realtime. - Best Region selection and a system for callbacks are now done in the Realtime API. The regions enum is gone.
- Only
ConnectUsingSettings()is making use of the PhotonServerSettings. - Many callbacks, methods and fields were renamed for various reasons. Read more here.
- For callbacks a class has to implement an interface and register itself as interested. Read more here.
MonoBehaviourPunCallbacksimplements most callbacks. Inherit this class and override as needed. Call base in OnEnable and OnDisable. Read more here.
API Changes
PhotonNetwork.autoJoinLobbyandServerSettings.JoinLobbyare gone. Unless you need a lobby, don't join them.PhotonNetwork.GetRoomList()is gone.
You get rooms list and updates fromILobbyCallbacks.OnRoomListUpdate(List<RoomInfo> roomList)callback. You can optionally cache it, update it and clear it when needed.
See snippet here.PhotonNetwork.Friendsis gone. You get friends list fromIMatchmakingCallbacks.OnFriendListUpdate(List<FriendInfo> friendList)callback. You can optionally cache it, update it and clear it when needed.PhotonNetwork.LobbyStatisticsis gone. When enabled, you can get lobby statistics list fromILobbyCallbacks.OnLobbyStatisticsUpdate(List<TypedLobbyInfo> lobbyStatistics)callback. You can optionally cache it, update it and clear it when needed.PhotonNetwork.connectingis gone.PhotonNetwork.connectionStateis gone.PhotonNetwork.isNonMasterClientInRoomis gone.PhotonNetwork.autoCleanUpPlayerObjectsis gone. The setting is per room, so it's now in the RoomOptions. Example:PhotonNetwork.CreateRoom(null, new RoomOptions() { CleanupCacheOnLeave = true });.PhotonNetwork.networkingPeeris gone. The tasks it covered are either now handled directly in the PhotonNetwork class or in thePhotonNetwork.NetworkingClient.PhotonNetwork.ConnectUsingSettings()no longer takesgameVersionparameter.
Instead the AppVersion parameter set from the Unity Editor in the PhotonServerSettings, AppSettings section is used asPhotonNetwork.GameVersion.
If you want to set thePhotonNetwork.GameVersionfrom code you could either- set
PhotonNetwork.GameVersionjust after callingPhotonNetwork.ConnectUsingSettings() - set
PhotonNetwork.PhotonServerSettings.AppSettings.AppVersionbefore callingPhotonNetwork.ConnectUsingSettings()
- set
Only
ConnectUsingSettings()is making use of PhotonServerSettings.
When using the other connect-methods, the app needs to set AppId and other values manually (and the settings won't override those values).ServerSettings.EnableLobbyStatisticsis moved toServerSettings.AppSettings.EnableLobbyStatistics.ServerSettings.AppIDis moved toServerSettings.AppSettings.AppIdRealtime.ServerSettings.VoiceAppIDis moved toServerSettings.AppSettings.AppIdVoice.ServerSettings.ChatAppIDis moved toServerSettings.AppSettings.AppIdChat.ServerSettings.NetworkLoggingis moved toServerSettings.AppSettings.NetworkLogging.ServerSettings.ServerAddressis moved toServerSettings.AppSettings.Server.ServerSettings.ServerPortis moved toServerSettings.AppSettings.Port.ServerSettings.Protocolis moved toServerSettings.AppSettings.Protocol.In PUN2, the UserId will no longer be set to the Nickname in case the former was not set.
This used to be the case for PUN1.PhotonNetwork.LoadLevelAsync()is gone.
PhotonNetwork.LoadLevelis always async by default and by design in PUN2.
To get the level loading progress, usePhotonNetwork.LevelLoadingProgress.PhotonNetwork.PhotonServerSettings.UseMyServer(server, port, app)is gone, it can be replaced with:C#
PhotonNetwork.PhotonServerSettings.AppSettings.Server = server; PhotonNetwork.PhotonServerSettings.AppSettings.Port = port; PhotonNetwork.PhotonServerSettings.AppSettings.RealtimeAppId = app; // either AppId or "master" or don't set anythingAlso, if you use Photon Server v4:
PhotonNetwork.PhotonServerSettings.AppSettings.UserNameServer = false;
If you use Photon Server v5 (and use the Name Server, which is recommended):
PhotonNetwork.PhotonServerSettings.AppSettings.UserNameServer = true;
Namespace Changes
RoomOptionsis now in thePhoton.RealtimenamespaceAuthenticationValuesis now in thePhoton.RealtimenamespaceRoomInfois now in thePhoton.RealtimenamespaceRoomis now in thePhoton.RealtimenamespaceFriendInfois now in thePhoton.RealtimenamespaceTypedLobbyInfois now in thePhoton.RealtimenamespaceTypedLobbyis now in thePhoton.RealtimenamespaceLobbyTypeis now in thePhoton.RealtimenamespaceRaiseEventOptionsis now in thePhoton.RealtimenamespaceWebRpcResponseis now in thePhoton.RealtimenamespaceIPunObservableis nowPhoton.Punnamespace
Renamed
camelCase to PascalCase
Public fields and properties:
photonView.isMineis nowphotonView.IsMinephotonView.owneris nowphotonView.OwnerphotonView.instantiationDatais nowphotonView.InstantiationDataPhotonNetwork.automaticallySyncSceneis nowPhotonNetwork.AutomaticallySyncScenePhotonNetwork.gameVersionis nowPhotonNetwork.GameVersionPhotonNetwork.masterClientis nowPhotonNetwork.MasterClientPhotonNetwork.isMasterClientis nowPhotonNetwork.IsMasterClientPhotonNetwork.inRoomis nowPhotonNetwork.InRoomPhotonNetwork.isMessageQueueRunningis nowPhotonNetwork.IsMessageQueueRunningPhotonNetwork.offlineModeis nowPhotonNetwork.OfflineModePhotonNetwork.countOfPlayersOnMasteris nowPhotonNetwork.CountOfPlayersOnMasterPhotonNetwork.countOfPlayersInRoomsis nowPhotonNetwork.CountOfPlayersInRoomsPhotonNetwork.countOfPlayersis nowPhotonNetwork.CountOfPlayersPhotonNetwork.countOfRoomsis nowPhotonNetwork.CountOfRoomsPhotonNetwork.sendRateis nowPhotonNetwork.SendRatePhotonNetwork.timeis nowPhotonNetwork.TimePhotonNetwork.playerListis nowPhotonNetwork.PlayerListPhotonNetwork.precisionForVectorSynchronizationis nowPhotonNetwork.PrecisionForVectorSynchronizationPhotonNetwork.precisionForQuaternionSynchronizationis nowPhotonNetwork.PrecisionForQuaternionSynchronizationPhotonNetwork.precisionForFloatSynchronizationis nowPhotonNetwork.PrecisionForFloatSynchronizationPhotonStream.isWritingis nowPhotonStream.IsWritingPhotonStream.isReadingis nowPhotonStream.IsReadingRoomOptions.cleanUpCacheOnLeaveis nowRoomOptions.CleanupCacheOnLeaveRoomOptions.publishUserIdis nowRoomOptions.PublishUserIdRoomOptions.suppressRoomEventsis nowRoomOptions.SuppressRoomEvents
Others
PhotonNetwork.connectedis nowPhotonNetwork.IsConnectedPhotonNetwork.connectedAndReadyis nowPhotonNetwork.IsConnectedAndReadyPhotonNetwork.networkingPeeris nowPhotonNetwork.NetworkingClientPhotonNetwork.connectionStateDetailedis nowPhotonNetwork.NetworkClientStatePhotonNetwork.playerNameis nowPhotonNetwork.NickNamePhotonNetwork.roomis nowPhotonNetwork.CurrentRoomPhotonNetwork.lobbyis nowPhotonNetwork.CurrentLobbyPhotonNetwork.playeris nowPhotonNetwork.LocalPlayerPhotonNetwork.insideLobbyis nowPhotonNetwork.InLobbyPhotonNetwork.otherPlayersis nowPhotonNetwork.PlayerListOthersPhotonNetwork.sendRateOnSerializeis nowPhotonNetwork.SerializationRatePhotonNetwork.versionPUNis nowPhotonNetwork.PunVersionPhotonTargetsenum is nowPhoton.Pun.RpcTargetPhotonPlayerclass is nowPhoton.Realtime.Player(constructor is no longer public)PhotonPlayer.IDis nowPhoton.Realtime.Player.ActorNumber
Callbacks Changes
You can read more about new callbacks on this page.
All callback interfaces except
IPunInstantiateMagicCallbackmust be registered and unregistered.
CallPhotonNetwork.AddCallbackTarget(this)andPhotonNetwork.RemoveCallbackTarget(this)(likely withinOnEnable()andOnDisable()respectivly)Replace
Photon.PunBehaviourwithMonoBehaviourPunCallbacks.
Call the base class methods in overrides ofOnEnable()andOnDisable()forMonoBehaviourPunCallbacksPhotonNetwork.OnEventCallis gone.
You have two options:- use
PhotonNetwork.NetworkingClient.EventReceived(EventData)instead. - use
IOnEventCallback.OnEvent(EventData).
In PUN2, events callback signature changed but you can get what you need from
EventData.
Also, events callback will be triggered for all events and not only custom events (custom event code < 200).- use
By default, setting properties for actor or room properties will not take effect on the sender/setter client (actor that sets the properties) immediately when joined to an online room unlike what it used to be in PUN Classic.
Now, instead, the sender/setter client (actor that sets the properties) will wait for the server eventPropertiesChangedto apply/set changes locally.
So you need to wait untilOnPlayerPropertiesUpdateorOnRoomPropertiesUpdatecallbacks are triggered for the local client in order to access them.
The new behaviour is due to the introduction of the new room option flagroomOptions.BroadcastPropsChangeToAllwhich is set totrueby default.
The reason behind this is that properties can easily go out of synchronization if we set them locally first and then send the request to do so on the server and for other actors in the room.
The latter might fail and we may end up with properties of the sender/setter client (actor that sets the properties) different locally from what's on the server or on other clients.
If you want to have the old behaviour (set properties locally before sending the request to the server to synchronize them) setroomOptions.BroadcastPropsChangeToAlltofalsebefore creating rooms.
But we highly recommend against doing this.PUN2 will not trigger
OnRoomPropertiesUpdatedorOnPlayerPropertiesUpdatedcallbacks unless those are changed via SetProperties calls.
In PUN2, when entering rooms (after creating, joining or re joining rooms)OnPlayerPropertiesUpdateorOnRoomPropertiesUpdatecallbacks will not be triggered for initialized properties.
You can access initial values viaPhotonNetwork.CurrentRoom.CustomProperties(or other class properties ofPhotonNetwork.CurrentRoom, likeIsOpen,IsVisible,MaxPlayers, etc.) orPhotonNetwork.LocalPlayer.CustomProperties.
| PUN 1 (Callback) | PUN 2 (Interface | Callback) | |
|---|---|---|
| OnConnectedToPhoton | IConnectionCallbacks | OnConnected |
| OnFailedToConnectToPhoton() | IConnectionCallbacks | OnDisconnected(DisconnectCause) |
| OnConnectionFail() | IConnectionCallbacks | OnDisconnected(DisconnectCause) |
| OnDisconnectedFromPhoton() | IConnectionCallbacks | OnDisconnected(DisconnectCause) |
| OnConnectedToMaster | IConnectionCallbacks | OnConnectedToMaster |
| OnPhotonMaxCccuReached() | IConnectionCallbacks | OnDisconnected(DisconnectCause) |
| OnCustomAuthenticationFailed | IConnectionCallbacks | OnCustomAuthenticationFailed |
| OnCustomAuthenticationResponse | IConnectionCallbacks | OnCustomAuthenticationResponse |
| OnMasterClientSwitched(PhotonPlayer) | IInRoomCallbacks | OnMasterClientSwitched(Player) |
| OnPhotonPlayerConnected(PhotonPlayer) | IInRoomCallbacks | OnPlayerEnteredRoom(Player) |
| OnPhotonPlayerDisconnected(PhotonPlayer) | IInRoomCallbacks | OnPlayerLeftRoom(Player) |
| OnPhotonPlayerActivityChanged(PhotonPlayer) | IInRoomCallbacks |
OnPlayerEnteredRoom(Player) OnPlayerLeftRoom(Player) |
| OnPhotonCustomRoomPropertiesChanged | IInRoomCallbacks | OnRoomPropertiesUpdate |
| OnPhotonPlayerPropertiesChanged(object[]) | IInRoomCallbacks | OnPlayerPropertiesUpdate(Player, Hashtable) |
| OnJoinedLobby | ILobbyCallbacks | OnJoinedLobby |
| OnLeftLobby | ILobbyCallbacks | OnLeftLobby |
| OnReceivedRoomListUpdate() | ILobbyCallbacks | OnRoomListUpdate(List<RoomInfo>) |
| OnLobbyStatisticsUpdate() | ILobbyCallbacks | OnLobbyStatisticsUpdate(List<TypedLobbyInfo>) |
| OnLeftRoom | IMatchmakingCallbacks | OnLeftRoom |
| OnPhotonCreateRoomFailed(object[]) | IMatchmakingCallbacks | OnCreateRoomFailed(short, string) |
| OnPhotonJoinRoomFailed(object[]) | IMatchmakingCallbacks | OnJoinRoomFailed(short, string) |
| OnCreatedRoom | IMatchmakingCallbacks | OnCreatedRoom |
| OnJoinedRoom | IMatchmakingCallbacks | OnJoinedRoom |
| OnPhotonRandomJoinFailed(object[]) | IMatchmakingCallbacks | OnJoinRandomFailed(short, string) |
| OnUpdatedFriendList() | IMatchmakingCallbacks | OnFriendListUpdate(List<FriendInfo>) |
| - | IOnEventCallback | OnEvent(EventData) |
| OnPhotonInstantiate | IPunInstantiateMagicCallback | OnPhotonInstantiate |
| OnPhotonSerializeView | IPunObservable | OnPhotonSerializeView |
| OnOwnershipRequest(object[]) | IPunOwnershipCallbacks | OnOwnershipRequest(PhotonView, Player) |
| OnOwnershipTransfered(object[]) | IPunOwnershipCallbacks | OnOwnershipTransfered(PhotonView, Player) |
| OnWebRpcResponse | IWebRpcCallback | OnWebRpcResponse |