Steam lobby implemented

This commit is contained in:
Madhav Kapa
2023-06-01 08:03:48 -07:00
parent c7647c8097
commit 49efee80e2
95 changed files with 10258 additions and 6167 deletions

View File

@ -0,0 +1,30 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Steamworks;
public class FriendObject : MonoBehaviour
{
public SteamId steamid;
public async void Invite()
{
if (SteamLobbyManager.UserInLobby)
{
SteamLobbyManager.CurrentLobby.InviteFriend(steamid);
Debug.Log("Invited " + steamid);
}
else
{
bool result = await SteamLobbyManager.CreateLobby();
if (result)
{
SteamLobbyManager.CurrentLobby.InviteFriend(steamid);
Debug.Log("Invited " + steamid + " Created a new lobby");
}
}
}
}

View File

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 57db693cbe9a46345add801ae22e1e2a
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -0,0 +1,74 @@
using Steamworks;
using UnityEngine;
using UnityEngine.UI;
public class SteamFriendsManager : MonoBehaviour
{
public RawImage playerprofile;
public Text playername;
public Transform friendsContent;
public GameObject friendObj;
async void Start()
{
if (!SteamClient.IsValid) return;
playername.text = SteamClient.Name;
InitFriendsAsync();
var img = await SteamFriends.GetLargeAvatarAsync(SteamClient.SteamId);
playerprofile.texture = GetTextureFromImage(img.Value);
}
public static Texture2D GetTextureFromImage(Steamworks.Data.Image image)
{
Texture2D texture = new Texture2D((int)image.Width, (int)image.Height);
for (int x = 0; x < image.Width; x++)
{
for (int y = 0; y < image.Height; y++)
{
var p = image.GetPixel(x, y);
texture.SetPixel(x, (int)image.Height - y, new Color(p.r / 255.0f, p.g / 255.0f, p.b / 255.0f, p.a / 255.0f));
}
}
texture.Apply();
return texture;
}
public void InitFriendsAsync()
{
foreach (var friend in SteamFriends.GetFriends())
{
GameObject f = Instantiate(friendObj, friendsContent);
f.GetComponentInChildren<Text>().text = friend.Name;
f.GetComponent<FriendObject>().steamid = friend.Id;
AssingFriendImage(f, friend.Id);
}
}
public async void AssingFriendImage(GameObject f, SteamId id)
{
var img = await SteamFriends.GetLargeAvatarAsync(id);
f.GetComponentInChildren<RawImage>().texture = GetTextureFromImage(img.Value);
}
public static async System.Threading.Tasks.Task<Texture2D> GetTextureFromSteamIdAsync(SteamId id)
{
var img = await SteamFriends.GetLargeAvatarAsync(SteamClient.SteamId);
Steamworks.Data.Image image = img.Value;
Texture2D texture = new Texture2D((int)image.Width, (int)image.Height);
for (int x = 0; x < image.Width; x++)
{
for (int y = 0; y < image.Height; y++)
{
var p = image.GetPixel(x, y);
texture.SetPixel(x, (int)image.Height - y, new Color(p.r / 255.0f, p.g / 255.0f, p.b / 255.0f, p.a / 255.0f));
}
}
texture.Apply();
return texture;
}
}

View File

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 3e1287d29b0590b4086746e0ea190f4e
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -0,0 +1,176 @@
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<SteamId, GameObject> 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>().text = friend.Name;
obj.GetComponentInChildren<RawImage>().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>().text = SteamClient.Name;
obj.GetComponentInChildren<RawImage>().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>().text = friend.Name;
obj2.GetComponentInChildren<RawImage>().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<bool> 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
{
}
}
}

View File

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: d4b922de688dc97428e1fcaec4244b9e
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -0,0 +1,39 @@
using System;
using Steamworks;
using UnityEngine;
using UnityEngine.Events;
public class SteamManager : MonoBehaviour
{
public uint appId;
public UnityEvent onSteamFailed;
private void Awake()
{
DontDestroyOnLoad(this);
try
{
SteamClient.Init(appId);
Debug.Log("Steam is up and running!");
}
catch (Exception e)
{
Debug.Log(e.Message);
onSteamFailed.Invoke();
}
}
private void OnApplicationQuit()
{
try
{
Debug.Log("Steam is shutting down!");
SteamClient.Shutdown();
}
catch
{
Debug.Log("Steam is not running!");
}
}
}

View File

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: c790abd20c22fc348ac5c95c419527bb
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant: