docs:api:fetchtable
FetchTable
public static IEnumerator FetchTable(SupabasePreset supabasePreset, string tableToFetch, System.Action<string> onComplete, bool mustBeLoggedIn = false)
Summary:
- Using Supabase, you can fetch information from supabase tables, to the Unity Instance.
- These should be whitelisted tables, defined in your commons edge function
- For example, we fetch data for the leaderboards using FetchTable
- This uses the 'Commons' Edge Function - Created during the supabase setup instructionals
- string url = supabasePreset.SupabaseURL + CommonEdgeFunctionURL;
Example:
[SerializeField] GameObject entryPrefab;
[SerializeField] SupabasePreset supabasepreset;
string leaderboardTable = "myleaderboard";
List<GameObject> scorerEntries = new List<GameObject>();
public void LoadLeaderboard()
{
StartCoroutine(
LivelyWebGL.Supabase.Tables.FetchTable(
supabasePreset, // the supabase preset so we can use our edge functions
leaderboardTable, // the table we will be fetching from
CreatePlayerEntries, // what happens after the data has been fetched
false // (optional) user doesn't have to be logged in (uses anon key as bearer instead of access token)
));
}
void CreatePlayerEntries(string result)
{
Debug.Log("Leaderboard Data: " + result);
foreach (GameObject entry in scorerEntries)
{
Destroy(entry);
}
scorerEntries.Clear();
if (result.StartsWith("ERROR"))
{
return;
}
// Each record (or row) is represented as a string array,
// data is returned as json, so we convert it to a multi-dimensional array: data[x][y]
// where x is the row, from top to bottom
// and y is the columns, from left to right
// [0][0] = wizard, [0][1] = 10] - playername / score
// [1][0] = beast, [1][1] = 100] - playername / score
// etc.
// don't include these rows when creating the multi-dimensional array
string[] ignoreList = { "id", "time" }; // Don't include these columns
string[][] allRows = LivelyWebGL.Supabase.Tables.ParseDynamicJson(result, ignoreList);
int sortIndex = 1;// sort the data by column index 1 (Scores)
allRows = LivelyWebGL.Supabase.Tables.SortByColumn(allRows, sortIndex, true);
for (int i = 0; i < allRows.Length; i++)
{
var row = allRows[i];
LeaderboardEntry leaderboardEntry = CreateEntry();
// Debug check so you can see what your arrays look like
// Debug.Log(string.Join(",", row));
leaderboardEntry.SetupEntry("#" + (i + 1), row[0], row[1], Color.white);
scorerEntries.Add(leaderboardEntry.gameObject);
}
}
LeaderboardEntry CreateEntry()
{
GameObject entry = Instantiate(entryPrefab);
entry.transform.SetParent(leaderboardContent);
entry.transform.localScale = Vector3.one;
LeaderboardEntry leaderboardEntry = entry.GetComponent<LeaderboardEntry>();
return leaderboardEntry;
}
docs/api/fetchtable.txt · Last modified: by admin
