Important: UNet is a deprecated solution, and a new Multiplayer and NetworkingThe Unity system that enables multiplayer gaming across a computer network. More info See in Glossary Solution (Netcode for GameObjects) is under development. For more information and next steps see the information on the Unity Netcode for GameObjects website. |
The Network Discovery component allows Unity multiplayer games to find each other on a local area network (a LAN). This means your players don’t have to find out the IP address of the host to connect to a game on a LAN. Network Discovery doesn’t work over the internet, only on local networks. For internet-based games, see the MatchMaker service.
The Network Discovery component can broadcast its presence, listen for broadcasts from other Network Discovery components, and optionally join matching games using the Network ManagerA Networking component that manages the network state of a project. More info
See in Glossary. The Network Discovery component uses the UDP broadcast feature of the network transport layer.
To use local network discovery, create an empty GameObjectThe fundamental object in Unity scenes, which can represent characters, props, scenery, cameras, waypoints, and more. A GameObject’s functionality is defined by the Components attached to it. More info
See in Glossary in the SceneA Scene contains the environments and menus of your game. Think of each unique Scene file as a unique level. In each Scene, you place your environments, obstacles, and decorations, essentially designing and building your game in pieces. More info
See in Glossary, and add the Network Discovery component to it.
Like the Network Manager HUD, this component has a default GUI that shows in the Game view for controlling it, intended for temporary developmental work, with the assumption that you will create your own replacement for it before finishing your game. Note that you also need a Network Manager component in the Scene to be able to join a game through the Network Discovery GUI. When the game starts, click the Initialize Broadcast button in the Network Discovery GUI (in the Game view) to send a broadcast and begin discovery of other games on the local network.
The Network Discovery component can run in server mode (activated by clicking the “Start Broadcasting” button in the GUI), or client mode (activated by clicking the ‘Listen for Broadcast’ button in the GUI).
When in server mode, the Network Discovery component sends broadcast messages over the network on the port specified in the inspectorA Unity window that displays information about the currently selected GameObject, asset or project settings, allowing you to inspect and edit the values. More info
See in Glossary. These messages contain the Broadcast Key and Broadcast Version of the game. You can set these to any value you like, their purpose is to identify this particular version and release of your game to avoid conflicts - such as your game trying to join a game of a different type. You should change the Broadcast Key value when releasing a new build of your game that should not be able to connect to older versions of your game. The component should be run in server mode if a game is being hosted on that machine. Without the default GUI, you need to call the StartAsServer() function to make the component run in server mode.
When in client mode, the component listens for broadcast messages on the specified port. When a message is received, and the Broadcast Key in the message matches the Broadcast Key in the Network Discovery component, this means that a game is available to join on the local network. Without the default GUI, you need to call the StartAsClient() function to make the component run in client mode.
When using the default GUI and listening for broadcasts in client mode, if a game is discovered on the local network, a button appears which allows the user of that client to join the game. The button is labeled “Game at:” followed by the host’s IP address.
There is a virtual function on the Network Discovery component that can be implemented to be notified when broadcast messages are received.
public class MyNetworkDiscovery: NetworkDiscovery {
public override void OnReceivedBroadcast(string fromAddress, string data)
{
Debug.Log("Received broadcast from: " + fromAddress+ " with the data: " + data);
}
}
For more information, see the Scripting API Reference documentation on NetworkDiscovery. Note that you cannot have a Network Discovery server and client running in the same process at the same time.
When you visit any website, it may store or retrieve information on your browser, mostly in the form of cookies. This information might be about you, your preferences or your device and is mostly used to make the site work as you expect it to. The information does not usually directly identify you, but it can give you a more personalized web experience. Because we respect your right to privacy, you can choose not to allow some types of cookies. Click on the different category headings to find out more and change our default settings. However, blocking some types of cookies may impact your experience of the site and the services we are able to offer.
More information
These cookies enable the website to provide enhanced functionality and personalisation. They may be set by us or by third party providers whose services we have added to our pages. If you do not allow these cookies then some or all of these services may not function properly.
These cookies allow us to count visits and traffic sources so we can measure and improve the performance of our site. They help us to know which pages are the most and least popular and see how visitors move around the site. All information these cookies collect is aggregated and therefore anonymous. If you do not allow these cookies we will not know when you have visited our site, and will not be able to monitor its performance.
These cookies may be set through our site by our advertising partners. They may be used by those companies to build a profile of your interests and show you relevant adverts on other sites. They do not store directly personal information, but are based on uniquely identifying your browser and internet device. If you do not allow these cookies, you will experience less targeted advertising. Some 3rd party video providers do not allow video views without targeting cookies. If you are experiencing difficulty viewing a video, you will need to set your cookie preferences for targeting to yes if you wish to view videos from these providers. Unity does not control this.
These cookies are necessary for the website to function and cannot be switched off in our systems. They are usually only set in response to actions made by you which amount to a request for services, such as setting your privacy preferences, logging in or filling in forms. You can set your browser to block or alert you about these cookies, but some parts of the site will not then work. These cookies do not store any personally identifiable information.