C# Класс Photon.PunBehaviour

This class provides a .photonView and all callbacks/events that PUN can call. Override the events/methods you want to use.
By extending this class, you can implement individual methods as override. Visual Studio and MonoDevelop should provide the list of methods when you begin typing "override". Your implementation does not have to call "base.method()". This class implements IPunCallbacks, which is used as definition of all PUN callbacks. Don't implement IPunCallbacks in your classes. Instead, implent PunBehaviour or individual methods.
Наследование: Photon.MonoBehaviour, IPunCallbacks
Показать файл Открыть проект Примеры использования класса

Открытые методы

Метод Описание
OnConnectedToMaster ( ) : void

Called after the connection to the master is established and authenticated but only when PhotonNetwork.autoJoinLobby is false.

If you set PhotonNetwork.autoJoinLobby to true, OnJoinedLobby() will be called instead of this. You can join rooms and create them even without being in a lobby. The default lobby is used in that case. The list of available rooms won't become available unless you join a lobby via PhotonNetwork.joinLobby.

OnConnectedToPhoton ( ) : void

Called when the initial connection got established but before you can use the server. OnJoinedLobby() or OnConnectedToMaster() are called when PUN is ready.

This callback is only useful to detect if the server can be reached at all (technically). Most often, it's enough to implement OnFailedToConnectToPhoton() and OnDisconnectedFromPhoton(). OnJoinedLobby() or OnConnectedToMaster() are called when PUN is ready. When this is called, the low level connection is established and PUN will send your AppId, the user, etc in the background. This is not called for transitions from the masterserver to game servers.

OnConnectionFail ( DisconnectCause cause ) : void

Called when something causes the connection to fail (after it was established), followed by a call to OnDisconnectedFromPhoton().

If the server could not be reached in the first place, OnFailedToConnectToPhoton is called instead. The reason for the error is provided as DisconnectCause.

OnCreatedRoom ( ) : void

Called when this client created a room and entered it. OnJoinedRoom() will be called as well.

This callback is only called on the client which created a room (see PhotonNetwork.CreateRoom). As any client might close (or drop connection) anytime, there is a chance that the creator of a room does not execute OnCreatedRoom. If you need specific room properties or a "start signal", it is safer to implement OnMasterClientSwitched() and to make the new MasterClient check the room's state.

OnCustomAuthenticationFailed ( string debugMessage ) : void

Called when the custom authentication failed. Followed by disconnect!

