/// <summary> /// Creates a new room. Will callback: OnCreatedRoom and OnJoinedRoom or OnCreateRoomFailed. /// </summary> /// <remarks> /// When successful, this calls the callbacks OnCreatedRoom and OnJoinedRoom (the latter, cause you join as first player). /// In all error cases, OnCreateRoomFailed gets called. /// /// Creating a room will fail if the room name is already in use or when the RoomOptions clashing /// with one another. Check the EnterRoomParams reference for the various room creation options. /// /// If you don't want to create a unique room-name, pass null or "" as name and the server will assign a roomName (a GUID as string). /// /// This method can only be called while the client is connected to a Master Server so you should /// implement the callback OnConnectedToMaster. /// Check the return value to make sure the operation will be called on the server. /// Note: There will be no callbacks if this method returned false. /// /// More about PUN matchmaking: /// https://doc.photonengine.com/en-us/pun/v2/lobby-and-matchmaking/matchmaking-and-lobby /// </remarks> /// <param name="roomName">Unique name of the room to create. Pass null or "" to make the server generate a name.</param> /// <param name="roomOptions">Common options for the room like MaxPlayers, initial custom room properties and similar. See RoomOptions type..</param> /// <param name="typedLobby">If null, the room is automatically created in the currently used lobby (which is "default" when you didn't join one explicitly).</param> /// <param name="expectedUsers">Optional list of users (by UserId) who are expected to join this game and who you want to block a slot for.</param> public static async UniTask CreateRoomAsync( string roomName, RoomOptions roomOptions = null, TypedLobby typedLobby = null, string[] expectedUsers = null, CancellationToken token = default) { var task = UniTask.WhenAny( Pun2TaskCallback.OnJoinedRoomAsync().AsAsyncUnitUniTask(), Pun2TaskCallback.OnCreateRoomFailedAsync()); var valid = PhotonNetwork.CreateRoom(roomName, roomOptions, typedLobby, expectedUsers); if (!valid) { throw new InvalidRoomOperationException("It is not ready to create a room."); } var(winIndex, _, (returnCode, message)) = await task.WithCancellation(token); if (winIndex == 0) { return; } throw new FailedToCreateRoomException(returnCode, message); }
/// <summary> /// Joins a specific room by name and creates it on demand. Will callback: OnJoinedRoom or OnJoinRoomFailed. /// </summary> /// <remarks> /// Useful when players make up a room name to meet in: /// All involved clients call the same method and whoever is first, also creates the room. /// /// When successful, the client will enter the specified room. /// The client which creates the room, will callback both OnCreatedRoom and OnJoinedRoom. /// Clients that join an existing room will only callback OnJoinedRoom. /// In all error cases, OnJoinRoomFailed gets called. /// /// Joining a room will fail, if the room is full, closed or when the user /// already is present in the room (checked by userId). /// /// To return to a room, use OpRejoinRoom. /// /// This method can only be called while the client is connected to a Master Server so you should /// implement the callback OnConnectedToMaster. /// Check the return value to make sure the operation will be called on the server. /// Note: There will be no callbacks if this method returned false. /// /// /// If you set room properties in roomOptions, they get ignored when the room is existing already. /// This avoids changing the room properties by late joining players. /// /// You can define an array of expectedUsers, to block player slots in the room for these users. /// The corresponding feature in Photon is called "Slot Reservation" and can be found in the doc pages. /// /// /// More about PUN matchmaking: /// https://doc.photonengine.com/en-us/pun/v2/lobby-and-matchmaking/matchmaking-and-lobby /// </remarks> /// <param name="roomName">Name of the room to join. Must be non null.</param> /// <param name="roomOptions">Options for the room, in case it does not exist yet. Else these values are ignored.</param> /// <param name="typedLobby">Lobby you want a new room to be listed in. Ignored if the room was existing and got joined.</param> /// <param name="expectedUsers">Optional list of users (by UserId) who are expected to join this game and who you want to block a slot for.</param> /// <returns>True will be returned when you are the first user.</returns>> public static async UniTask <bool> JoinOrCreateRoomAsync( string roomName, RoomOptions roomOptions, TypedLobby typedLobby, string[] expectedUsers = null, CancellationToken token = default) { var createdRoomTask = Pun2TaskCallback.OnCreatedRoomAsync().GetAwaiter(); var task = UniTask.WhenAny( Pun2TaskCallback.OnJoinedRoomAsync().AsAsyncUnitUniTask(), Pun2TaskCallback.OnCreateRoomFailedAsync(), Pun2TaskCallback.OnJoinRoomFailedAsync()); var valid = PhotonNetwork.JoinOrCreateRoom(roomName, roomOptions, typedLobby, expectedUsers); if (!valid) { throw new InvalidRoomOperationException("It is not ready to join a room."); } var(winIndex, _, (createFailedCode, createFailedMessage), (joinFailedCode, joinFailedMessage)) = await task.WithCancellation(token); if (winIndex == 0) { return(createdRoomTask.IsCompleted); } if (winIndex == 1) { throw new FailedToCreateRoomException(createFailedCode, createFailedMessage); } else { throw new FailedToJoinRoomException(createFailedCode, createFailedMessage); } }