Installation & Project Setup
This networking library is designed to be used inside Unity, and it expects you to set up two separate Unity projects: one for the Client and one for the Server. The server project runs the authoritative simulation, while the client project runs the player-facing game.
Prerequisites
Unity (2021 or newer recommended)
.NET 4.x scripting runtime
The Lidgren.Network library (already bundled in this project)
Basic familiarity with Unity scenes, prefabs, and components
Project Layout
You should create two Unity projects side by side:
MyGame/
├── Client/ (Unity project for the client build)
└── Server/ (Unity project for the server build)
This separation makes it easier to build, deploy, and test each role independently. Both projects can share code via Unity packages or a shared folder linked into each project.
Adding the Library
Copy the following scripts into both projects: -
UCNetwork.cs-ClientNetwork.cs-ServerNetwork.cs-NetworkSync.csMake sure the Lidgren.Network.dll dependency is available in your project. You can place it inside
Assets/Plugins/in both Client and Server.(Optional) If you want voice chat later, also include
VoIPManagerand related stubs, though these are not required.
Unity Scene Setup
Client Scene
Create a new empty GameObject called
NetworkManager.Add the
ClientNetworkcomponent to it.(Optional) Add your own bootstrap script to handle connecting:
public class ClientBoot : MonoBehaviour { public ClientNetwork client; void Start() { client.Connect("127.0.0.1", UCNetwork.port, "Player01", "", "Player", 0); } }
Server Scene
Create a new empty GameObject called
ServerManager.Add the
ServerNetworkcomponent to it.(Optional) Add your own script to handle approvals and logging:
public class ServerBoot : MonoBehaviour { public ServerNetwork server; void ConnectionRequest(ServerNetwork.ConnectionRequestInfo info) { // Example: approve everyone for now server.ConnectionApproved(info.id); } }
Networking Prefabs
All prefabs that you want to instantiate across the network must:
Be placed in a
Resources/folder.Contain a
NetworkSynccomponent.Have a unique name (the prefab name is sent across the network).
Example:
Assets/
└── Resources/
├── PlayerAvatar.prefab
├── Monster.prefab
└── Door.prefab
Ports and Firewall
Default port: 603 (set in
UCNetwork.port).Make sure this port is open on your server host machine.
Clients must know the server’s IP address (LAN or WAN).
Testing in Editor
You can run the server project in one Unity editor instance and the client project in another.
For local testing, connect to
127.0.0.1.For multiple clients, run multiple builds of the client project while the server runs in Editor.
Headless Server Builds
For deployment, build the server project as a headless Unity build:
In the build settings, select Server Build or add the
-batchmode -nographicscommand line arguments.This runs without rendering and uses fewer resources.
Checklist
[ ] Create two Unity projects: Client and Server.
[ ] Add core scripts (
UCNetwork,ClientNetwork,ServerNetwork,NetworkSync) to both.[ ] Place networked prefabs with
NetworkSyncin aResources/folder.[ ] Add a
ClientNetworkcomponent to your Client scene.[ ] Add a
ServerNetworkcomponent to your Server scene.[ ] Test locally by running both projects and connecting to 127.0.0.1.
Summary
Use two Unity projects: one for the server, one for the client.
Add
ClientNetworkorServerNetworkto a manager GameObject.Networked prefabs must live in
Resources/and includeNetworkSync.Default port is 603; open it on your host.
Test locally first, then deploy the server as a headless build.