Class NetworkDiscovery | Multiplayer HLAPI | 1.0.8
docs.unity3d.com
    Show / Hide Table of Contents

    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();
       }
    }
    Inheritance
    Object
    Object
    Component
    Behaviour
    MonoBehaviour
    NetworkDiscovery
    Inherited Members
    MonoBehaviour.IsInvoking()
    MonoBehaviour.CancelInvoke()
    MonoBehaviour.Invoke(String, Single)
    MonoBehaviour.InvokeRepeating(String, Single, Single)
    MonoBehaviour.CancelInvoke(String)
    MonoBehaviour.IsInvoking(String)
    MonoBehaviour.StartCoroutine(String)
    MonoBehaviour.StartCoroutine(String, Object)
    MonoBehaviour.StartCoroutine(IEnumerator)
    MonoBehaviour.StartCoroutine_Auto(IEnumerator)
    MonoBehaviour.StopCoroutine(IEnumerator)
    MonoBehaviour.StopCoroutine(Coroutine)
    MonoBehaviour.StopCoroutine(String)
    MonoBehaviour.StopAllCoroutines()
    MonoBehaviour.print(Object)
    MonoBehaviour.useGUILayout
    MonoBehaviour.runInEditMode
    Behaviour.enabled
    Behaviour.isActiveAndEnabled
    Component.GetComponent(Type)
    Component.GetComponent<T>()
    Component.GetComponent(String)
    Component.GetComponentInChildren(Type, Boolean)
    Component.GetComponentInChildren(Type)
    Component.GetComponentInChildren<T>(Boolean)
    Component.GetComponentInChildren<T>()
    Component.GetComponentsInChildren(Type, Boolean)
    Component.GetComponentsInChildren(Type)
    Component.GetComponentsInChildren<T>(Boolean)
    Component.GetComponentsInChildren<T>(Boolean, List<T>)
    Component.GetComponentsInChildren<T>()
    Component.GetComponentsInChildren<T>(List<T>)
    Component.GetComponentInParent(Type)
    Component.GetComponentInParent<T>()
    Component.GetComponentsInParent(Type, Boolean)
    Component.GetComponentsInParent(Type)
    Component.GetComponentsInParent<T>(Boolean)
    Component.GetComponentsInParent<T>(Boolean, List<T>)
    Component.GetComponentsInParent<T>()
    Component.GetComponents(Type)
    Component.GetComponents(Type, List<Component>)
    Component.GetComponents<T>(List<T>)
    Component.GetComponents<T>()
    Component.CompareTag(String)
    Component.SendMessageUpwards(String, Object, SendMessageOptions)
    Component.SendMessageUpwards(String, Object)
    Component.SendMessageUpwards(String)
    Component.SendMessageUpwards(String, SendMessageOptions)
    Component.SendMessage(String, Object)
    Component.SendMessage(String)
    Component.SendMessage(String, Object, SendMessageOptions)
    Component.SendMessage(String, SendMessageOptions)
    Component.BroadcastMessage(String, Object, SendMessageOptions)
    Component.BroadcastMessage(String, Object)
    Component.BroadcastMessage(String)
    Component.BroadcastMessage(String, SendMessageOptions)
    Component.transform
    Component.gameObject
    Component.tag
    Component.rigidbody
    Component.rigidbody2D
    Component.camera
    Component.light
    Component.animation
    Component.constantForce
    Component.renderer
    Component.audio
    UnityEngine.Component.guiText
    Component.networkView
    UnityEngine.Component.guiElement
    UnityEngine.Component.guiTexture
    Component.collider
    Component.collider2D
    Component.hingeJoint
    Component.particleSystem
    Object.GetInstanceID()
    Object.GetHashCode()
    Object.Equals(Object)
    Object.Instantiate(Object, Vector3, Quaternion)
    Object.Instantiate(Object, Vector3, Quaternion, Transform)
    Object.Instantiate(Object)
    Object.Instantiate(Object, Transform)
    Object.Instantiate(Object, Transform, Boolean)
    Object.Instantiate<T>(T)
    Object.Instantiate<T>(T, Vector3, Quaternion)
    Object.Instantiate<T>(T, Vector3, Quaternion, Transform)
    Object.Instantiate<T>(T, Transform)
    Object.Instantiate<T>(T, Transform, Boolean)
    Object.Destroy(Object, Single)
    Object.Destroy(Object)
    Object.DestroyImmediate(Object, Boolean)
    Object.DestroyImmediate(Object)
    Object.FindObjectsOfType(Type)
    Object.DontDestroyOnLoad(Object)
    Object.DestroyObject(Object, Single)
    Object.DestroyObject(Object)
    Object.FindSceneObjectsOfType(Type)
    Object.FindObjectsOfTypeIncludingAssets(Type)
    Object.FindObjectsOfType<T>()
    Object.FindObjectOfType<T>()
    Object.FindObjectsOfTypeAll(Type)
    Object.FindObjectOfType(Type)
    Object.ToString()
    Object.name
    Object.hideFlags
    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()
    Back to top
    Copyright © 2023 Unity Technologies — Terms of use
    • Legal
    • Privacy Policy
    • Cookies
    • Do Not Sell or Share My Personal Information
    • Your Privacy Choices (Cookie Settings)
    "Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
    Generated by DocFX on 18 October 2023