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: .. code-block:: text 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 ------------------ 1. Copy the following scripts into **both projects**: - ``UCNetwork.cs`` - ``ClientNetwork.cs`` - ``ServerNetwork.cs`` - ``NetworkSync.cs`` 2. Make sure the **Lidgren.Network.dll** dependency is available in your project. You can place it inside ``Assets/Plugins/`` in both Client and Server. 3. (Optional) If you want voice chat later, also include ``VoIPManager`` and related stubs, though these are not required. Unity Scene Setup ----------------- **Client Scene** 1. Create a new empty GameObject called ``NetworkManager``. 2. Add the ``ClientNetwork`` component to it. 3. (Optional) Add your own bootstrap script to handle connecting: .. code-block:: csharp public class ClientBoot : MonoBehaviour { public ClientNetwork client; void Start() { client.Connect("127.0.0.1", UCNetwork.port, "Player01", "", "Player", 0); } } **Server Scene** 1. Create a new empty GameObject called ``ServerManager``. 2. Add the ``ServerNetwork`` component to it. 3. (Optional) Add your own script to handle approvals and logging: .. code-block:: csharp 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 ``NetworkSync`` component. - Have a **unique name** (the prefab name is sent across the network). Example: .. code-block:: text 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 -nographics`` command 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 ``NetworkSync`` in a ``Resources/`` folder. - [ ] Add a ``ClientNetwork`` component to your Client scene. - [ ] Add a ``ServerNetwork`` component 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 ``ClientNetwork`` or ``ServerNetwork`` to a manager GameObject. - Networked prefabs must live in ``Resources/`` and include ``NetworkSync``. - Default port is 603; open it on your host. - Test locally first, then deploy the server as a headless build.