spacetimedb-unity

Integrate SpacetimeDB with Unity clients for real-time game state synchronization.

25.0k|1.0k|Updated Jun 17, 2023
One-click install
npx skills add https://github.com/clockworklabs/SpacetimeDB --skill spacetimedb-unity
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: spacetimedb-unity
Source: https://github.com/clockworklabs/SpacetimeDB/tree/main/skills/spacetimedb-unity
Command: npx skills add https://github.com/clockworklabs/SpacetimeDB --skill spacetimedb-unity

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve?

This Skill provides Unity-specific patterns and lifecycle guidance to correctly connect, maintain, and use SpacetimeDB in Unity projects, preventing common issues like missed callbacks, thread-safety violations, lost connections on scene load, and token loss.

Core Features & Use Cases

  • Connection lifecycle management: Use a SpacetimeManager MonoBehaviour singleton with DontDestroyOnLoad, Awake/Start/OnDestroy hooks, and proper OnConnect/OnDisconnect handling.
  • Frame-driven networking: Ensure Connection.FrameTick() is invoked from Update() on the main thread so queued network messages are processed and callbacks fire.
  • Subscriptions, reducers, and client cache: Generate C# module bindings, subscribe to tables, register OnInsert/OnUpdate/OnDelete handlers, call reducers from UI, and persist auth tokens with PlayerPrefs for seamless reconnects.
  • Use Case: Real-time multiplayer game state synchronization and chat where deterministic main-thread processing and token persistence are required.

Quick Start

Create a SpacetimeManager MonoBehaviour that builds a DbConnection with WithUri and WithDatabaseName, persists the auth token to PlayerPrefs in OnConnected, and calls Connection.FrameTick() from Update().

Frequently Asked Questions about spacetimedb-unity

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
How do I integrate SpacetimeDB with Unity for real-time game state synchronization?

To integrate SpacetimeDB with Unity, create a SpacetimeManager MonoBehaviour singleton using DontDestroyOnLoad. Build a DbConnection, call Connection.FrameTick() from Update() to process queued network messages, and subscribe to tables for real-time state updates.

Why are my SpacetimeDB Unity callbacks not firing or causing thread-safety violations?

SpacetimeDB callbacks fail or cause thread-safety violations if Connection.FrameTick() is not called from the main thread. Invoke FrameTick() inside the MonoBehaviour Update() loop to ensure queued network messages are processed correctly on the main thread.

How do I persist SpacetimeDB auth tokens in Unity between sessions?

Persist SpacetimeDB auth tokens in Unity using PlayerPrefs. In your OnConnected callback, save the token to PlayerPrefs to enable seamless reconnects and prevent token loss across game sessions without requiring external storage solutions.

Does SpacetimeDB work with Unity 2022.3 and MonoBehaviour lifecycle events?

Yes, SpacetimeDB works with Unity 2022.3+ projects using the MonoBehaviour lifecycle. The SDK integrates with Awake, Start, Update, and OnDestroy hooks, and is installed via Unity Package Manager to ensure proper connection lifecycle management.

What is the best way to handle SpacetimeDB connection loss on Unity scene loads?

To prevent lost connections on scene loads, use a SpacetimeManager MonoBehaviour singleton with DontDestroyOnLoad. This ensures the DbConnection persists across scene transitions, maintaining active subscriptions and avoiding unexpected disconnects during gameplay.

How do I call SpacetimeDB reducers and handle table updates from Unity UI?

Generate C# module bindings for your SpacetimeDB tables, register OnInsert, OnUpdate, and OnDelete handlers for subscribed tables, and invoke reducers directly from Unity UI events. The FrameTick loop ensures these client cache updates process on the main thread.