Search Unity

Question Recommendation for Networking Library

Discussion in 'Multiplayer' started by saifshk17, Mar 11, 2022.

  1. saifshk17

    saifshk17

    Joined:
    Dec 4, 2016
    Posts:
    488
    Hi,

    I have an application that gets installed in 2 devices. The application contains 3 scenes + few buttons in both scenes. I want a multiplayer connection (LAN) with them such that if user 1 clicks on a button, the button should also be triggered on user 2's device and vice versa. I know that such behavior can be obtained through RPCs. I am not sure which library to use.

    So far I have tried Mirror & Unity's MLAPI, both require player prefabs to be spawned and also does additive scene loading. I do not want this. Earlier I used to use Forge Networking and it was quite simple to send across RPC calls but then it is now deprecated/no update for latest Unity.

    Such behavior is obtained through Photon PUN but that requires internet, so is there any such library that allows me to get this behavior in a LAN (no internet) ?
     
  2. ep1s0de

    ep1s0de

    Joined:
    Dec 24, 2015
    Posts:
    168
  3. luke-unity

    luke-unity

    Joined:
    Sep 30, 2020
    Posts:
    306
    You can disable the spawning of player prefabs and the entire scene management in the NetworkManager of MLAPI. They are optional features.
     
  4. saifshk17

    saifshk17

    Joined:
    Dec 4, 2016
    Posts:
    488
  5. saifshk17

    saifshk17

    Joined:
    Dec 4, 2016
    Posts:
    488
    How do I do this? I do not see any option. Also if I change the scene, the network breaks.
     
  6. cerestorm

    cerestorm

    Joined:
    Apr 16, 2020
    Posts:
    666
    There's some info here on controlling player spawning https://forum.unity.com/threads/spawn-player-prefab-on-server-only.1231716/

    To disable scene management untick Enable Scene Management on the NetworkManager in the editor. With it disabled clients can switch scenes independently of the server, but you'll need to take responsibility for the spawning and despawning of network objects for each client scene.
     
  7. saifshk17

    saifshk17

    Joined:
    Dec 4, 2016
    Posts:
    488
    To understand you correctly, the client and host from the starting scene can still be active when switching scene?
     
  8. cerestorm

    cerestorm

    Joined:
    Apr 16, 2020
    Posts:
    666
    The client/host players? For the client the server would have to despawn it when the client notifies the server it's making a scene switch. Once the scene is loaded the client can then notify the server to spawn it again. To get around this I would imagine you could use DontDestroyOnLoad().

    I'm not sure how having a host would work with scene management disabled, at least the way I use it allows separation between client and server and a host would conflict with that.
     
  9. saifshk17

    saifshk17

    Joined:
    Dec 4, 2016
    Posts:
    488
    I am able to send RPC calls within the same scene. But when I load a different scene and use to pass RPC calls, it does not work anymore. I even spawn the network object in the server part of the new scene. On the client side, the network object is already spawned.

    I tried disabling "Enable Scene Management" in Network Manager but that just disables all network objects. What am I doing wrong? Is there a simple video explanation of passing RPC calls in a new scene?

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using Unity.Netcode;
    4. using UnityEngine;
    5. using UnityEngine.UI;
    6. using UnityEngine.SceneManagement;
    7.  
    8. public class RPCCube : NetworkBehaviour {
    9.     public GameObject cube;
    10.  
    11.     void Update () {
    12.         if (Input.GetKeyDown (KeyCode.P)) {
    13.             SceneClientRpc (Time.frameCount, "hello, world"); // Server -> Client
    14.         }
    15.  
    16.         if (Input.GetKeyDown (KeyCode.R)) {
    17.             DeactiveClientRpc (Time.frameCount, "hello, world"); // Server -> Client
    18.         }
    19.  
    20.     }
    21.  
    22.     void OnGUI () {
    23.         GUILayout.BeginArea (new Rect (10, 10, 300, 300));
    24.         if (!NetworkManager.Singleton.IsClient && !NetworkManager.Singleton.IsServer) {
    25.             StartButtons ();
    26.         } else {
    27.             StatusLabels ();
    28.         }
    29.  
    30.         GUILayout.EndArea ();
    31.     }
    32.  
    33.     static void StartButtons () {
    34.         if (GUILayout.Button ("Host")) NetworkManager.Singleton.StartHost ();
    35.         if (GUILayout.Button ("Client")) NetworkManager.Singleton.StartClient ();
    36.         if (GUILayout.Button ("Server")) NetworkManager.Singleton.StartServer ();
    37.     }
    38.  
    39.     static void StatusLabels () {
    40.         var mode = NetworkManager.Singleton.IsHost ?
    41.             "Host" : NetworkManager.Singleton.IsServer ? "Server" : "Client";
    42.  
    43.         GUILayout.Label ("Transport: " +
    44.             NetworkManager.Singleton.NetworkConfig.NetworkTransport.GetType ().Name);
    45.         GUILayout.Label ("Mode: " + mode);
    46.     }
    47.    
    48.     [ClientRpc]
    49.     void SceneClientRpc (int somenumber, string sometext) {
    50.         SceneManager.LoadScene("Load");
    51.     }
    52.  
    53.      [ClientRpc]
    54.     void DeactiveClientRpc (int somenumber, string sometext) {
    55.         cube.SetActive (false);
    56.     }
    57.  
    58. }
     
  10. cerestorm

    cerestorm

    Joined:
    Apr 16, 2020
    Posts:
    666
    It's hard to say what's going on, but check the NetworkObjectId of the object making the RPC call to make sure they match on client and server.

    I wouldn't turn off scene management as there's a lot you have to handle that you can take for granted when it's enabled. If you must have it disabled get things working step by step.

    For RPC examples take a look at some of the Netcode video's on Youtube.
     
  11. Punfish

    Punfish

    Joined:
    Dec 7, 2014
    Posts:
    401
    You should check out Fish-Networking. It does all of that and doesn't require a player prefab. It's developed by a career game designer. Also has lan discovery and offline mode that let's your game run multiplayer code but offline.

    Links to everything here including asset store page
    https://fish-networking.gitbook.io/docs/
     
    saifshk17 likes this.
  12. saifshk17

    saifshk17

    Joined:
    Dec 4, 2016
    Posts:
    488
    The best so far out there!