SearchAndCreateLobby
Namespace: SynicSugar.MatchMake
Class: MatchMakeManager
Auto matchmaking
public async UniTask<bool> SearchAndCreateLobby(Lobby lobbyCondition, CancellationTokenSource token = default(CancellationTokenSource))
Manual matchmaking
public async UniTask<bool> SearchAndCreateLobby(Lobby lobbyCondition, uint minLobbyMember, List<AttributeData> userAttributes = null, CancellationTokenSource token = default(CancellationTokenSource))
Description
Start MatchMake with args condition and get the data for p2p connect.
At first, search and try to join. If can't, the user create lobby as host.
If success and finish preparation p2p connect, return true. If not (by timeout or anything problem), return false.
This CancellationTokenSource is used only to cancel matchmaking.
Usually we don't need pass token source. If not pass, when we call CancelMatchMaking(), we get just bool result from this method. If pass source, we need TryCatch for CancelMatching.
When matchmaking fails, this always returns false, not an exception. To get result code, use LastResultCode.
For Host to conclude matchmaking, pass minLobbyMember. This allows Host to kick other member, and end matchmaking after meeting members condition. In the case, the matchmaking won't have ended until Host conclude it in manual.
The value is 0, less than LobbyMaxMembers or null, the matchmaking becomes Auto(Random) matchmaking.
Auto does not allow Host to kick Guests, and anyone who meets the lobbyCondition can join the lobby. When the lobby is full, closes the lobby not to join and start to prepare p2p automatically.
userAttributes is for matchmaking. The user attributes of names, job and so on that is needed before P2P.
These should be used just for matchmaking and the kick, the data for actual game should be exchanged via p2p for security and server bandwidth.
using UnityEngine;
// using System.Threading;
using Cysharp.Threading.Tasks;
using SynicSugar.MatchMake;
public class MatchMake : MonoBehaviour {
Lobby condition; //Create a Lobby as a condition before matchmake.
async UniTask StartMatching(){
Result result = await MatchMakeManager.Instance.SearchAndCreateLobby(condition);
// //try catch
// Result result;
// try{
// CancellationTokenSource cts = new CancellationTokenSource();
// //Get Success or Failuer
// result = await MatchMakeManager.Instance.SearchAndCreateLobby(condition, cts);
// }catch(OperationCanceledException){
// //Cancel matchmaking
// result = Result.Canceled;
// }
if(result != Result.Success){
//Failuer
return;
}
//Success
}
}