Lobby interaction handler
unknown
csharp
a year ago
4.3 kB
4
Indexable
using System.Collections.Generic; using UnityEngine; using Unity.Services.Lobbies; using Unity.Services.Lobbies.Models; public class LobbyCreateOrJoinHandler : MonoBehaviour { [SerializeField] SceneTransitionManager _sceneTransitionManager; string _roomCode; string _lobbyID; /*public async void CreateLobby(string lobbyName) { try { int maxPlayers = 2; string roomCode = RoomCodeGenerator.GenerateRoomCode(5); var options = new CreateLobbyOptions { IsPrivate = false, Data = new Dictionary<string, DataObject> { {"roomCode", new DataObject(DataObject.VisibilityOptions.Public, roomCode)} }, }; Lobby lobby = await Lobbies.Instance.CreateLobbyAsync(lobbyName, maxPlayers, options); Debug.Log($"Lobby created, ID: {lobby.Id}"); Debug.Log($"Room code: {roomCode}"); _roomCode = roomCode; //_sceneTransitionManager.LoadSceneTransition(_sceneTransitionManager.Wipe, "LobbyRoom"); } catch (LobbyServiceException e) { Debug.LogError(e); } }*/ public async void CreateLobby(string lobbyName) { try { int maxPlayers = 2; string roomCode = RoomCodeGenerator.GenerateRoomCode(5); CreateLobbyOptions options = new(); options.Data = new Dictionary<string, DataObject>{{"roomCode", new DataObject(DataObject.VisibilityOptions.Public, roomCode)}}; Lobby lobby = await Lobbies.Instance.CreateLobbyAsync(lobbyName, maxPlayers, options); Debug.Log($"Lobby created, ID: {lobby.Id}"); Debug.Log($"Room code: {roomCode}"); _roomCode = roomCode; _lobbyID = lobby.Id; //_sceneTransitionManager.LoadSceneTransition(_sceneTransitionManager.Wipe, "LobbyRoom"); } catch (LobbyServiceException e) { Debug.LogError(e); } } public async void JoinLobby(string roomCode) { try { QueryLobbiesOptions options = new QueryLobbiesOptions { Filters = new List<QueryFilter> { new QueryFilter( field: QueryFilter.FieldOptions.S1, op: QueryFilter.OpOptions.EQ, value: roomCode) } }; QueryResponse queryResponse = await Lobbies.Instance.QueryLobbiesAsync(options); if (queryResponse.Results.Count > 0) { Debug.Log("Found a lobby"); Lobby foundLobby = queryResponse.Results[0]; if (foundLobby.AvailableSlots > 0) { Lobby lobby = await Lobbies.Instance.JoinLobbyByCodeAsync(foundLobby.Id); Debug.Log($"Joined lobby {lobby.Id}"); _sceneTransitionManager.LoadSceneTransition(_sceneTransitionManager.Wipe, "LobbyRoom"); } else { Debug.Log("Lobby is full."); } } else { Debug.Log($"Lobby code: | {roomCode} | was not found."); } } catch (LobbyServiceException e) { Debug.LogError(e); } } public async void QuickJoin() { Debug.Log($"QuickPlay..."); try { QuickJoinLobbyOptions options = new(); Lobby lobby = await Lobbies.Instance.QuickJoinLobbyAsync(options); Debug.Log($"Joined lobby: {lobby.Id}"); Debug.Log($"Lobby players: {lobby.Players.Count}"); } catch (LobbyServiceException e) { Debug.Log("Couldn't find a lobby"); Debug.LogWarning(e); } } public string GetRoomCode() { return _roomCode; } public string GetLobbyID() { return _lobbyID; } }
Editor is loading...
Leave a Comment