Class NetworkDiscovery
The NetworkDiscovery component allows Unity games to find each other on a local network. It can broadcast presence and listen for broadcasts, and optionally join matching games using the NetworkManager.
This component can run in server mode (by calling StartAsServer) where it broadcasts to other computers on the local network, or in client mode (by calling StartAsClient) where it listens for broadcasts from a server. This class should be override to receive calls from OnReceivedBroadcast.
Note : Do not use void Update() in a class that inherits from NetworkDiscovery. If needed, you must override it and call base.Update() instead.
using UnityEngine;
using UnityEngine.Networking;
using System.Collections;
public class OverriddenNetworkDiscovery : NetworkDiscovery
{
public override void OnReceivedBroadcast(string fromAddress, string data)
{
NetworkManager.singleton.networkAddress = fromAddress;
NetworkManager.singleton.StartClient();
}
}
Inherited Members
Namespace: UnityEngine.Networking
Syntax
[DisallowMultipleComponent]
[AddComponentMenu("Network/NetworkDiscovery")]
[Obsolete("The high level API classes are deprecated and will be removed in the future.")]
public class NetworkDiscovery : MonoBehaviour
Properties
broadcastData
The data to include in the broadcast message when running as a server.
If using NetworkManager integration, this will be overriden with the NetworkManager's address.
Declaration
public string broadcastData { get; set; }
Property Value
Type | Description |
---|---|
String |
broadcastInterval
How often in milliseconds to broadcast when running as a server.
Declaration
public int broadcastInterval { get; set; }
Property Value
Type | Description |
---|---|
Int32 |
broadcastKey
A key to identify this application in broadcasts.
Declaration
public int broadcastKey { get; set; }
Property Value
Type | Description |
---|---|
Int32 |
broadcastPort
The network port to broadcast on and listen to.
Declaration
public int broadcastPort { get; set; }
Property Value
Type | Description |
---|---|
Int32 |
broadcastsReceived
A dictionary of broadcasts received from servers.
The key is the server address, and the value is a NetworkBroadcastResult object that contains the data sent by the server.
Declaration
public Dictionary<string, NetworkBroadcastResult> broadcastsReceived { get; }
Property Value
Type | Description |
---|---|
Dictionary<String, NetworkBroadcastResult> |
broadcastSubVersion
The sub-version of the application to broadcast. This is used to match versions of the same application.
Declaration
public int broadcastSubVersion { get; set; }
Property Value
Type | Description |
---|---|
Int32 |
broadcastVersion
The version of the application to broadcast. This is used to match versions of the same application.
Declaration
public int broadcastVersion { get; set; }
Property Value
Type | Description |
---|---|
Int32 |
hostId
The TransportLayer hostId being used (read-only).
Declaration
public int hostId { get; set; }
Property Value
Type | Description |
---|---|
Int32 |
isClient
True if running in client mode (read-only).
Declaration
public bool isClient { get; set; }
Property Value
Type | Description |
---|---|
Boolean |
isServer
True if running in server mode (read-only).
Declaration
public bool isServer { get; set; }
Property Value
Type | Description |
---|---|
Boolean |
offsetX
The horizontal offset of the GUI if active.
Declaration
public int offsetX { get; set; }
Property Value
Type | Description |
---|---|
Int32 |
offsetY
The vertical offset of the GUI if active.
Declaration
public int offsetY { get; set; }
Property Value
Type | Description |
---|---|
Int32 |
running
True is broadcasting or listening (read-only).
Declaration
public bool running { get; set; }
Property Value
Type | Description |
---|---|
Boolean |
showGUI
True to draw the default Broacast control UI.
Declaration
public bool showGUI { get; set; }
Property Value
Type | Description |
---|---|
Boolean |
useNetworkManager
True to integrate with the NetworkManager.
When running as a server, this will include the NetworkManager's address in broadcast messages. When running as a client, this will be able to join matching games found by using the NetworkManager.
Declaration
public bool useNetworkManager { get; set; }
Property Value
Type | Description |
---|---|
Boolean |
Methods
Initialize()
Initializes the NetworkDiscovery component.
Declaration
public bool Initialize()
Returns
Type | Description |
---|---|
Boolean | Return true if the network port was available. |
OnReceivedBroadcast(String, String)
This is a virtual function that can be implemented to handle broadcast messages when running as a client.
Declaration
public virtual void OnReceivedBroadcast(string fromAddress, string data)
Parameters
Type | Name | Description |
---|---|---|
String | fromAddress | The IP address of the server. |
String | data | The data broadcast by the server. |
StartAsClient()
Starts listening for broadcasts messages.
Declaration
public bool StartAsClient()
Returns
Type | Description |
---|---|
Boolean | True is able to listen. |
StartAsServer()
Starts sending broadcast messages.
Declaration
public bool StartAsServer()
Returns
Type | Description |
---|---|
Boolean | True is able to broadcast. |
StopBroadcast()
Stops listening and broadcasting.
Declaration
public void StopBroadcast()