using System; using System.Collections.Generic; using System.Threading.Tasks; using Steamworks; using Steamworks.Data; using UnityEngine; using UnityEngine.Events; using UnityEngine.UI; public class SteamLobbyManager : MonoBehaviour { public static Lobby CurrentLobby; public static bool UserInLobby; public UnityEvent onLobbyCreated; public UnityEvent onLobbyJoined; public UnityEvent onLobbyLeave; public GameObject inLobbyFriend; public Transform content; public Dictionary inLobby = new(); private void Start() { DontDestroyOnLoad(this); SteamMatchmaking.OnLobbyCreated += OnLobbyCreatedCallBack; SteamMatchmaking.OnLobbyEntered += OnLobbyEntered; SteamMatchmaking.OnLobbyMemberJoined += OnLobbyMemberJoined; SteamMatchmaking.OnChatMessage += OnChatMessage; SteamMatchmaking.OnLobbyMemberDisconnected += OnLobbyMemberDisconnected; SteamMatchmaking.OnLobbyMemberLeave += OnLobbyMemberDisconnected; SteamMatchmaking.OnLobbyGameCreated += OnLobbyGameCreated; SteamFriends.OnGameLobbyJoinRequested += OnGameLobbyJoinRequest; SteamMatchmaking.OnLobbyInvite += OnLobbyInvite; } private void OnLobbyInvite(Friend friend, Lobby lobby) { Debug.Log($"{friend.Name} invited you to his lobby."); } private void OnLobbyGameCreated(Lobby lobby, uint ip, ushort port, SteamId id) { } private async void OnLobbyMemberJoined(Lobby lobby, Friend friend) { Debug.Log($"{friend.Name} joined the lobby"); var obj = Instantiate(inLobbyFriend, content); obj.GetComponentInChildren().text = friend.Name; obj.GetComponentInChildren().texture = await SteamFriendsManager.GetTextureFromSteamIdAsync(friend.Id); inLobby.Add(friend.Id, obj); } private void OnLobbyMemberDisconnected(Lobby lobby, Friend friend) { Debug.Log($"{friend.Name} left the lobby"); Debug.Log($"New lobby owner is {CurrentLobby.Owner}"); if (inLobby.ContainsKey(friend.Id)) { Destroy(inLobby[friend.Id]); inLobby.Remove(friend.Id); } } private void OnChatMessage(Lobby lobby, Friend friend, string message) { Debug.Log($"incoming chat message from {friend.Name} : {message}"); } private async void OnGameLobbyJoinRequest(Lobby joinedLobby, SteamId id) { var joinedLobbySuccess = await joinedLobby.Join(); if (joinedLobbySuccess != RoomEnter.Success) Debug.Log("failed to join lobby : " + joinedLobbySuccess); else CurrentLobby = joinedLobby; } private void OnLobbyCreatedCallBack(Result result, Lobby lobby) { if (result != Result.OK) { Debug.Log("lobby creation result not ok : " + result); } else { onLobbyCreated.Invoke(); Debug.Log("lobby creation result ok"); } } private async void OnLobbyEntered(Lobby lobby) { Debug.Log("Client joined the lobby"); UserInLobby = true; foreach (var user in inLobby.Values) Destroy(user); inLobby.Clear(); var obj = Instantiate(inLobbyFriend, content); obj.GetComponentInChildren().text = SteamClient.Name; obj.GetComponentInChildren().texture = await SteamFriendsManager.GetTextureFromSteamIdAsync(SteamClient.SteamId); inLobby.Add(SteamClient.SteamId, obj); foreach (var friend in CurrentLobby.Members) if (friend.Id != SteamClient.SteamId) { var obj2 = Instantiate(inLobbyFriend, content); obj2.GetComponentInChildren().text = friend.Name; obj2.GetComponentInChildren().texture = await SteamFriendsManager.GetTextureFromSteamIdAsync(friend.Id); inLobby.Add(friend.Id, obj2); } onLobbyJoined.Invoke(); } public async void CreateLobbyAsync() { var result = await CreateLobby(); if (!result) { //Invoke a error message. } } public static async Task CreateLobby() { try { var createLobbyOutput = await SteamMatchmaking.CreateLobbyAsync(); if (!createLobbyOutput.HasValue) { Debug.Log("Lobby created but not correctly instantiated."); return false; } CurrentLobby = createLobbyOutput.Value; CurrentLobby.SetPublic(); //currentLobby.SetPrivate(); CurrentLobby.SetJoinable(true); return true; } catch (Exception exception) { Debug.Log("Failed to create multiplayer lobby : " + exception); return false; } } public void LeaveLobby() { try { UserInLobby = false; CurrentLobby.Leave(); onLobbyLeave.Invoke(); foreach (var user in inLobby.Values) Destroy(user); inLobby.Clear(); } catch { } } private void Update() { if(Input.GetKeyDown(KeyCode.Return)) { print("OWNER?:"+CurrentLobby.Owner); } } }