Photon Unity Networking 2 2.45

Public Types | Public Member Functions | Public Attributes | Protected Member Functions | Protected Attributes | List of all members
OnJoinedInstantiate Class Reference

This component will instantiate a network GameObject when a room is joined More...

Inherits MonoBehaviour, and IMatchmakingCallbacks.

Public Types

enum  SpawnSequence
 

Public Member Functions

virtual void OnEnable ()
 
virtual void OnDisable ()
 
virtual void OnJoinedRoom ()
 Called when the LoadBalancingClient entered a room, no matter if this client created it or simply joined. More...
 
virtual void SpawnObjects ()
 
virtual void DespawnObjects (bool localOnly)
 Destroy all objects that have been spawned by this component for this client. More...
 
virtual void OnFriendListUpdate (List< FriendInfo > friendList)
 Called when the server sent the response to a FindFriends request. More...
 
virtual void OnCreatedRoom ()
 Called when this client created a room and entered it. OnJoinedRoom() will be called as well. More...
 
virtual void OnCreateRoomFailed (short returnCode, string message)
 Called when the server couldn't create a room (OpCreateRoom failed). More...
 
virtual void OnJoinRoomFailed (short returnCode, string message)
 Called when a previous OpJoinRoom call failed on the server. More...
 
virtual void OnJoinRandomFailed (short returnCode, string message)
 Called when a previous OpJoinRandom (or OpJoinRandomOrCreateRoom etc.) call failed on the server. More...
 
virtual void OnLeftRoom ()
 Called when the local user/client left a room, so the game's logic can clean up it's internal state. More...
 
virtual void GetSpawnPoint (out Vector3 spawnPos, out Quaternion spawnRot)
 Gets the next SpawnPoint from the list using the SpawnSequence, and applies RandomOffset (if used) to the transform matrix. Override this method with any custom code for coming up with a spawn location. This method is used by AutoSpawn. More...
 

Public Attributes

SpawnSequence Sequence = SpawnSequence.Connection
 
List< Transform > SpawnPoints = new List<Transform>(1) { null }
 
bool UseRandomOffset = true
 
float RandomOffset = 2.0f
 
bool ClampY = true
 
List< GameObject > PrefabsToInstantiate = new List<GameObject>(1) { null }
 
bool AutoSpawnObjects = true
 
Stack< GameObject > SpawnedObjects = new Stack<GameObject>()
 

Protected Member Functions

virtual Transform GetSpawnPoint ()
 Get the transform of the next SpawnPoint from the list, selected using the SpawnSequence setting. RandomOffset is not applied, only the transform of the SpawnPoint is returned. Override this method to change how Spawn Point transform is selected. Return the transform you want to use as a spawn point. More...
 
virtual Vector3 GetRandomOffset ()
 When UseRandomeOffset is enabled, this method is called to produce a Vector3 offset. The default implementation clamps the Y value to zero. You may override this with your own implementation. More...
 

Protected Attributes

int spawnedAsActorId
 
int lastUsedSpawnPointIndex = -1
 

Detailed Description

This component will instantiate a network GameObject when a room is joined

Member Function Documentation

◆ DespawnObjects()

virtual void DespawnObjects ( bool  localOnly)
virtual

Destroy all objects that have been spawned by this component for this client.

Parameters
localOnlyUse Object.Destroy rather than PhotonNetwork.Destroy.

◆ GetRandomOffset()

virtual Vector3 GetRandomOffset ( )
protectedvirtual

When UseRandomeOffset is enabled, this method is called to produce a Vector3 offset. The default implementation clamps the Y value to zero. You may override this with your own implementation.

◆ GetSpawnPoint() [1/2]

virtual Transform GetSpawnPoint ( )
protectedvirtual

Get the transform of the next SpawnPoint from the list, selected using the SpawnSequence setting. RandomOffset is not applied, only the transform of the SpawnPoint is returned. Override this method to change how Spawn Point transform is selected. Return the transform you want to use as a spawn point.

Returns

◆ GetSpawnPoint() [2/2]

virtual void GetSpawnPoint ( out Vector3  spawnPos,
out Quaternion  spawnRot 
)
virtual

