Search Unity

Network Related C++ solution in Unity

Discussion in 'Scripting' started by enes-ozdemir, Apr 18, 2022.

  1. enes-ozdemir

    enes-ozdemir

    Joined:
    Sep 30, 2019
    Posts:
    1
    Hello,

    I have a network related C++ code/solution that I want to use in Unity.

    My C++ solution is about network operations and it includes async operations and threads. So I'm looking for a cross-platform solution that I could successfully "trigger" these C++ based network operation sdk/code from Unity(C#)

    The solutions that I considered are as follows:

    C++ / CLI --> In my current research I found some feedbacks about 2018 version which implies that C++/CLI is not supported by Unity (The resources I found on this are outdated.). However, I am not perfectly sure if this is the same case for more recent versions 2019-2020.
    Does anyone know if the latest versions support C++/CLI solution or not for such cross-platform solution?
    https://issuetracker.unity3d.com/is...n-calling-a-function-inside-a-c-plus-plus-dll

    PInvoke --> Is the PInvoke only solution capable of triggering the C++ solution from Unity(C#) and provide enough cross-platform support for managing async, thread, etc. operations without problem?

    C++ Native Plugin (C Extern) --> There are some basic samples for this solution.
    https://levelup.gitconnected.com/in...-as-plugins-a-step-by-step-guide-17ad70c2e3b4

    C++ with NDK --> Since my solution is for Android devices and Unity can use NDK to reference the so file of C++ code on the Android platform. This seems like a suitable solution as well.
    https://blog.fearcat.in/a?ID=00650-66034cf2-826b-4771-8ce2-1f160528598b

    Can anyone help about which of these solutions is the best for this kind of network related C++ code/solution and why?

    I would appreciate any guide/resource link on the suggested solutions.
     
    bar1scanHW likes this.
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,742
    If you haven't integrated at least a few native C++ packages, I warn you that starting with a multi-threaded networking package is a MASSIVE lift. It will only be made harder by your cross-platform requirement. You will essentially need to re-engineer the entire cross-platform networking solution that already exists in the UnityWebRequest class.

    Keep in mind that everything you do back on the Unity side (eg, via callback from networking) must be on the main thread, so any sort of threaded networking solution will require you to marshal delegates back to the main thread for dispatch into the Unity scene on the main thread.

    About the only thing I can suggest is to try the first one, evaluate it against all the targets you want it to run on, and soon you will appreciate what you are up against.

    Good luck!
     
    Bunny83 likes this.
  3. bhupiister

    bhupiister

    Joined:
    Dec 13, 2019
    Posts:
    42
    Even i was looking forward for support of C++/CLI in latest version of unity. I am currently working on CAD application which i intend to launch cross platform. Pinvoking takes lot of time and is not good option when you want to work with big libraries.

    Did you find any solution? @enes-ozdemir
     
  4. Bunny83

    Bunny83

    Joined:
    Oct 18, 2010
    Posts:
    4,000
    I think a lot of people have the wrong idea about "C++/CLI". This language is not C++. It's a .NET language and is fully compiled to managed IL code. Interaction with native code will always have the same issues as you have to bridge the gap between the managed world and the native world. You can essentially use any .NET language in Unity as long as you pre-compile that code yourself into a managed assembly and import that assembly into your Unity project. Though some .NET languages have additional runtime assemblies which it requires for it's own features which you have to ship with your game. I'm not sure if there are any integration issues of C++/CLI, VB .NET or any other .NET / mono language in Unity. Some parts may not be compatible with Unity. This may differ depending on the target platform.
     
  5. bhupiister

    bhupiister

    Joined:
    Dec 13, 2019
    Posts:
    42
    @Bunny83
    I am aware of the working side of the unity.
    Its just that unity uses mono and hence C++/CLI is forbidden. At least that what I have read.
    If I was making a mobile application in Android studio, support for linking library via C++/CLI library is supported.
    Just looking forward to same support on unity.