Search code examples
unity-game-enginedll.net-4.5

TypeLoadException after loading .NET 4.5 C# DLL into Unity 2018.2.5


I want to be able to feed the camera frames from a webcam into Unity. I made a .NET 4.5 C# DLL using MediaFrameReader and event listeners. Here is some other user's implementation for accessing the Hololens camera frames: Hololens - Access Camera Frames.

When I import the DLL into my 2018.2.5 Unity project, it gives me the following error:

  1. Unloading broken assembly "....", this assembly can cause crashes in the runtime
  2. TypeLoadException: Could not find method due to a type load error

The C# plugin built successfully many times on Visual Studio. Also, I have properly set the Api Compatibility Level (in player settings) to .NET 4.x. What could be the fix to this?

All the other SO answers related to this I have already taken a look, but does not seem to help the problem. Thanks for all the help.

EDIT: https://issuetracker.unity3d.com/issues/unity-fails-to-load-net-4-dot-6-assemblies-with-typeloadexception is the most relatable post, but had no solution.


Solution

  • I have few possible solutions for you to explore.

    DLLs issues:

    Workaround:

    Comment your UWP code (the part that uses the DLL), then build it in UNITY without the DLL. In the generated UWP solution, install the package from nuget or manually import the dll, then uncomment your code and finish your development. This is a short-term solution.It is going to be annoying as you re-build your solution many times and have to comment/uncomment then re-add dlls and so on.

    Other possible solutions:

    Failed to run reference rewriter with command error with unity error when adding a DLL to the assets folder

    Your exact need

    From your description, you really do not need everything in the link you referenced (Hololens - Access Camera Frames). You need much simpler version. I recently created MediaCapture solution for HoloLens as a workaround because PhotoCapture in Unity is not working in the HoloLens and everything is working without any additional DLLs. I will post for you few links to see if it may help you:

    1. MediaCapture Unity & HoloLens: https://github.com/MSAlshair/HoloLensMediaCapture

    This maybe a good start for you. You can combine it with your original reference. Use this project as starting point to make sure your project is building correct, then use the necessary code from the other resource that you posted to accomplish the task that you desire. You may need to download Unity 2018.2.12f1 because I didn't test it in 2018.2.5

    1. MediaCapture & PhotoCapture: Hololens font camera

    Good Luck!