Skip to content

The Postgres development platform. Supabase gives you a dedicated Postgres database to build your web, mobile, and AI applications.

2.9 KB
Raw Blame
import { MultipleCodeBlock } from 'ui-patterns/MultipleCodeBlock'

import type { StepContentProps } from '@/components/interfaces/ConnectSheet/Connect.types'

const ContentFile = ({ projectKeys }: StepContentProps) => {
  const supabaseUrl = projectKeys.apiUrl ?? 'your-project-url'
  const supabaseKey = projectKeys.publishableKey ?? projectKeys.anonKey ?? 'your-publishable-key'

  const files = [
    {
      name: 'SupabaseManager.cs',
      language: 'csharp',
      code: `
using System.Threading.Tasks;
using Supabase;
using UnityEngine;

// Keep a single client alive for the whole game and expose it through a
// MonoBehaviour singleton that survives scene loads.
public class SupabaseManager : MonoBehaviour
{
    public static SupabaseManager Instance { get; private set; }
    public Client Supabase { get; private set; }

    // Await this before using the client. Awake returns at the first await, so
    // other scripts could run before InitializeAsync() finishes without it.
    public Task InitializationTask { get; private set; }

    private const string Url = "${supabaseUrl}";
    private const string Key = "${supabaseKey}";

    private void Awake()
    {
        if (Instance != null)
        {
            Destroy(gameObject);
            return;
        }

        Instance = this;
        DontDestroyOnLoad(gameObject);
        InitializationTask = InitializeAsync();
    }

    private async Task InitializeAsync()
    {
        Supabase = new Client(Url, Key, new SupabaseOptions
        {
            AutoConnectRealtime = true
        });
        await Supabase.InitializeAsync();
    }
}
`,
    },
    {
      name: 'TodoList.cs',
      language: 'csharp',
      code: `
using System.Threading.Tasks;
using UnityEngine;

public class TodoList : MonoBehaviour
{
    // Start() can't be awaited, so kick off the async work from a Task method.
    private void Start()
    {
        _ = LoadTodosAsync();
    }

    private async Task LoadTodosAsync()
    {
        var manager = SupabaseManager.Instance;
        await manager.InitializationTask;

        var response = await manager.Supabase.From<Todo>().Get();
        foreach (var todo in response.Models)
        {
            Debug.Log(todo.Name);
        }
    }
}
`,
    },
    {
      name: 'Todo.cs',
      language: 'csharp',
      code: `
using Supabase.Postgrest.Attributes;
using Supabase.Postgrest.Models;

[Table("todos")]
public class Todo : BaseModel
{
    [PrimaryKey("id")]
    public long Id { get; set; }

    [Column("name")]
    public string Name { get; set; } = string.Empty;
}
`,
    },
  ]

  return (
    <div className="flex flex-col gap-y-4">
      <p className="text-sm text-foreground-light">
        Before running the sample, attach <code>SupabaseManager</code> to an active GameObject and
        attach <code>TodoList</code> to an active GameObject in the scene.
      </p>
      <MultipleCodeBlock files={files} />
    </div>
  )
}

// Used as a dynamic import
export default ContentFile