Custom Authentication can fail due to user-input, bad tokens/secrets. If authentication is successful, this method is not called. Implement OnJoinedLobby() or OnConnectedToMaster() (as usual). During development of a game, it might also fail due to wrong configuration on the server side. In those cases, logging the debugMessage is very important. Unless you setup a custom authentication service for your app (in the [Dashboard](https://www.exitgames.com/dashboard)), this won't be called!

OnDisconnectedFromPhoton ( ) : void

Called after disconnecting from the Photon server.

In some cases, other callbacks are called before OnDisconnectedFromPhoton is called. Examples: OnConnectionFail() and OnFailedToConnectToPhoton().

OnFailedToConnectToPhoton ( DisconnectCause cause ) : void

Called if a connect call to the Photon server failed before the connection was established, followed by a call to OnDisconnectedFromPhoton().

This is called when no connection could be established at all. It differs from OnConnectionFail, which is called when an existing connection fails.

OnJoinedLobby ( ) : void

Called on entering a lobby on the Master Server. The actual room-list updates will call OnReceivedRoomListUpdate().

Note: When PhotonNetwork.autoJoinLobby is false, OnConnectedToMaster() will be called and the room list won't become available. While in the lobby, the roomlist is automatically updated in fixed intervals (which you can't modify). The room list gets available when OnReceivedRoomListUpdate() gets called after OnJoinedLobby().

OnJoinedRoom ( ) : void

Called when entering a room (by creating or joining it). Called on all clients (including the Master Client).

This method is commonly used to instantiate player characters. If a match has to be started "actively", you can call an [PunRPC](@ref PhotonView.RPC) triggered by a user's button-press or a timer. When this is called, you can usually already access the existing players in the room via PhotonNetwork.playerList. Also, all custom properties should be already available as Room.customProperties. Check Room.playerCount to find out if enough players are in the room to start playing.

OnLeftLobby ( ) : void

Called after leaving a lobby.

When you leave a lobby, [CreateRoom](@ref PhotonNetwork.CreateRoom) and [JoinRandomRoom](@ref PhotonNetwork.JoinRandomRoom) automatically refer to the default lobby.

OnLeftRoom ( ) : void

Called when the local user/client left a room.

When leaving a room, PUN brings you back to the Master Server. Before you can use lobbies and join or create rooms, OnJoinedLobby() or OnConnectedToMaster() will get called again.

OnLobbyStatisticsUpdate ( ) : void

Called when the Master Server sent an update for the Lobby Statistics, updating PhotonNetwork.LobbyStatistics.

This callback has two preconditions: EnableLobbyStatistics must be set to true, before this client connects. And the client has to be connected to the Master Server, which is providing the info about lobbies.

OnMasterClientSwitched ( PhotonPlayer newMasterClient ) : void

Called after switching to a new MasterClient when the current one leaves.

This is not called when this client enters a room. The former MasterClient is still in the player list when this method get called.

OnOwnershipRequest ( object viewAndPlayer ) : void

Called when another player requests ownership of a PhotonView from you (the current owner).

The parameter viewAndPlayer contains: PhotonView view = viewAndPlayer[0] as PhotonView; PhotonPlayer requestingPlayer = viewAndPlayer[1] as PhotonPlayer;

OnPhotonCreateRoomFailed ( object codeAndMsg ) : void

Called when a CreateRoom() call failed. The parameter provides ErrorCode and message (as array).

Most likely because the room name is already in use (some other client was faster than you). PUN logs some info if the PhotonNetwork.logLevel is >= PhotonLogLevel.Informational.

OnPhotonCustomRoomPropertiesChanged ( ExitGames.Client.Photon.Hashtable propertiesThatChanged ) : void

Called when a room's custom properties changed. The propertiesThatChanged contains all that was set via Room.SetCustomProperties.

Since v1.25 this method has one parameter: Hashtable propertiesThatChanged.
Changing properties must be done by Room.SetCustomProperties, which causes this callback locally, too.

OnPhotonInstantiate ( PhotonMessageInfo info ) : void

Called on all scripts on a GameObject (and children) that have been Instantiated using PhotonNetwork.Instantiate.

PhotonMessageInfo parameter provides info about who created the object and when (based off PhotonNetworking.time).

OnPhotonJoinRoomFailed ( object codeAndMsg ) : void

Called when a JoinRoom() call failed. The parameter provides ErrorCode and message (as array).

Most likely error is that the room does not exist or the room is full (some other client was faster than you). PUN logs some info if the PhotonNetwork.logLevel is >= PhotonLogLevel.Informational.

OnPhotonMaxCccuReached ( ) : void

Because the concurrent user limit was (temporarily) reached, this client is rejected by the server and disconnecting.

When this happens, the user might try again later. You can't create or join rooms in OnPhotonMaxCcuReached(), cause the client will be disconnecting. You can raise the CCU limits with a new license (when you host yourself) or extended subscription (when using the Photon Cloud). The Photon Cloud will mail you when the CCU limit was reached. This is also visible in the Dashboard (webpage).

OnPhotonPlayerConnected ( PhotonPlayer newPlayer ) : void

Called when a remote player entered the room. This PhotonPlayer is already added to the playerlist at this time.

If your game starts with a certain number of players, this callback can be useful to check the Room.playerCount and find out if you can start.

OnPhotonPlayerDisconnected ( PhotonPlayer otherPlayer ) : void

Called when a remote player left the room. This PhotonPlayer is already removed from the playerlist at this time.

When your client calls PhotonNetwork.leaveRoom, PUN will call this method on the remaining clients. When a remote client drops connection or gets closed, this callback gets executed. after a timeout of several seconds.

OnPhotonPlayerPropertiesChanged ( object playerAndUpdatedProps ) : void

Called when custom player-properties are changed. Player and the changed properties are passed as object[].

Since v1.25 this method has one parameter: object[] playerAndUpdatedProps, which contains two entries.
[0] is the affected PhotonPlayer.
[1] is the Hashtable of properties that changed.
We are using a object[] due to limitations of Unity's GameObject.SendMessage (which has only one optional parameter). Changing properties must be done by PhotonPlayer.SetCustomProperties, which causes this callback locally, too. Example:

 void OnPhotonPlayerPropertiesChanged(object[] playerAndUpdatedProps) { PhotonPlayer player = playerAndUpdatedProps[0] as PhotonPlayer; Hashtable props = playerAndUpdatedProps[1] as Hashtable; //... }
OnPhotonRandomJoinFailed ( object codeAndMsg ) : void

Called when a JoinRandom() call failed. The parameter provides ErrorCode and message.

Most likely all rooms are full or no rooms are available.
When using multiple lobbies (via JoinLobby or TypedLobby), another lobby might have more/fitting rooms.
PUN logs some info if the PhotonNetwork.logLevel is >= PhotonLogLevel.Informational.

OnReceivedRoomListUpdate ( ) : void

Called for any update of the room-listing while in a lobby (PhotonNetwork.insideLobby) on the Master Server.

PUN provides the list of rooms by PhotonNetwork.GetRoomList().
Each item is a RoomInfo which might include custom properties (provided you defined those as lobby-listed when creating a room). Not all types of lobbies provide a listing of rooms to the client. Some are silent and specialized for server-side matchmaking.

OnUpdatedFriendList ( ) : void

Called when the server sent the response to a FindFriends request and updated PhotonNetwork.Friends.

The friends list is available as PhotonNetwork.Friends, listing name, online state and the room a user is in (if any).

OnWebRpcResponse ( OperationResponse response ) : void

Called by PUN when the response to a WebRPC is available. See PhotonNetwork.WebRPC.

Important: The response.ReturnCode is 0 if Photon was able to reach your web-service. The content of the response is what your web-service sent. You can create a WebResponse instance from it. Example: WebRpcResponse webResponse = new WebRpcResponse(operationResponse); Please note: Class OperationResponse is in a namespace which needs to be "used": using ExitGames.Client.Photon; // includes OperationResponse (and other classes) The OperationResponse.ReturnCode by Photon is:

 0 for "OK" -3 for "Web-Service not configured" (see Dashboard / WebHooks) -5 for "Web-Service does now have RPC path/name" (at least for Azure)

Описание методов

OnConnectedToMaster() публичный Метод

Called after the connection to the master is established and authenticated but only when PhotonNetwork.autoJoinLobby is false.
If you set PhotonNetwork.autoJoinLobby to true, OnJoinedLobby() will be called instead of this. You can join rooms and create them even without being in a lobby. The default lobby is used in that case. The list of available rooms won't become available unless you join a lobby via PhotonNetwork.joinLobby.
public OnConnectedToMaster ( ) : void
Результат void

OnConnectedToPhoton() публичный Метод

Called when the initial connection got established but before you can use the server. OnJoinedLobby() or OnConnectedToMaster() are called when PUN is ready.
This callback is only useful to detect if the server can be reached at all (technically). Most often, it's enough to implement OnFailedToConnectToPhoton() and OnDisconnectedFromPhoton(). OnJoinedLobby() or OnConnectedToMaster() are called when PUN is ready. When this is called, the low level connection is established and PUN will send your AppId, the user, etc in the background. This is not called for transitions from the masterserver to game servers.
public OnConnectedToPhoton ( ) : void
Результат void

OnConnectionFail() публичный Метод

Called when something causes the connection to fail (after it was established), followed by a call to OnDisconnectedFromPhoton().
If the server could not be reached in the first place, OnFailedToConnectToPhoton is called instead. The reason for the error is provided as DisconnectCause.
public OnConnectionFail ( DisconnectCause cause ) : void
cause DisconnectCause
Результат void

OnCreatedRoom() публичный Метод

Called when this client created a room and entered it. OnJoinedRoom() will be called as well.
This callback is only called on the client which created a room (see PhotonNetwork.CreateRoom). As any client might close (or drop connection) anytime, there is a chance that the creator of a room does not execute OnCreatedRoom. If you need specific room properties or a "start signal", it is safer to implement OnMasterClientSwitched() and to make the new MasterClient check the room's state.
public OnCreatedRoom ( ) : void
Результат void

OnCustomAuthenticationFailed() публичный Метод

Called when the custom authentication failed. Followed by disconnect!
Custom Authentication can fail due to user-input, bad tokens/secrets. If authentication is successful, this method is not called. Implement OnJoinedLobby() or OnConnectedToMaster() (as usual). During development of a game, it might also fail due to wrong configuration on the server side. In those cases, logging the debugMessage is very important. Unless you setup a custom authentication service for your app (in the [Dashboard](https://www.exitgames.com/dashboard)), this won't be called!
public OnCustomAuthenticationFailed ( string debugMessage ) : void
debugMessage string Contains a debug message why authentication failed. This has to be fixed during development time.
Результат void

OnDisconnectedFromPhoton() публичный Метод

Called after disconnecting from the Photon server.
In some cases, other callbacks are called before OnDisconnectedFromPhoton is called. Examples: OnConnectionFail() and OnFailedToConnectToPhoton().
public OnDisconnectedFromPhoton ( ) : void
Результат void

OnFailedToConnectToPhoton() публичный Метод

Called if a connect call to the Photon server failed before the connection was established, followed by a call to OnDisconnectedFromPhoton().
This is called when no connection could be established at all. It differs from OnConnectionFail, which is called when an existing connection fails.
public OnFailedToConnectToPhoton ( DisconnectCause cause ) : void
cause DisconnectCause
Результат void

OnJoinedLobby() публичный Метод

Called on entering a lobby on the Master Server. The actual room-list updates will call OnReceivedRoomListUpdate().
Note: When PhotonNetwork.autoJoinLobby is false, OnConnectedToMaster() will be called and the room list won't become available. While in the lobby, the roomlist is automatically updated in fixed intervals (which you can't modify). The room list gets available when OnReceivedRoomListUpdate() gets called after OnJoinedLobby().
public OnJoinedLobby ( ) : void
Результат void

OnJoinedRoom() публичный Метод

Called when entering a room (by creating or joining it). Called on all clients (including the Master Client).
This method is commonly used to instantiate player characters. If a match has to be started "actively", you can call an [PunRPC](@ref PhotonView.RPC) triggered by a user's button-press or a timer. When this is called, you can usually already access the existing players in the room via PhotonNetwork.playerList. Also, all custom properties should be already available as Room.customProperties. Check Room.playerCount to find out if enough players are in the room to start playing.
public OnJoinedRoom ( ) : void
Результат void

OnLeftLobby() публичный Метод

Called after leaving a lobby.
When you leave a lobby, [CreateRoom](@ref PhotonNetwork.CreateRoom) and [JoinRandomRoom](@ref PhotonNetwork.JoinRandomRoom) automatically refer to the default lobby.
public OnLeftLobby ( ) : void
Результат void

OnLeftRoom() публичный Метод

Called when the local user/client left a room.
When leaving a room, PUN brings you back to the Master Server. Before you can use lobbies and join or create rooms, OnJoinedLobby() or OnConnectedToMaster() will get called again.
public OnLeftRoom ( ) : void
Результат void

OnLobbyStatisticsUpdate() публичный Метод

Called when the Master Server sent an update for the Lobby Statistics, updating PhotonNetwork.LobbyStatistics.
This callback has two preconditions: EnableLobbyStatistics must be set to true, before this client connects. And the client has to be connected to the Master Server, which is providing the info about lobbies.
public OnLobbyStatisticsUpdate ( ) : void
Результат void

OnMasterClientSwitched() публичный Метод

Called after switching to a new MasterClient when the current one leaves.
This is not called when this client enters a room. The former MasterClient is still in the player list when this method get called.
public OnMasterClientSwitched ( PhotonPlayer newMasterClient ) : void
newMasterClient PhotonPlayer
Результат void

OnOwnershipRequest() публичный Метод

Called when another player requests ownership of a PhotonView from you (the current owner).
The parameter viewAndPlayer contains: PhotonView view = viewAndPlayer[0] as PhotonView; PhotonPlayer requestingPlayer = viewAndPlayer[1] as PhotonPlayer;
public OnOwnershipRequest ( object viewAndPlayer ) : void
viewAndPlayer object The PhotonView is viewAndPlayer[0] and the requesting player is viewAndPlayer[1].
Результат void

OnPhotonCreateRoomFailed() публичный Метод

Called when a CreateRoom() call failed. The parameter provides ErrorCode and message (as array).
Most likely because the room name is already in use (some other client was faster than you). PUN logs some info if the PhotonNetwork.logLevel is >= PhotonLogLevel.Informational.
public OnPhotonCreateRoomFailed ( object codeAndMsg ) : void
codeAndMsg object codeAndMsg[0] is a short ErrorCode and codeAndMsg[1] is a string debug msg.
Результат void

OnPhotonCustomRoomPropertiesChanged() публичный Метод

Called when a room's custom properties changed. The propertiesThatChanged contains all that was set via Room.SetCustomProperties.
Since v1.25 this method has one parameter: Hashtable propertiesThatChanged.
Changing properties must be done by Room.SetCustomProperties, which causes this callback locally, too.
public OnPhotonCustomRoomPropertiesChanged ( ExitGames.Client.Photon.Hashtable propertiesThatChanged ) : void
propertiesThatChanged ExitGames.Client.Photon.Hashtable
Результат void

OnPhotonInstantiate() публичный Метод

Called on all scripts on a GameObject (and children) that have been Instantiated using PhotonNetwork.Instantiate.
PhotonMessageInfo parameter provides info about who created the object and when (based off PhotonNetworking.time).
public OnPhotonInstantiate ( PhotonMessageInfo info ) : void
info PhotonMessageInfo
Результат void

OnPhotonJoinRoomFailed() публичный Метод

Called when a JoinRoom() call failed. The parameter provides ErrorCode and message (as array).
Most likely error is that the room does not exist or the room is full (some other client was faster than you). PUN logs some info if the PhotonNetwork.logLevel is >= PhotonLogLevel.Informational.
public OnPhotonJoinRoomFailed ( object codeAndMsg ) : void
codeAndMsg object codeAndMsg[0] is short ErrorCode. codeAndMsg[1] is string debug msg.
Результат void

OnPhotonMaxCccuReached() публичный Метод

Because the concurrent user limit was (temporarily) reached, this client is rejected by the server and disconnecting.
When this happens, the user might try again later. You can't create or join rooms in OnPhotonMaxCcuReached(), cause the client will be disconnecting. You can raise the CCU limits with a new license (when you host yourself) or extended subscription (when using the Photon Cloud). The Photon Cloud will mail you when the CCU limit was reached. This is also visible in the Dashboard (webpage).
public OnPhotonMaxCccuReached ( ) : void
Результат void

OnPhotonPlayerConnected() публичный Метод

Called when a remote player entered the room. This PhotonPlayer is already added to the playerlist at this time.
If your game starts with a certain number of players, this callback can be useful to check the Room.playerCount and find out if you can start.
public OnPhotonPlayerConnected ( PhotonPlayer newPlayer ) : void
newPlayer PhotonPlayer
Результат void

OnPhotonPlayerDisconnected() публичный Метод

Called when a remote player left the room. This PhotonPlayer is already removed from the playerlist at this time.
When your client calls PhotonNetwork.leaveRoom, PUN will call this method on the remaining clients. When a remote client drops connection or gets closed, this callback gets executed. after a timeout of several seconds.
public OnPhotonPlayerDisconnected ( PhotonPlayer otherPlayer ) : void
otherPlayer PhotonPlayer
Результат void

OnPhotonPlayerPropertiesChanged() публичный Метод

Called when custom player-properties are changed. Player and the changed properties are passed as object[].
Since v1.25 this method has one parameter: object[] playerAndUpdatedProps, which contains two entries.
[0] is the affected PhotonPlayer.
[1] is the Hashtable of properties that changed.
We are using a object[] due to limitations of Unity's GameObject.SendMessage (which has only one optional parameter). Changing properties must be done by PhotonPlayer.SetCustomProperties, which causes this callback locally, too. Example:
 void OnPhotonPlayerPropertiesChanged(object[] playerAndUpdatedProps) { PhotonPlayer player = playerAndUpdatedProps[0] as PhotonPlayer; Hashtable props = playerAndUpdatedProps[1] as Hashtable; //... }
public OnPhotonPlayerPropertiesChanged ( object playerAndUpdatedProps ) : void
playerAndUpdatedProps object Contains PhotonPlayer and the properties that changed See remarks.
Результат void

OnPhotonRandomJoinFailed() публичный Метод

Called when a JoinRandom() call failed. The parameter provides ErrorCode and message.
Most likely all rooms are full or no rooms are available.
When using multiple lobbies (via JoinLobby or TypedLobby), another lobby might have more/fitting rooms.
PUN logs some info if the PhotonNetwork.logLevel is >= PhotonLogLevel.Informational.
public OnPhotonRandomJoinFailed ( object codeAndMsg ) : void
codeAndMsg object codeAndMsg[0] is short ErrorCode. codeAndMsg[1] is string debug msg.
Результат void

OnReceivedRoomListUpdate() публичный Метод

Called for any update of the room-listing while in a lobby (PhotonNetwork.insideLobby) on the Master Server.
PUN provides the list of rooms by PhotonNetwork.GetRoomList().
Each item is a RoomInfo which might include custom properties (provided you defined those as lobby-listed when creating a room). Not all types of lobbies provide a listing of rooms to the client. Some are silent and specialized for server-side matchmaking.
public OnReceivedRoomListUpdate ( ) : void
Результат void

OnUpdatedFriendList() публичный Метод

Called when the server sent the response to a FindFriends request and updated PhotonNetwork.Friends.
The friends list is available as PhotonNetwork.Friends, listing name, online state and the room a user is in (if any).
public OnUpdatedFriendList ( ) : void
Результат void

OnWebRpcResponse() публичный Метод

Called by PUN when the response to a WebRPC is available. See PhotonNetwork.WebRPC.
Important: The response.ReturnCode is 0 if Photon was able to reach your web-service. The content of the response is what your web-service sent. You can create a WebResponse instance from it. Example: WebRpcResponse webResponse = new WebRpcResponse(operationResponse); Please note: Class OperationResponse is in a namespace which needs to be "used": using ExitGames.Client.Photon; // includes OperationResponse (and other classes) The OperationResponse.ReturnCode by Photon is:
 0 for "OK" -3 for "Web-Service not configured" (see Dashboard / WebHooks) -5 for "Web-Service does now have RPC path/name" (at least for Azure)
public OnWebRpcResponse ( OperationResponse response ) : void
response OperationResponse
Результат void