Create a database (Roaming Databases)

You must create a database before you can use the package. Creating a database requires a storage location option and generates a globally unique identifier (GUID) for the database. The GUID uniquely and globally identifies the database. The storage location option tells the library where to create and store the database; it has the following options:

  • StorageLocation.Local - The database remains on the local disk.
  • StorageLocation.Remote - The database downloads and uploads to Unity Gaming Services' cloud storage.

The Roaming Database library manages the filesystem paths regardless of the storage location.

The following code snippet shows an example of using the StorageLocation.Local option:

using System;
using UnityEngine;
using Unity.Services.Realm.Database;

public class DatabaseManager : MonoBehaviour
{
    private IDatabaseConnection db;
    private string dbId = null;

    private async void OnEnable()
    {
        if (dbId == null)
        {
            // Create a new local database
            db = await DatabaseService.Instance.CreateDatabaseAsync(StorageLocation.Local);
            Debug.Log($"Created database {db.Id} at {db.FilePath}...");
            dbId = db.Id.ToString();
        }
        else
        {
            // Load an existing local database
            db = DatabaseService.Instance.OpenLocalDatabase(dbId);
        }
    }
}

The example includes a function to create the database and returns the connection. Creating a new database also calls the function to open the connection before returning it. You only need open the database connection if you’ve already closed the connection.