Unity에서 DB 변화를 감지하는 기능을 구현해보자.
시작하기 앞서 일단 DB가 Realtime on 상태여야 Subscribe가 된다.
아래 Docs를 보고 코드를 작성해봤다.
using System;
using Supabase;
using UnityEngine;
using Client = Supabase.Client;
using System.Collections.Generic;
using Supabase.Realtime.PostgresChanges; // PostgresChangesOptions.ListenType.Inserts
namespace com.example
{
public class SupabasePost : MonoBehaviour //insert data
{
public SupabaseSettings SupabaseSettings = null!;
private Client client;
public async void Start()
{
var options = new SupabaseOptions
{
AutoConnectRealtime = true
};
var supabase = new Supabase.Client(SupabaseSettings.SupabaseURL, SupabaseSettings.SupabaseAnonKey, options);
await supabase.InitializeAsync();
#region DB Subscribe
await supabase.From<product>().On(PostgresChangesOptions.ListenType.Inserts, (sender, change) =>
{
Debug.Log(change.Model<product>().description);
});
#endregion
new WaitForSecondsRealtime(2f);
var model = new product()
{
id = 50,
name = "1",
model_code = "1",
stock = 1,
brand_id = 1,
price = 1000,
description = "new post Subscribe 1 ",
image_url = "http://hello"
};
await supabase.From<product>().Insert(model);
var model2 = new product()
{
id = 51,
name = "1",
model_code = "1",
stock = 1,
brand_id = 1,
price = 1000,
description = "new post Subscribe 2 ",
image_url = "http://hello"
};
await supabase.From<product>().Insert(model2);
}
}
}
Project Setting글은 이전글 참고.
SupaManager에 Supabase Post Script를 넣고 플레이 버튼을 눌러보자.
정상적으로 작동한 모습이다.