Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Resolved Separating Client and Server Code with Unity Netcode for GameObjects

Discussion in 'Dedicated Server' started by qtpoison, Sep 5, 2023.

  1. qtpoison

    qtpoison

    Joined:
    May 6, 2018
    Posts:
    11
    I am new to multiplayer and I've been searching online but haven't found a satisfactory way to cleanly separate client and server logic when using Unity Netcode for GameObjects. Although Unity provides a "Dedicated Server" build target, using this approach means that both the server and client binaries contain functionalities intended for both roles. This is undesirable for several reasons:

    1. File Size: The server build ends up being unnecessarily large because it includes client-side code and assets.
    2. Security: Exposing server-side code in a client build could pose security risks.
    Is there a way to maintain both server and client code within the same Unity project and yet build them separately, so that each build includes only the necessary files and functionalities?

    If this isn't feasible, would the recommended approach be to create separate projects for the client and server? I'd appreciate any advice on managing this more efficiently, while maintaining security and reducing build size.

    Thank you!
     
    Last edited: Sep 5, 2023
  2. CodeSmile

    CodeSmile

    Joined:
    Apr 10, 2014
    Posts:
    3,899
    Separate server and client code into separate assemblies.
    Uncheck „Any platform“ in the server code’s assembly definition and only check the dedicated server build target. Pretty sure that shows up in there as a separate platform but cannot confirm this right now. Doing so will include the server code only when building a dedicated server.

    Naturally none of the client or any other code can depend on the server code. Which is good software architectural practice.
     
  3. qtpoison

    qtpoison

    Joined:
    May 6, 2018
    Posts:
    11
    I couldn't find a checkbox named "Dedicated Server". Tbh I was thinking about the server to be something like a 2MB console application, not including anything but maybe MonoBehaviour.
     
    Last edited: Sep 6, 2023
  4. CodeSmile

    CodeSmile

    Joined:
    Apr 10, 2014
    Posts:
    3,899
    I have Windows Dedicated Server as a platform checkbox in Assembly Definitions:
    upload_2023-9-6_13-18-29.png

    Perhaps this may depend on either the Dedicated Server module not being installed for the editor version you are using, or you are using an editor version where these platforms aren't yet available - though I believe this should be in 2021.3 and definitely in 2022.3.

    The dedicated server still needs to include most of the UnityEngine modules but is supposed to perform code and asset stripping. You may want to read through the dedicated server manual pages.
     
  5. qtpoison

    qtpoison

    Joined:
    May 6, 2018
    Posts:
    11
    Thanks for the reply, I am using 2022.3.8f1, I have installed the required modules, as you can see here:
    Unity_Hub_7lRD1P2Alv.png

    Yet I still don't have the options you have in the image, what might be the issue? Unity_cDldD2U5mE.png
     
  6. CodeSmile

    CodeSmile

    Joined:
    Apr 10, 2014
    Posts:
    3,899
    Strange, I made the screenshot in 2023.1 but I was sure this existed back in 2022.
    Perhaps dedicated server is actually "Linux Headless Simulation". Could be that the windows builds for the dedicated server are new.
     
  7. cristianm_unity

    cristianm_unity

    Unity Technologies

    Joined:
    Oct 16, 2018
    Posts:
    234
    For versions where the Dedicated Server is not part of the assembly exclude platforms you should be able to still use the define constraints: The Dedicated Server builds always set the UNITY_SERVER define, so you can use it to strip specific assemblies or even compile out only some parts of the code by putting it behind `#if UNITY_SERVER`