Это инициализация словаря
Код: Выделить всё
public class GiftRow : MonoBehaviour
{
private Dictionary usersCoinsTotal = new Dictionary();
private void Awake()
{
LoadFromFile();
}
Код: Выделить всё
public void Init(TikTokGift gift)
{
OnGiftUpdateCoins(gift.Sender.UniqueId, GetGiftValue(gift.Gift.Name));
Gift = gift;
Gift.OnAmountChanged += AmountChanged;
Gift.OnStreakFinished += StreakFinished;
txtUserName.text = $"{Gift.Sender.UniqueId} sent a {Gift.Gift.Name}!";
txtAmount.text = $"{Gift.Amount}x";
RequestImage(imgUserProfile, Gift.Sender.AvatarThumbnail);
RequestImage(imgGiftIcon, Gift.Gift.Image);
// Run Streak-End for non-streakable gifts
if (gift.StreakFinished)
StreakFinished(gift, gift.Amount);
}
Код: Выделить всё
private void OnGiftUpdateCoins(string username, int coins)
{
if (usersCoinsTotal.ContainsKey(username))
{
usersCoinsTotal[username] += coins;
Debug.Log($"{username}'s coin value updated to: {usersCoinsTotal[username]}\n");
}
else
{
usersCoinsTotal.Add(username, coins);
Debug.Log($"{username}'s coin value created as: {coins}\n");
}
ExportSaveObjectsToJson();
}
public int GetGiftValue(string giftName)
{
if (giftValues.ContainsKey(giftName))
{
return giftValues[giftName]; // Return the coin value if the gift exists
}
else
{
Debug.LogWarning("Gift not found: " + giftName); // Log a warning if the gift is not in the dictionary
return 0; // Return 0 if the gift doesn't exist
}
}
Код: Выделить всё
public void ExportSaveObjectsToJson()
{
// Convert the list of SaveObject to JSON string
string jsonString = JsonConvert.SerializeObject(usersCoinsTotal, Formatting.Indented);
// Define the path to save the JSON file
//string path = Application.persistentDataPath + "/saveObjects.json";
string path = "D:/saveObjects.json";
// Write the JSON to the file
File.WriteAllText(path, jsonString);
Debug.Log("Save objects exported to JSON file at: " + path);
}
Код: Выделить всё
private void OnApplicationQuit()
{
Debug.Log("Game quit");
//foreach (var i in usersCoinsTotal.Values)
//{
// Debug.Log(i);
//}
ExportSaveObjectsToJson();
foreach (var kvp in usersCoinsTotal)
{
Debug.Log($"{kvp.Key}: {kvp.Value}");
}
}

Проблема в том, что когда я запускаю игру, она успешно подключается к потоку, и игра показывает все подарки , однако, когда я закрываю игру, в словаре есть только одно значение (последний даритель, имя пользователя и отправленные им монеты, не общее количество добавленных монет, а только количество монет последнего подарка), то же самое и в json, только один даритель показан.
Вот весь json-файл, это последний подарок перед тем, как я закрыл игру:
Код: Выделить всё
{
"девочка1234590": 1
}
Подробнее здесь: https://stackoverflow.com/questions/789 ... ue-to-json