C Sharp Driver in Unity


we are using Mongo DB Atlas as our Unity game backend DB.

The backend is a Unity instance (currently 2021.3.12) that runs on linux servers (mono build).

Some time ago, we have been able to get the 2.11.6 driver working using a nuget install with some minor changes (removing some libraries for different .Net version etc.).

We would like to update to the latest version 2.18.0 but fail to internal error in the library. The latest one came from the BSON dll: “Unable to resolve reference ‘System.Runtime.CompilerServices.Unsafe’.”

I was wondering:

  • Is the CSharp driver recommended to be used with Unity 3D?
  • If not, what is the recommended way of using MongoDB with Unity 3D?
  • Does it also work with IL2CPP? (we tried that with the old version but gave up)
  • Did anyone from the community manage to get the 2.18.0 csharp driver working in Unity (and how)?

Thank you very much in advance!

We manage to get working :tada:

The trick was using a dotnet classlibrary project targeting .Net Standard 2.0:
> dotnet new classlib --framework "netstandard2.0" -o MongoDBUnity
> cd MongoDBUnity
> dotnet add package MongoDB.Driver
> dotnet publish

This will collect all the required dependencies:

Add this to the Unity project and it worked.

Note: This is only for the mono backend. We did not test it with IL2CCP (and I doubt that would work).

1 Like

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.