Using .NET 4.5 code in Unity 5

36,608

Solution 1

Using a visual studio plugin: unmanaged exports, I have been able to export the C# .NET 4.5 to an unmanaged/native dll which I can use in Unity Engine. This forces to call only simple functions, but with little work, it was possible for me to wrap the core of my engine.

Solution 2

Looks like Unity is upgrading to 4.6+ and is available in beta now

https://forum.unity3d.com/threads/upgraded-mono-net-in-editor-on-5-5-0b4.433541/

Solution 3

Short answer is no.

Unity has ".NET 2.0/3.5 era functionality," according to an official blog post.

According to the same post, a future version of Unity will be upgraded to "a modern version of .NET," with no timeline given for this major undertaking.

NOTE: This answer was written in response to the original version of the question, which was simply whether the .NET 4.5 library could be used directly in Unity.

Share:
36,608
Admin
Author by

Admin

Updated on July 05, 2022

Comments

  • Admin
    Admin almost 2 years

    I am working on a game using Unity3D version 5. The game should use strict business rules already programmed in a C# .NET4.5 engine. Using Xamarin/Mono, we hope to make this usable on windows, android, others...

    I tried building the engine into a dll and use it as a managed plugin in Unity. But Unity uses an old version of Mono to compile the whole game and refuses to use .NET 4.5 dlls.

    I really need to use this code, I looked into downgrading it to .NET 3.5 but it is not an acceptable solution given the work already done on the engine.

    Concerning android, I think to use Xamarin to get native android code and use it as a native plugin in Unity. Now I need a solution for Windows. I am looking into ways to use the C# code in C++ to make an unmanaged dll but I am struggling understanding which way is applicable here between COM interop, C++/CLI or other methods.

    Does any one knows a way to include code from .NET 4.5 in Unity with no/little effort ?

  • Admin
    Admin about 9 years
    You say short answer is no. But what is the long story? I really need to use this code, I looked into downgrading it to .NET 3.5 but it is not an acceptable solution given the work already done on the engine. Concerning android, I think to use Xamarin to get native android code and use it as a native plugin in Unity. Now I need a solution for Windows. I am looking into ways to use the C# code in C++ to make an unmanaged dll but I am struggling understanding which way is applicable here between COM interop, C++/CLI or other methods.
  • Ghopper21
    Ghopper21 about 9 years
    Even if you could downgrade to 3.5, that's no guarantee it would work, as Unity doesn't have full 3.5 support. Unity does support native Windows calls but how to convert your library to Windows native, I'm afraid I don't know. Good luck!
  • Admin
    Admin about 9 years
    Thanks, I'm editing the question to be more precise.
  • Ghopper21
    Ghopper21 about 9 years
    @Warrows - friendly tip about Stack Overflow. Best to be careful about editing questions in this way, where you are fundamentally changing the question, as it can make existing answers (like mine, which you previously accepted) incomplete or even wrong. This means other users may think my answer is a bad one, and may downvote it.
  • Ghopper21
    Ghopper21 about 9 years
    @Warrows - also, I think in this case you may be more likely to get specific help if you ask a separate, more specific question about how to convert C# library into Windows native code. The problem with a question about "little/no effort" is that it's vague, and some users may even vote to close your question as "too broad."
  • Admin
    Admin about 9 years
    Thank you for the tip, I was wondering what was the best course of action. I will be more careful in the future. I chose to edit because it is more specific that way. Also your answer is more a confirmation of my original concern than a solution to the problem.
  • jungle_mole
    jungle_mole over 8 years
    do you use dynamic, Async / Await etc in your engine? if yes, please give some tips how to wrap it
  • Admin
    Admin over 8 years
    It was some month ago in a professional project I can't access code anymore. But if I recall correctly, I had Async/Await in the code but did not need to call them through the wrapping so it was pretty straightforward.
  • jungle_mole
    jungle_mole over 8 years
    so, as long as you, say, checking in update loop some collection for incoming commands (i suppose collection must be locked) and produce raise some messages with subscribers "on the other end", it's all ok? what platforms the application was planned to be run on (or was run)?
  • Admin
    Admin over 8 years
    I am sorry I can't recall precisely what we did or did not succeed to do. But in the end we had to switch to .NET 3.5 for various reasons, this compatibility problem being one of them.
  • Arto Bendiken
    Arto Bendiken almost 7 years
    The current status as of June 2017: forum.unity3d.com/threads/…