Local Network Discovery Using Unet

Unity networking has a built-in network discovery component  which allows Unity games to find each other on a local network.This component has mainly used for

1)Broadcast their presence

2)Listen broadcast

3)Join the matching game(obviously, it’s optional but very useful) all these three works are done simultaneously.

This component broadcast data using UDP broadcast of the network transport layer.

Step 1: How it Works???

Before we are going further it’s important to know how it works?.This Component has several methods which need to be initialized during the game.check out a list here.

1) Initialize :

This is used to initialize the network discovery Component.The component can run in server mode or in client mode.

2) StartAsServer :

If the component starts as a server it called StartAsServer function to broadcast message into the local network.This message contains key and version of the game.

3) StartAsClient :

When in client mode, this will listen for the broadcast message on a specific port.When a message is received and the key matches with a key of the network component, the game is available to join.

4) OnReceivedBroadcast :

Network discovery has a virtual function OnReceivedBroadcast which use to receive a broadcast message.

5) StopBroadcast :

This component used to stop listening and broadcasting the message.

6) OnGUI :

Most of the developers don’t use this function because of its overhead.Even an empty OnGUI has a significant performance overhead.

Step 2: How to Customize this Component?

Unity technology provides a source of their networking features here.

1)Create a script called CustomNetworkDiscovery.cs Class in Unity.

2)Inherit this class from NetworkDiscovery Class in Unet.

3) Create your own initialization functions inside the class as defines above.

and call it from Start function.

initialize is done.

4) Create a server and broadcast their presence.

network discovery

5) Set up a manual broadcast data before broadcasting.After setting this data let’s start a broadcasting.

6) On the other side, you have to receive this broadcast data and connect this client to the server.To do this, create a Receive Broadcast function and start a client.

7) A network manager is used to connecting this server and client.

8) Start host from the network manager.

9) Start client from the network manager.

10) Smile!You did it.Get an Example Project here.

Still confused? See How to make a multiplayer game?Unity asset store has good plugins to start your multiplayer game easily.

Some other helpful assets in the multiplayer game.

15 thoughts on “Local Network Discovery Using Unet”

  1. Wonderful post with elegant simplicity in your sample Unity package. Works beautifully in Unity 5.6.2 but joining doesn’t seem to work in Unity 2017.1.

    Any thoughts?

  2. They are already included. LocalNetworkDiscovery still does not work with Unity 2017.1. Clicking the Join button does execute NetworkUi.ReceiveGameBroadcast, which in turn, runs ReceiveBroadscast and StartAsClient does run, but nothing seems to be detected (that is, there is no server with an IP Address that gets displayed, and therefore, nothing to join).

    Interestingly, the Host compiled on 2017.1 works fine with the Client compiled on Unity 5.6.2. So the problem is clearly on the client side, as no host is ever detected.

    You can PM me and I’ll be happy to provide as much detail as you need.

    If you have a working sample using Unity 2017.1 it would be worth turning that into a package.

  3. I got it working in 2017.1, as I can deploy to mobile devices or to a standalone executable on a desktop system. Any combination of standalone mobile/desktop easily discovers the running host.

    When running the player (CTRL+P) from the 2017.1 Editor, clicking the Join button never results in discovering a host IP. Possibly it’s some kind of a firewall issue?? This definitely doesn’t happen with Unity 5.6.

    The upshot is that with 2017.1 it’s harder to routinely develop/test, as I have to Build+Run to two devices, rather than testing on one end within the Unity Editor.

    If you do have further insight on this, I’d be appreciative. Anyhow, thanks for putting up the original code. It vastly simplified my coding task and helped to make clear essential networking concepts.

  4. I feel that is among the most significant information for
    me. And i am satisfied studying your article.
    However want to statement on few general things, The site style is great, the articles is in reality nice :
    D. Good task, cheers

  5. Thank you very much for this…
    It works very well for me, so I’d like to know how I can use this to create a lobby

  6. Your example scene worked great for me! It shows a bit of choppiness and lag though when connecting with two mobile devices. Any tips for reducing this choppiness or lag?

  7. Amazing Thankyou. Working great on Unity 2017.2.0f3 Personal 64bit between a Mac locally, and with an Android. I struggled with the official documentation but the code in this was clear and well structured.

Leave a Reply

Your email address will not be published. Required fields are marked *