Configurable NetworkManager Example
This example demonstrates how to create a custom NetworkManager that allows for easy switching between different transport layers (e.g., UDP, Steam, or an offline mode) before the network is started. This is a common requirement for games that offer multiple ways to connect.
Setup
- Create a new GameObject and add the
ConfigurableNetworkManagercomponent to it. - In the inspector, assign your
UdpTransportandSteamTransportsocket factory assets to the corresponding fields.
Key Components
ConfigurableNetworkManager.cs
This script extends NetworkManager and provides a way to configure the transport layer before starting the server or client. It includes:
- Public Methods:
StartHost,StartClient, andStartServermethods that take aSocketTypeenum as an argument. These methods can be easily hooked up to a UI to allow the player to choose their desired connection method. - Transport Configuration: The
ConfigureNetworkmethod is called before starting the network to set theSocketFactoryandPeerConfigon theNetworkServerandNetworkClient. This is where the core logic for switching transports resides. - Offline Mode: The example shows how to implement an offline mode by setting
Server.Listeningtofalse. This prevents the server from listening for incoming connections, effectively running it in a single-player mode.
SocketType Enum
This simple enum defines the available transport options. In this example, it includes Offline, UDP, and Steam.
PeerConfig
The PeerConfig object allows you to fine-tune the transport layer's behavior. Setting this is optional, but the default values might be too low for some projects. You can find more details about the available settings in the API reference for the Config class.
How to Use
- Create a UI: In your scene, create a UI with buttons that call the public methods on the
ConfigurableNetworkManagerscript. For example, you could have a "Start Host (UDP)" button that callsStartHost(SocketType.UDP). - Assign Transports: In the inspector for your
ConfigurableNetworkManagerGameObject, assign yourUdpTransportandSteamTransportassets to the corresponding fields. - Run the Game: When you run the game, you can use your UI to start the server, client, or host with the desired transport layer.
This example provides a clean and reusable way to manage multiple transport layers in your game, giving you the flexibility to support different networking backends.