Docs Menu

Integrate Realm with Unity - .NET SDK

On this page

  • Overview
  • Prerequisites
  • Install
  • Import Realm
  • Using Realm in Your Unity Project
  • Additional Examples

This page contains information on how to install and integrate MongoDB Realm into your Unity project.

Known Issues When Developing With Unity

The following is a list of known issues when integrating Realm with Unity:

  • The Realm .NET SDK only supports Intel64 Macs and does not yet support Macs with the Apple M1 chip. Set your build settings: Architecture to "Intel64" in order to allow building for MacOS.
  • On Windows, your project may crash when running multiple instances of your project since multiple processes are attempting to access the same realm. To avoid these crashes, explicitly specify different names for each respective realm.

The Realm .NET SDK may be compatible with other versions of Unity, but 2020.3.12f1 (LTS) is the version that the Realm team uses for testing and development. We recommend using this version to ensure your project works with Realm and that the install steps match the steps below since Unity's UI often changes between versions.

Realm provides various ways to install the Realm .NET SDK for use with Unity. Experienced Unity developers may find installing Realm manually with a tarball to be intuitive. However, we recommend installing the Realm .NET SDK via npm since it provides notifications of version updates through Unity's package manager.

Create a C# script or use a C# script you have already created. Open that script in Visual Studio or another text editor and add the following line to import your Realm package:

using Realms;

When developing with Realm .NET SDK, the API methods are the same regardless of whether you use Unity or another platform. However, since Unity has some scripting restrictions, you should keep the following additional considerations in mind when developing your project:

Unity does not support the dynamic keyword when using IL2CPP. This means all types used in C# scripts must be known at compile time. When you use the Realm .NET SDK with Unity, you must convert dynamic types to concrete types. To view the full list of limitations, see the Unity scripting limitations documentation. Although the Realm .NET SDK exposes the Dynamic API, we recommend it only when performing migrations.


The following example shows a portion of a code block for performing a migration to illustrate the usage of the DynamicAPI.

var dynamicPeople = (IQueryable<RealmObject>)migration.OldRealm.All("Person");
var personName = dynamicPeople.First().DynamicApi.Get<string>("Name");

Unity performs managed code stripping, discarding any unused code from a build to reduce binary size. This may lead to issues when deserializing BSON into C# classes. For platforms that use IL2CPP, such as iOS, managed code stripping is enabled by default. When working with BSON, use the [Preserve] attribute to prevent managed code stripping on types properties that are only populated by the serializer. Since those properties use reflection, Unity cannot statically infer that the property setter is used. This means that unless you apply the [Preserve] attribute, Unity will strip those properties away. For examples of when you may perform BSON deserialization, check out the Query MongoDB and Call a Function documentation.

The Realm .NET SDK cannot be accessed within the AppDomain.DomainUnload Event or the Application.quitting event. This means you cannot write data to a Realm while the player application is quitting. If you need to store some data just before the app exits, consider using the Application.wantsToQuit event instead.

The MongoDB Realm community has created many projects that demonstrate the usage of the Realm .NET SDK. Check out the examples below that demonstrate the usage of the Realm .NET SDK with Unity!


The MongoDB Documentation team does not directly maintain these examples.

Give Feedback
MongoDB logo
© 2021 MongoDB, Inc.


  • Careers
  • Legal Notices
  • Privacy Notices
  • Security Information
  • Trust Center
© 2021 MongoDB, Inc.