Firestore

Introduction

A flexible, scalable database for our games. Like realtime database, it keeps user data in sync across client apps.

Prerequisites

  • You must have access to Firebase console project of the game

Create Firestore Database

  1. Go to the Firestore Database on the sidebar

  2. Click on “Create database”

  1. It will present a popup to select the location of the database. Keep the default values

  2. Click Next

  1. Select “Start in production mode” and click "Create"

  1. Copy-paste the code block given below into the Rules window of your Firestore Database.

rules_version = '2';
service cloud.firestore 
{
  match /databases/{database}/documents 
  {
    function signedIn() { return request.auth != null; }
  
    // Device-like IDs: 32-hex OR UUID-with-dashes OR generic 16–64 hex
    function isDeviceId(id) {
      return
        id.matches('^[0-9A-Fa-f]{32}$') ||                         // 32 hex
        id.matches('^[0-9A-Fa-f]{8}-[0-9A-Fa-f]{4}-[0-9A-Fa-f]{4}-[0-9A-Fa-f]{4}-[0-9A-Fa-f]{12}$') || // UUID
        id.matches('^[0-9A-Fa-f]{16,64}$');                         // other hex lengths (e.g., 16, 40, 64)
    }
       
    // /<anyCollection>/<UID or deviceId>
    match /{collectionId}/{userId} {
      allow read, write: if signedIn() && (
        request.auth.uid == userId ||      // own UID doc
        isDeviceId(userId)                 // any deviceId doc (see caveat)
      );
    }
       
    // Any depth under those docs
    match /{collectionId}/{userId}/{document=**} {
      allow read, write: if signedIn() && (
        request.auth.uid == userId ||
        isDeviceId(userId)
      );
    }
       
    // Deny everything else
    match /{document=**} {
      allow read, write: if false;
    }
  }
}
  1. Click on Authentication/Sign-in method and click the "Add new provider" button. Enable Email/Password and Facebook from here.\

At this point, Firestore dashboard setup is complete!!

Configure Firestore in Unity

  1. Install the Cloud Firestore & Firebase Authentication packages from Lion Registry under My Registries→ Lion Studios - Third Party SDKs→ Cloud Firestore.

Install Firestore And Auth
  1. Go to LionStudios → Settings Manager -> Game Interfaces -> Under Storage, select Firestore.

Optionally generate a template if you need a custom storage implementation.

The Firestore storage interface is initialized automatically, when it is the selected. For a visual walkthrough of the Settings Manager, see Game Interfaces.

Implementation

Merge Conflict Data

If you're using Firestore as your save data backend, you must pass a MergeConflictData object to all LionStorage.Save() method calls. This parameter is required for automatic conflict resolution when syncing data between devices.\

This MergeConflictData object has the following fields you can set.

  • PlayerXP: This is where you pass information about the players progression in the game. If you have any internal progression tracking metric then use that here.

    • Tip: In the case that if your game doesn't have a PlayerXP equivalent, you should pass some data variable that increases every time the save method is called.

  • LastPurchaseUtc: Set this to the timestamp of the user's most recent in-app purchase. You will have to keep track of this timestamp internally.

This MergeConflictData must be passed to any LionStorage.Save(MergeConflictData) method calls you make. More info about that here. Below is an example of how to use it:

MergeConflictData data = new MergeConflictData();
data.PlayerXP += amount;
data.LastPurchaseUtc = InternalDataManager.GetLastPurchaseTime(); // Your logic here
await LionStorage.Save(data);

If there is a conflict between two device's data, then our system chooses user data depending upon,

  • Whichever device has the most recent IAP purchase time

  • Higher value of playerXP

  • Most recent save time (This save time is tracked internally by us)\

Samples & Testing

  1. Import the sample from Package Manager → My Registries → Lion - Remote Storage → Sample.

Package Manager
  1. Add the example scenes to your Build Settings and run in Editor or on device to validate read/write and auth.

Storage Sample Scene

Last updated