Home > Mobile >  How to avoid getting TypeLoadExceptions in Unity when using/creating external assemblies?
How to avoid getting TypeLoadExceptions in Unity when using/creating external assemblies?

Time:02-24

I am currently building an external assembly for my Unity projects. For that i have created a new class library project in Visual Studio (Community 2019).

When using specific "System.[...]" resources (e.g. System.Random) i get a TypeLoadException when using the self created dll in my Unity Project.

TypeLoadException: Could not resolve type with token 01000010 (from typeref, class/assembly System.Random, System.Runtime, Version=5.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a)

The Unity version that i used for testing are 2021.1.14f1 and 2022.1.0b8.

I tried switching the framework version of the class library from .NET 5.0 even down to .NET Core 1.0 or also in Unity switching from IL2CPP to Mono or changing the Api Compatibility Level up and down.

Currently none of this has worked for me and somehow i dont find anything that is helping me online.

How can i get on the same framework level in my external visual studio project as the Unity projects that should use the dll?

Thanks a lot in advance!

CodePudding user response:


This is a summary of my comments beneath the question.


For pre-compiled .NET assemblies to use as plugins in Unity be sure your external project is either a .NET Standard 2 class library or .NET Framework 4.x class library. This happens to be the same profiles for code compiled by Unity. https://docs.unity3d.com/Manual/dotnetProfileSupport.html.

Note anything beyond .NET Standard 2 including .NET 5 & 6 are not supported in Unity or out.

Also I think your attempt of .NET Core 1.0 was just a bit too low. Try again with .NET Standard 2.0.

  • Related