Gets the next SpawnPoint from the list using the SpawnSequence, and applies RandomOffset (if used) to the transform matrix. Override this method with any custom code for coming up with a spawn location. This method is used by AutoSpawn.

◆ OnCreatedRoom()

virtual void OnCreatedRoom ( )
virtual

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 OpCreateRoom).

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", implement OnMasterClientSwitched() and make each new MasterClient check the room's state.

Implements IMatchmakingCallbacks.

◆ OnCreateRoomFailed()

virtual void OnCreateRoomFailed ( short  returnCode,
string  message 
)
virtual

Called when the server couldn't create a room (OpCreateRoom failed).

Creating a room may fail for various reasons. Most often, the room already exists (roomname in use) or the RoomOptions clash and it's impossible to create the room.

When creating a room fails on a Game Server: The client will cache the failure internally and returns to the Master Server before it calls the fail-callback. This way, the client is ready to find/create a room at the moment of the callback. In this case, the client skips calling OnConnectedToMaster but returning to the Master Server will still call OnConnected. Treat callbacks of OnConnected as pure information that the client could connect.

Parameters
returnCodeOperation ReturnCode from the server.
messageDebug message for the error.

Implements IMatchmakingCallbacks.

◆ OnFriendListUpdate()

virtual void OnFriendListUpdate ( List< FriendInfo friendList)
virtual

Called when the server sent the response to a FindFriends request.

After calling OpFindFriends, the Master Server will cache the friend list and send updates to the friend list. The friends includes the name, userId, online state and the room (if any) for each requested user/friend.

Use the friendList to update your UI and store it, if the UI should highlight changes.

Implements IMatchmakingCallbacks.

◆ OnJoinedRoom()

virtual void OnJoinedRoom ( )
virtual

Called when the LoadBalancingClient entered a room, no matter if this client created it or simply joined.

When this is called, you can access the existing players in Room.Players, their custom properties and Room.CustomProperties.

In this callback, you could create player objects. For example in Unity, instantiate a prefab for the player.

If you want a match to be started "actively", enable the user to signal "ready" (using OpRaiseEvent or a Custom Property).

Implements IMatchmakingCallbacks.

◆ OnJoinRandomFailed()

virtual void OnJoinRandomFailed ( short  returnCode,
string  message 
)
virtual

Called when a previous OpJoinRandom (or OpJoinRandomOrCreateRoom etc.) call failed on the server.

The most common causes are that a room is full or does not exist (due to someone else being faster or closing the room).

This operation is only ever sent to the Master Server. Once a room is found by the Master Server, the client will head off to the designated Game Server and use the operation Join on the Game Server.

When using multiple lobbies (via OpJoinLobby or a TypedLobby parameter), another lobby might have more/fitting rooms.

Parameters
returnCodeOperation ReturnCode from the server.
messageDebug message for the error.

Implements IMatchmakingCallbacks.

◆ OnJoinRoomFailed()

virtual void OnJoinRoomFailed ( short  returnCode,
string  message 
)
virtual

Called when a previous OpJoinRoom call failed on the server.

Joining a room may fail for various reasons. Most often, the room is full or does not exist anymore (due to someone else being faster or closing the room).

When joining a room fails on a Game Server: The client will cache the failure internally and returns to the Master Server before it calls the fail-callback. This way, the client is ready to find/create a room at the moment of the callback. In this case, the client skips calling OnConnectedToMaster but returning to the Master Server will still call OnConnected. Treat callbacks of OnConnected as pure information that the client could connect.

Parameters
returnCodeOperation ReturnCode from the server.
messageDebug message for the error.

Implements IMatchmakingCallbacks.

◆ OnLeftRoom()

virtual void OnLeftRoom ( )
virtual

Called when the local user/client left a room, so the game's logic can clean up it's internal state.

When leaving a room, the LoadBalancingClient will disconnect the Game Server and connect to the Master Server. This wraps up multiple internal actions.

Wait for the callback OnConnectedToMaster, before you use lobbies and join or create rooms.

OnLeftRoom also gets called, when the application quits. It makes sense to check static ConnectionHandler.AppQuits before loading scenes in OnLeftRoom().

Implements IMatchmakingCallbacks.