Search Unity

Question Need Help using Ui with facepunch + unity netcode for games

Discussion in 'Multiplayer' started by Vitor_barbosa, Mar 8, 2022.

  1. Vitor_barbosa

    Vitor_barbosa

    Joined:
    Oct 10, 2020
    Posts:
    5
    Ok, so what I'm trying to do is be able to use these two panels I've created to create and join a lobby using steam services. I don't have any experience with using multiplayer so i just followed a tutorial and this is the code i have so far but he did not cover implementing it into a UI and have not been able to find a video or article/documentation on this subject. So i thought it would be worth an ask.
    upload_2022-3-8_17-27-30.png
    upload_2022-3-8_17-27-53.png

    Code (CSharp):
    1. using Netcode.Transports.Facepunch;
    2. using UnityEngine;
    3. using Steamworks.Data;
    4. using Steamworks;
    5. using Unity.Netcode;
    6.  
    7. public class GameNetworkManager : MonoBehaviour
    8. {
    9.     public GameNetworkManager Instance { get; private set; } = null;
    10.     public Lobby? currentLobby { get; private set; } = null ;
    11.  
    12.     private FacepunchTransport transport = null;
    13.  
    14.     //bool InviteFriend(SteamId friendId)
    15.  
    16.     private void Awake()
    17.     {
    18.         if(Instance == null)
    19.             Instance = this;
    20.         else
    21.         {
    22.             Destroy(gameObject);
    23.             return;
    24.         }
    25.     }
    26.     private void Start()
    27.     {
    28.         transport = GetComponent<FacepunchTransport>();
    29.         SteamMatchmaking.OnLobbyCreated += OnlobbyCreated;
    30.         SteamMatchmaking.OnLobbyEntered += OnLobbyEntered;
    31.         SteamMatchmaking.OnLobbyMemberJoined += OnLobbyMemberJoined;
    32.         SteamMatchmaking.OnLobbyMemberLeave += OnLobbyMemberLeave;
    33.         SteamMatchmaking.OnLobbyInvite += OnLobbyInvite;
    34.         SteamMatchmaking.OnLobbyGameCreated += OnLobbyGameCreated;
    35.         SteamFriends.OnGameLobbyJoinRequested += OnGameLobbyJoinRequested;
    36.     }
    37.  
    38.     public void StartClient(SteamId id)
    39.     {
    40.         NetworkManager.Singleton.OnClientConnectedCallback += OnClientConnectedCallback;
    41.         NetworkManager.Singleton.OnClientDisconnectCallback += OnClientDisconnectCallback;
    42.  
    43.         transport.targetSteamId = id;
    44.  
    45.         if (NetworkManager.Singleton.StartClient())
    46.             Debug.Log("Client has joined", this);
    47.     }
    48.  
    49.     public async void startHost(int maxMembers = 100)
    50.     {
    51.    
    52.         NetworkManager.Singleton.OnServerStarted += OnServerStarted;
    53.         NetworkManager.Singleton.StartHost();
    54.  
    55.        currentLobby =  await SteamMatchmaking.CreateLobbyAsync(maxMembers);
    56.  
    57.     }
    58.     public void OnApplicationQuit() => Disconnect();
    59.     public void Disconnect( )
    60.     {
    61.  
    62.         currentLobby?.Leave();
    63.         if (NetworkManager.Singleton == null)
    64.             return;
    65.  
    66.         NetworkManager.Singleton.Shutdown();
    67.     }
    68.  
    69.  
    70.     #region Network Callbacks
    71.     private void OnServerStarted() => Debug.Log("Server has started",this);
    72.     private void OnClientConnectedCallback(ulong clientId)
    73.     {
    74.         Debug.Log($"Client Connected, clientId={clientId}");
    75.     }
    76.     private void OnClientDisconnectCallback(ulong clientId)
    77.     {
    78.         Debug.Log($"Client Disconnected, clientId={clientId}");
    79.         NetworkManager.Singleton.OnClientConnectedCallback -= OnClientConnectedCallback;
    80.         NetworkManager.Singleton.OnClientDisconnectCallback -= OnClientDisconnectCallback;
    81.     }
    82.  
    83.     #endregion
    84.  
    85.  
    86.  
    87.  
    88.     #region Steam Callbacks
    89.     private void OnGameLobbyJoinRequested(Lobby lobby, SteamId id) => StartClient(id);
    90.     private void OnLobbyGameCreated(Lobby lobby, uint ip, ushort port, SteamId id)
    91.     {
    92.    
    93.     }
    94.  
    95.     private void OnLobbyInvite(Friend friend, Lobby lobby) => Debug.Log($"You got an invite from {friend.Name}", this);
    96.  
    97.     private void OnLobbyMemberLeave(Lobby lobby, Friend friend)
    98.     {
    99.    
    100.     }
    101.  
    102.     private void OnLobbyMemberJoined(Lobby lobby, Friend friend)
    103.     {
    104.    
    105.     }
    106.  
    107.     private void OnLobbyEntered(Lobby lobby)
    108.     {
    109.         if (NetworkManager.Singleton.IsHost)
    110.             return;
    111.  
    112.         StartClient(lobby.Id);
    113.     }
    114.  
    115.     private void OnlobbyCreated(Result result,Lobby lobby)
    116.     {
    117.        if(result != Result.OK)
    118.         {
    119.             Debug.LogError($"Lobby couldn't be created!,{result}",this);
    120.             return;
    121.         }
    122.  
    123.         lobby.SetFriendsOnly();
    124.         lobby.SetData("name", "vitors test lobby");
    125.         lobby.SetJoinable(true);
    126.         Debug.Log("Lobby Created");
    127.  
    128.     }
    129.     #endregion
    130.  
    131.     private void OnDestroy()
    132.     {
    133.  
    134.         SteamMatchmaking.OnLobbyCreated -= OnlobbyCreated;
    135.         SteamMatchmaking.OnLobbyEntered -= OnLobbyEntered;
    136.         SteamMatchmaking.OnLobbyMemberJoined -= OnLobbyMemberJoined;
    137.         SteamMatchmaking.OnLobbyMemberLeave -= OnLobbyMemberLeave;
    138.         SteamMatchmaking.OnLobbyInvite -= OnLobbyInvite;
    139.         SteamMatchmaking.OnLobbyGameCreated -= OnLobbyGameCreated;
    140.         SteamFriends.OnGameLobbyJoinRequested -= OnGameLobbyJoinRequested;
    141.         if (NetworkManager.Singleton == null)
    142.             return;
    143.         NetworkManager.Singleton.OnClientConnectedCallback -= OnClientConnectedCallback;
    144.         NetworkManager.Singleton.OnClientDisconnectCallback -= OnClientDisconnectCallback;
    145.         NetworkManager.Singleton.OnServerStarted -= OnServerStarted;
    146.     }
    147. }


    So I'm just wondering what sort of classes or what would i have to add to this in order to get the buttons to work like used Unity Ui and even have done the lobby screen for multiplayer with different solutions but with implementing steam and facepunch I'm having a bit of problem, especially with the limited amount of documentation on the facepunch website on how to do this. So yeah I'm just wondering if there is a hero out there to save this damsel in distress
     
    Last edited: Mar 14, 2022