Realm Local DB in Unity

How can I include Local Realm DB files in Unity Build, both for Android and iOS? I have a huge amount of data stored in realm local database which I need to access for reading during gameplay, so I need a realm file which is included in installation file for Android and iOS. any ideas?

Hi @dats_fain,

This seems more a Unity question than a Realm question, so I don’t know how helpful I can be, but I’ll try.
It seems to me that you’re asking “how to include a file in a Unity build”. If that is the case the resource needed is Unity - Manual: Streaming Assets.
From what I can see, you need to place the Realm file in a folder named StreamingAssets. Once there you can reference the file in your code like

#if UNITY_IPHONE
var realmPath = Path.Combine(Application.streamingAssetsPath, "yourRealm.realm");
#endif
 
#if UNITY_ANDROID
var realmUri = Path.Combine(Application.streamingAssetsPath, "yourRealm.realm");
using (var webRequest = UnityWebRequest.Get(realmUri))
{
    // here handle the request and obtain the *realmFilePath*
}
var realmPath = *realmFilePath*;
#endif

var realm = Realm.GetInstance(realmPath);

This is some pseudo code which is not tested but it should point you in the right direction. And specifically, by what I read, Android needs a little more work, so make sure you follow the docs. My example should give you a decent start.

I hope this helps.

1 Like