Usage
This guide demonstrates how to utilize the DataService
within Unity to manage custom data. It covers saving, retrieving, and deleting data, as well as how to list all data keys.
Saving Custom Data
To save custom data, define your data type and create an instance with the data you wish to save. Then, use DataService
to save it.
using ByteCobra.Database.Client;
using UnityEngine;
// Define your custom data type
public class SampleData
{
public int Id { get; set; }
public string Name { get; set; }
}
public class DataExample : MonoBehaviour
{
public string BaseUrl = "http://localhost:21301/";
public string SecretKey;
public string SampleName = "Example Name";
public int SampleId = 1;
private async void Start()
{
DataService dataService = new DataService(BaseUrl, SecretKey);
// Create an instance of your data
SampleData data = new SampleData
{
Name = SampleName,
Id = SampleId
};
// Save your data
await dataService.SetDataAsync(data);
}
}
Retrieving Custom Data
Retrieve your previously saved data by specifying its type and, optionally, a unique key if it was set during saving.
SampleData retrievedData = await dataService.GetDataAsync<SampleData>();
Debug.Log($"Retrieved Data: Name - {retrievedData.Name}, ID - {retrievedData.Id}");
Deleting Data
To delete data associated with a specific key:
await dataService.DeleteDataAsync("YourUniqueKey");
Listing All Data Keys
To obtain a list of all keys representing stored data:
List<string> allKeys = await dataService.GetKeysAsync();
Debug.Log($"Keys: {string.Join(", ", allKeys)}");