Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice

Question Compile NetworkBehaviour into dll not working

Discussion in 'Code Editors & IDEs' started by zisaMan, Mar 16, 2024.

  1. zisaMan

    zisaMan

    Joined:
    Jun 27, 2013
    Posts:
    25
    Hello,
    i am trying to compile some NetworkBehaviour scripts into a DLL to be used inside a unity 2023.2.8f1 project. I'm using Visual Studio 2022 Managed Library project Net standard 2.1.

    My script contains [ServerRcp] and [ClientRpc] but once i compile it, the dll is not working as expected.

    I've inspected my script in my dll with IlSpy and it is clearly different compared to the same script compiled in Assemply-CSharp by Unity.

    I understand that i'm missing something but i don't know what, please can any one tell me how to correctly compile the dll?

    Thank you.
     
    Last edited: Mar 16, 2024
  2. CodeSmile

    CodeSmile

    Joined:
    Apr 10, 2014
    Posts:
    6,631
    We need to know more details. How is it set up? How does it differ? What does "not working" mean?

    Just in case: try compiling the DLL with .NET Standard 2.0.
     
  3. zisaMan

    zisaMan

    Joined:
    Jun 27, 2013
    Posts:
    25
    Hello, thanks for answering.

    That's my steps:
    • Created a Managed Library project in Visual Studio 2022.
    • Added references from UnityEditor Dirtectory for UnityEngine.CoreModule.dll
    • Added a reference to NetCode from a PC compiled build NetCode.Runtime

    Then i compiled my scripts. Here is an example of what i get:

    MyDLL: (it's the same code i have in the script)

    Code (CSharp):
    1.  
    2. using System.Collections.Generic;
    3. using Unity.Netcode;
    4. using UnityEngine;
    5.  
    6. [ServerRpc]
    7. private void deviceNameServerRpc()
    8. {
    9.     Debug.Log("*** Logged: " + SystemInfo.deviceType);
    10. }
    Assembly-CSharp.dll (compiled by unity):


    Code (CSharp):
    1.  
    2. using System.Collections.Generic;
    3. using Unity.Netcode;
    4. using UnityEngine;
    5.  
    6. [ServerRpc]
    7. private void deviceNameServerRpc()
    8. {
    9.     NetworkManager networkManager = base.NetworkManager;
    10.     if ((object)networkManager == null || !networkManager.IsListening)
    11.     {
    12.         return;
    13.     }
    14.     if (__rpc_exec_stage != __RpcExecStage.Server && (networkManager.IsClient || networkManager.IsHost))
    15.     {
    16.         if (base.OwnerClientId != networkManager.LocalClientId)
    17.         {
    18.             if (networkManager.LogLevel <= LogLevel.Normal)
    19.             {
    20.                 Debug.LogError("Only the owner can invoke a ServerRpc that requires ownership!");
    21.             }
    22.             return;
    23.         }
    24.         ServerRpcParams serverRpcParams = default(ServerRpcParams);
    25.         FastBufferWriter bufferWriter = __beginSendServerRpc(509992527u, serverRpcParams, RpcDelivery.Reliable);
    26.         __endSendServerRpc(ref bufferWriter, 509992527u, serverRpcParams, RpcDelivery.Reliable);
    27.     }
    28.     if (__rpc_exec_stage == __RpcExecStage.Server && (networkManager.IsServer || networkManager.IsHost))
    29.     {
    30.         Debug.Log("*** Logged: " + SystemInfo.deviceType);
    31.     }
    32. }
    i hope it is more clear.

    In general, the unexpected code behavuior is that my client executes code that must be executed by the server.
     
    Last edited: Mar 16, 2024
  4. CodeSmile

    CodeSmile

    Joined:
    Apr 10, 2014
    Posts:
    6,631
    I wouldn't expect that to work!

    You have to include the Netcode code in the project that builds the DLL. Plus any dependent code that Netcode requires, like com.unity.collections.


    Why are you trying to do this anyway?

    2023.3 added Multiplayer Roles so you can define what assets go into a Server vs Client build, if that's your intention.
     
  5. zisaMan

    zisaMan

    Joined:
    Jun 27, 2013
    Posts:
    25
    I am creating a package to be used in unity editor and avoid any code inspection/modification. I know it's a very basic protection but it is enough for what i have to do.

    So please how can i include the code in my Visual Studio project?
    Simply copying all com.unity.netcode.gameobjects folder or is there ia specific way?

    Thank you again for the help.
     
  6. CodeSmile

    CodeSmile

    Joined:
    Apr 10, 2014
    Posts:
    6,631
    Hopefully you're not trying to do that for a store asset.
    Putting code in a DLL that should be available as source deters potential customers. Developers with at least some experience avoid black boxes of other people's code.

    And as you said, the protection isn't even there. With Rider I can inspect your DLLs C# code with a single click, and that code almost looks like someone disciplined wrote it, it's not obscured with random names or such unless the DLL was explicitly obfuscated.

    Thinking about this, Netcode depends on other packages, like Collections, and these again may depend on more. Even if that worked, you will end up including a lot of package code within the DLL. This means all the types and namespaces are duplicated in your DLL and then you get duplicate definition errors when you add that DLL to a Unity project.

    I did a quick search for how a DLL can depend on a Unity Package Manager package but found nothing obvious. If at all possible, that's the concept you need to research into.

    The problem is that the package code itself is not meant to be compiled outside of a Unity project. And the way you got this DLL means the compiled package code would still contain duplicate definitions when added to a Unity project. On top, such a package DLL would be locked to the specific platform you built it for, and thus likely wouldn't work for mobile or web or Mac, just Windows if that was the build target.

    Overall, I'd just not do it. ;)
     
  7. zisaMan

    zisaMan

    Joined:
    Jun 27, 2013
    Posts:
    25
    Thank you so much for the advices. They were very helpful.

    Bye :D