Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

How did the Unity team combine C++ and C#?

Discussion in 'Editor & General Support' started by agostonr, Mar 23, 2018.

  1. agostonr

    agostonr

    Joined:
    Jan 3, 2016
    Posts:
    193
    I'm not looking for a step by step tutorial, just the basics of it.
    I'm writing some stuff in C++ because it's fast and I found a great many of libraries that can help me out, but when the C++ part of the app is ready and I'm just using it, I want to use a higher level language to script in it.Note it doesn't necessarily have to be C# though. If you know how to pull this off with another high level language, I'm all ears. I asked C# because Unity uses C#.
    I'd like someone who did this stuff before or knows about these to share their knowledge with me.
    Thanks~
     
    Last edited: Mar 23, 2018
  2. JeffDUnity3D

    JeffDUnity3D

    Joined:
    May 2, 2017
    Posts:
    14,446
    You can wrap a DLL around your C++ methods that can then be called from a C# application
     
  3. agostonr

    agostonr

    Joined:
    Jan 3, 2016
    Posts:
    193
    Thanks for the answer.
    And does that require the JNI magic like for a Java app, or it's much simpler?
     
  4. JeffDUnity3D

    JeffDUnity3D

    Joined:
    May 2, 2017
    Posts:
    14,446
    I'm not familiar with Java nor JNI so I would say no! You just need to create stubs for each C++ method within your DLL. Google "calling C++ from C#" for more info.
     
  5. agostonr

    agostonr

    Joined:
    Jan 3, 2016
    Posts:
    193
    Will do :D
    In my case though I might have to use Lua or Kotlin/Java (so I can run the stuff on Android). Again, I asked for C# cuz Unity uses that.
    I'm looking into SWIG as well, unsure how usable that is.
    I might as well build using Xamarin and actually use C# myself :D will find out in due time.
     
  6. JeffDUnity3D

    JeffDUnity3D

    Joined:
    May 2, 2017
    Posts:
    14,446
    You don't need Xamarin to code in C# in Unity when building for Android.
     
  7. agostonr

    agostonr

    Joined:
    Jan 3, 2016
    Posts:
    193
    I know, but I would if I use my own s***, not Unity.
    See, I'm building an engine meself, obviously nothing as complex as Unity, but it's practice.
    Right now I'm using lwjgl, but it doesn't have up to date binding to bullet physics or box2d, so I'll rewrite my renderer in C++ (basically copy-paste, some minor adjustments). Another option is to write a physics engine which I'll start, but won't see through.

    With java I can easily interop Kotlin, but with C++, not so much.
    So I might forget about Kotlin and use C#.
    But C# won't run on Android just like that. In your case it does, because you do some magic (not sure what). In my case it won't.