Hello!
I am creating a lobby for my first multiplayer game. However I can’t figure out that how I can filter the lobbies based on the player’s preference. Here is a code snippet of my lobby script:
using System;
using System.Collections;
using System.Collections.Generic;
using Unity.Netcode;
using Unity.Services.Authentication;
using Unity.Services.Core;
using Unity.Services.Lobbies;
using Unity.Services.Lobbies.Models;
using UnityEngine;
public class Lobby : MonoBehaviour {
public enum Language
{
English,
Chinese,
Hindi,
Spanish,
French,
German,
Portuguese,
Russian,
Japanese,
Korean
}
public async void CreateLobby(string lobbyName,bool isPrivate, Language language) {
CreateLobbyOptions options = new CreateLobbyOptions {
IsPrivate = isPrivate,
Data = new Dictionary<string, DataObject> {
{ KEY_START_GAME, new DataObject(DataObject.VisibilityOptions.Member,"0") },
{ LANGUAGE, new DataObject(DataObject.VisibilityOptions.Public, language.ToString()) }
}
};
Lobby lobby = await LobbyService.Instance.CreateLobbyAsync(lobbyName, maxPlayers, options);
joinedLobby = lobby;
OnJoinedLobby?.Invoke(this, new LobbyEventArgs { lobby = lobby });
}
public async void RefreshLobbyList() {
try {
} catch (LobbyServiceException e) {
Debug.Log(e);
}
}
This is a code snippet of script which allows the player to create a lobby:
using System.Collections;
using System.Collections.Generic;
using TMPro;
using UnityEngine;
using UnityEngine.UI;
public class CreateLobby : MonoBehaviour {
public static LobbyCreateUI Instance { get; private set; }
[SerializeField] private Button languageButton;
[SerializeField] private TMPro.TextMeshProUGUI languageDisplayText;
private LobbyManager.Language language;
private void Awake() {
Instance = this;
languageButton.onClick.AddListener(() => {
switch (language)
{
default:
case LobbyManager.Language.English:
language = LobbyManager.Language.Chinese;
break;
case LobbyManager.Language.Chinese:
language = LobbyManager.Language.Hindi;
break;
case LobbyManager.Language.Hindi:
language = LobbyManager.Language.Spanish;
break;
case LobbyManager.Language.Spanish:
language = LobbyManager.Language.French;
break;
case LobbyManager.Language.French:
language = LobbyManager.Language.German;
break;
case LobbyManager.Language.German:
language = LobbyManager.Language.Portuguese;
break;
case LobbyManager.Language.Portuguese:
language = LobbyManager.Language.Russian;
break;
case LobbyManager.Language.Russian:
language = LobbyManager.Language.Japanese;
break;
case LobbyManager.Language.Japanese:
language = LobbyManager.Language.Korean;
break;
case LobbyManager.Language.Korean:
language = LobbyManager.Language.English;
break;
}
UpdateText();
});
Hide();
}
}
And this is the script which lists all the available lobbies in the lobby window so that a player can join it.:
using System;
using System.Collections;
using System.Collections.Generic;
using Unity.Services.Lobbies.Models;
using UnityEngine;
using UnityEngine.UI;
public class ListAvailableLobbies: MonoBehaviour {
public static LobbyListUI Instance { get; private set; }
[SerializeField] private Transform lobbySingleTemplate;
[SerializeField] private Transform container;
[SerializeField] private Button refreshButton;
[SerializeField] private Button createLobbyButton;
[SerializeField] private Button languageButton;
[SerializeField] private TMPro.TextMeshProUGUI languageDisplayText;
private void Awake() {
Instance = this;
lobbySingleTemplate.gameObject.SetActive(false);
refreshButton.onClick.AddListener(RefreshButtonClick);
createLobbyButton.onClick.AddListener(CreateLobbyButtonClick);
}
private void Start() {
LobbyManager.Instance.OnLobbyListChanged += LobbyManager_OnLobbyListChanged;
LobbyManager.Instance.OnJoinedLobby += LobbyManager_OnJoinedLobby;
LobbyManager.Instance.OnLeftLobby += LobbyManager_OnLeftLobby;
LobbyManager.Instance.OnKickedFromLobby += LobbyManager_OnKickedFromLobby;
}
private void LobbyManager_OnKickedFromLobby(object sender, LobbyManager.LobbyEventArgs e) {
Show();
}
private void LobbyManager_OnLeftLobby(object sender, EventArgs e) {
Show();
}
private void LobbyManager_OnJoinedLobby(object sender, LobbyManager.LobbyEventArgs e) {
Hide();
}
private void LobbyManager_OnLobbyListChanged(object sender, LobbyManager.OnLobbyListChangedEventArgs e) {
UpdateLobbyList(e.lobbyList);
}
private void UpdateLobbyList(List<Lobby> lobbyList) {
foreach (Transform child in container) {
if (child == lobbySingleTemplate) continue;
Destroy(child.gameObject);
}
foreach (Lobby lobby in lobbyList) {
Transform lobbySingleTransform = Instantiate(lobbySingleTemplate, container);
lobbySingleTransform.gameObject.SetActive(true);
LobbyListSingleUI lobbyListSingleUI = lobbySingleTransform.GetComponent<LobbyListSingleUI>();
lobbyListSingleUI.UpdateLobby(lobby);
}
}
private void RefreshButtonClick() {
LobbyManager.Instance.RefreshLobbyList();
}
private void CreateLobbyButtonClick() {
LobbyCreateUI.Instance.Show();
}
private void Hide() {
gameObject.SetActive(false);
}
private void Show() {
gameObject.SetActive(true);
}
}
I just want the player to be able to filter the lobby by pressing the language button. Please also explain how the player can cycle through all the language options in the enum by pressing this button.
Your response will really matter a lot.
Thank You!