UNET - Local Discovery and Lobby

Hey Guys,

I want to develop a local (lan) multiplayer game for mobile (android).
Therefore I want to use Local Discovery to find a hosted game lobby and automatically connect with it.

I tried the Network Game Lobby (beta) Asset from the Assetstore provided by Unity. This example has a perfectly working local Lobby, however I don’t know how to “merge” this with Local Discovery.

Connecting via IP works great in this example, but i don’t want mobile gamers to ask for IP & stuff.

Well, I tried the following:

  • I created a new Script “MyLocalDiscovery” and added it to the LobbyManager Game Object

     public class MyNetworkDiscovery : NetworkDiscovery
     {
         public override void OnReceivedBroadcast(string fromAddress, string data)
         {
             NetworkManager.singleton.networkAddress = fromAddress;
             NetworkManager.singleton.StartClient();
             StopBroadcast();
         }
     }
    
  • Then i edited the Method invoked by pressing the Play and Host Button

         public override void OnStartHost()
         {
             base.OnStartHost();
    
             ChangeTo(lobbyPanel);
             backDelegate = StopHostClbk;
             SetServerInfo("Hosting", networkAddress);
    
             discovery.Initialize();
             discovery.StartAsServer();
         }
    
  • Eventually, I edited the Method invoked by pressing the Join Button

         public void OnClickJoin()
         {
             lobbyManager.ChangeTo(lobbyPanel);
    
             /*
             lobbyManager.networkAddress = ipInput.text;
             lobbyManager.StartClient();
             */
    
             lobbyManager.discovery.Initialize();
             lobbyManager.discovery.StartAsClient();
    
             lobbyManager.backDelegate = lobbyManager.StopClientClbk;
             lobbyManager.DisplayIsConnecting();
    
             lobbyManager.SetServerInfo("Connecting...", lobbyManager.networkAddress);
         }
    

So, when I host a game a broadcast is started. By pressing join the client will look for a broadcast signal and connect to the host.

I really have no idea about networking, this is all i could figure out.

Your Help is much appriciated, thank you in advance!

The code above is alright to use. However, I experienced very odd behaviour and error

host id out of bound id {-1} max id should be greater 0 and less than {2}

thrown in:

    UnityEngine.Networking.NetworkTransport:ReceiveFromHost(Int32, Int32&, Int32&, Byte[], Int32, Int32&, Byte&)
    UnityEngine.Networking.NetworkDiscovery:Update() (at C:\buildslave\unity\build\Extensions\Networking\Runtime\NetworkDiscovery.cs:314)

when triggering StopBroadcast in OnReceivedBroadcast. I spent a lot of time in Unity source

[1] and found the solution:

Due **NetworkDiscovery.Update()** method poor design the method RecieveFromHost is called even after StopBroadcast is executed. Therefore it ends with error message.

The trick is to use **LateUpdate()** as:

    public class MyDiscovery : NetworkDiscovery
    {
        private bool end = false;
        public override void OnReceivedBroadcast(string fromAddress, string data)
        {
            Debug.Log("Broadcast recieved from" + fromAddress + " data:" + data);
            NetworkManager.singleton.networkAddress = fromAddress;
            NetworkManager.singleton.StartClient();
            end = true;
        }
        private void LateUpdate()
        {
            if (end)
            {
                StopBroadcast();
                end = false;
            }
        }
    }

Hope this helps someone.

  [1]: https://bitbucket.org/Unity-Technologies/networking/src/303d5875befe232c4833bbb31d9c918bdcd3c62e/Runtime/NetworkDiscovery.cs?at=2017.3&fileviewer=file-view-default