public async void SaveData()
{
string trashJson = null;
string playerJson = null;
this.PlayerDataSaved = false; // 저장 시작 전에 false로 설정
while (!this.PlayerDataSaved)
{
await Task.Yield(); // 플레이어 데이터가 저장될 때까지 대기
}
await Task.Run(() =>
{
trashJson = JsonConvert.SerializeObject(dicTrash); // 딕셔너리를 JSON으로 직렬화
playerJson = JsonConvert.SerializeObject(playerData); // 플레이어 데이터를 JSON으로 직렬화
});
await File.WriteAllTextAsync("./Assets/Resources/TrashMapData.json", trashJson);
await File.WriteAllTextAsync("./Assets/Resources/PlayerData.json", playerJson);
Debug.Log("저장됨");
}
이건 버그가 난다
가끔 저 중간에 while문을 탈출하지 못하고 무한루프가 돌아서 저장이 되지않는다.
player쪽에서 Application quit에서 DataSaved라는 저 bool값을 true로 바꾸는데, 바꾸자마자 저기서 false로 다시 바꿔서 무한루프를 돈다
public async void SaveData()
{
string trashJson = null;
string playerJson = null;
while (!this.PlayerDataSaved)
{
await Task.Yield(); // 플레이어 데이터가 저장될 때까지 대기
}
await Task.Run(() =>
{
trashJson = JsonConvert.SerializeObject(dicTrash); // 딕셔너리를 JSON으로 직렬화
playerJson = JsonConvert.SerializeObject(playerData); // 플레이어 데이터를 JSON으로 직렬화
});
await File.WriteAllTextAsync("./Assets/Resources/TrashMapData.json", trashJson);
await File.WriteAllTextAsync("./Assets/Resources/PlayerData.json", playerJson);
Debug.Log("저장됨");
this.PlayerDataSaved = false; // 저장이 끝났으니 다시 false로 설정
}
false로 바꾸는 구간을 저장 이후로 변경하면 잘 된다.