How to Connect Steamworks to Unity for Multiplayer Games Video Tutorial by Caleb For The CGDC-CG
Download Links (for what’s needed for this tutorial):
https://assetstore.unity.com/packages/tools/network/mirror-129321
https://github.com/Chykary/FizzySteamworks
C# Script Needed for the Video Tutorial:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Steamworks;
using Mirror;
public class SteamLobby : MonoBehaviour
{
protected Callback<LobbyCreated_t> lobbyCreated;
protected Callback<GameLobbyJoinRequested_t> gameLobbyJoinRequested;
protected Callback<LobbyEnter_t> lobbyEntered;
private NetworkManager networkManager;
private const string HostAddressKey = “HostAddress”;
public void HostLobby()
{
buttons.SetActive(false);
SteamMatchmaking.CreateLobby(ELobbyType.k_ELobbyTypeFriendsOnly,networkManager.maxConnections);
}
// Start is called before the first frame update
void Start()
{
networkManager = GetComponent<NetworkManager>();
if (!SteamManager.Initialized) { Debug.Log(“Not Initialized!! Returning”); return; }
lobbyCreated = Callback<LobbyCreated_t>.Create(OnLobbyCreated);
gameLobbyJoinRequested = Callback<GameLobbyJoinRequested_t>.Create(OnGameLobbyJoinRequested);
lobbyEntered = Callback<LobbyEnter_t>.Create(OnLobbyEntered);
}
private void OnLobbyCreated(LobbyCreated_t callback)
{
if(callback.m_eResult != EResult.k_EResultOK)
{
buttons.SetActive(true);
Debug.Log(“Not created!”);
return;
}
networkManager.StartHost();
SteamMatchmaking.SetLobbyData(new CSteamID(callback.m_ulSteamIDLobby), HostAddressKey, SteamUser.GetSteamID().ToString());
}
private void OnGameLobbyJoinRequested(GameLobbyJoinRequested_t callback)
{
SteamMatchmaking.JoinLobby(callback.m_steamIDLobby);
}
private void OnLobbyEntered(LobbyEnter_t callback)
{
if (NetworkServer.active) { return; }
string hostAddress = SteamMatchmaking.GetLobbyData(new CSteamID(callback.m_ulSteamIDLobby), HostAddressKey);
networkManager.networkAddress = hostAddress;
networkManager.StartClient();
}
}