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.Pun
andPhoton.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.
MonoBehaviourPunCallbacks
implements most callbacks. Inherit this class and override as needed. Call base in OnEnable and OnDisable. Read more here.
API Changes
PhotonNetwork.autoJoinLobby
andServerSettings.JoinLobby
are 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.Friends
is gone. You get friends list fromIMatchmakingCallbacks.OnFriendListUpdate(List<FriendInfo> friendList)
callback. You can optionally cache it, update it and clear it when needed.PhotonNetwork.LobbyStatistics
is 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.connecting
is gone.PhotonNetwork.connectionState
is gone.PhotonNetwork.isNonMasterClientInRoom
is gone.PhotonNetwork.autoCleanUpPlayerObjects
is gone. The setting is per room, so it's now in the RoomOptions. Example:PhotonNetwork.CreateRoom(null, new RoomOptions() { CleanupCacheOnLeave = true });
.PhotonNetwork.networkingPeer
is gone. The tasks it covered are either now handled directly in the PhotonNetwork class or in thePhotonNetwork.NetworkingClient
.PhotonNetwork.ConnectUsingSettings()
no longer takesgameVersion
parameter.
Instead the AppVersion parameter set from the Unity Editor in the PhotonServerSettings, AppSettings section is used asPhotonNetwork.GameVersion
.
If you want to set thePhotonNetwork.GameVersion
from code you could either- set
PhotonNetwork.GameVersion
just after callingPhotonNetwork.ConnectUsingSettings()
- set
PhotonNetwork.PhotonServerSettings.AppSettings.AppVersion
before 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.EnableLobbyStatistics
is moved toServerSettings.AppSettings.EnableLobbyStatistics
.ServerSettings.AppID
is moved toServerSettings.AppSettings.AppIdRealtime
.ServerSettings.VoiceAppID
is moved toServerSettings.AppSettings.AppIdVoice
.ServerSettings.ChatAppID
is moved toServerSettings.AppSettings.AppIdChat
.ServerSettings.NetworkLogging
is moved toServerSettings.AppSettings.NetworkLogging
.ServerSettings.ServerAddress
is moved toServerSettings.AppSettings.Server
.ServerSettings.ServerPort
is moved toServerSettings.AppSettings.Port
.ServerSettings.Protocol
is 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.LoadLevel
is 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 anything
Also, 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
RoomOptions
is now in thePhoton.Realtime
namespaceAuthenticationValues
is now in thePhoton.Realtime
namespaceRoomInfo
is now in thePhoton.Realtime
namespaceRoom
is now in thePhoton.Realtime
namespaceFriendInfo
is now in thePhoton.Realtime
namespaceTypedLobbyInfo
is now in thePhoton.Realtime
namespaceTypedLobby
is now in thePhoton.Realtime
namespaceLobbyType
is now in thePhoton.Realtime
namespaceRaiseEventOptions
is now in thePhoton.Realtime
namespaceWebRpcResponse
is now in thePhoton.Realtime
namespaceIPunObservable
is nowPhoton.Pun
namespace
Renamed
camelCase to PascalCase
Public fields and properties:
photonView.isMine
is nowphotonView.IsMine
photonView.owner
is nowphotonView.Owner
photonView.instantiationData
is nowphotonView.InstantiationData
PhotonNetwork.automaticallySyncScene
is nowPhotonNetwork.AutomaticallySyncScene
PhotonNetwork.gameVersion
is nowPhotonNetwork.GameVersion
PhotonNetwork.masterClient
is nowPhotonNetwork.MasterClient
PhotonNetwork.isMasterClient
is nowPhotonNetwork.IsMasterClient
PhotonNetwork.inRoom
is nowPhotonNetwork.InRoom
PhotonNetwork.isMessageQueueRunning
is nowPhotonNetwork.IsMessageQueueRunning
PhotonNetwork.offlineMode
is nowPhotonNetwork.OfflineMode
PhotonNetwork.countOfPlayersOnMaster
is nowPhotonNetwork.CountOfPlayersOnMaster
PhotonNetwork.countOfPlayersInRooms
is nowPhotonNetwork.CountOfPlayersInRooms
PhotonNetwork.countOfPlayers
is nowPhotonNetwork.CountOfPlayers
PhotonNetwork.countOfRooms
is nowPhotonNetwork.CountOfRooms
PhotonNetwork.sendRate
is nowPhotonNetwork.SendRate
PhotonNetwork.time
is nowPhotonNetwork.Time
PhotonNetwork.playerList
is nowPhotonNetwork.PlayerList
PhotonNetwork.precisionForVectorSynchronization
is nowPhotonNetwork.PrecisionForVectorSynchronization
PhotonNetwork.precisionForQuaternionSynchronization
is nowPhotonNetwork.PrecisionForQuaternionSynchronization
PhotonNetwork.precisionForFloatSynchronization
is nowPhotonNetwork.PrecisionForFloatSynchronization
PhotonStream.isWriting
is nowPhotonStream.IsWriting
PhotonStream.isReading
is nowPhotonStream.IsReading
RoomOptions.cleanUpCacheOnLeave
is nowRoomOptions.CleanupCacheOnLeave
RoomOptions.publishUserId
is nowRoomOptions.PublishUserId
RoomOptions.suppressRoomEvents
is nowRoomOptions.SuppressRoomEvents
Others
PhotonNetwork.connected
is nowPhotonNetwork.IsConnected
PhotonNetwork.connectedAndReady
is nowPhotonNetwork.IsConnectedAndReady
PhotonNetwork.networkingPeer
is nowPhotonNetwork.NetworkingClient
PhotonNetwork.connectionStateDetailed
is nowPhotonNetwork.NetworkClientState
PhotonNetwork.playerName
is nowPhotonNetwork.NickName
PhotonNetwork.room
is nowPhotonNetwork.CurrentRoom
PhotonNetwork.lobby
is nowPhotonNetwork.CurrentLobby
PhotonNetwork.player
is nowPhotonNetwork.LocalPlayer
PhotonNetwork.insideLobby
is nowPhotonNetwork.InLobby
PhotonNetwork.otherPlayers
is nowPhotonNetwork.PlayerListOthers
PhotonNetwork.sendRateOnSerialize
is nowPhotonNetwork.SerializationRate
PhotonNetwork.versionPUN
is nowPhotonNetwork.PunVersion
PhotonTargets
enum is nowPhoton.Pun.RpcTarget
PhotonPlayer
class is nowPhoton.Realtime.Player
(constructor is no longer public)PhotonPlayer.ID
is nowPhoton.Realtime.Player.ActorNumber
Callbacks Changes
You can read more about new callbacks on this page.
All callback interfaces except
IPunInstantiateMagicCallback
must be registered and unregistered.
CallPhotonNetwork.AddCallbackTarget(this)
andPhotonNetwork.RemoveCallbackTarget(this)
(likely withinOnEnable()
andOnDisable()
respectivly)Replace
Photon.PunBehaviour
withMonoBehaviourPunCallbacks
.
Call the base class methods in overrides ofOnEnable()
andOnDisable()
forMonoBehaviourPunCallbacks
PhotonNetwork.OnEventCall
is 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 eventPropertiesChanged
to apply/set changes locally.
So you need to wait untilOnPlayerPropertiesUpdate
orOnRoomPropertiesUpdate
callbacks 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.BroadcastPropsChangeToAll
which is set totrue
by 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.BroadcastPropsChangeToAll
tofalse
before creating rooms.
But we highly recommend against doing this.PUN2 will not trigger
OnRoomPropertiesUpdated
orOnPlayerPropertiesUpdated
callbacks unless those are changed via SetProperties calls.
In PUN2, when entering rooms (after creating, joining or re joining rooms)OnPlayerPropertiesUpdate
orOnRoomPropertiesUpdate
callbacks 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 |