-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDataSaver
More file actions
58 lines (47 loc) · 1.29 KB
/
Copy pathDataSaver
File metadata and controls
58 lines (47 loc) · 1.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;
using Firebase.Database;
[Serializable]
public class dataToSave {
public string userName;
public int totalCoins;
public int crrLevel;
public int highScore;//and many more
}
public class DataSaver : MonoBehaviour
{
public dataToSave dts;
public string userId;
DatabaseReference dbRef;
private void Awake()
{
dbRef = FirebaseDatabase.DefaultInstance.RootReference;
}
public void SaveDataFn()
{
string json = JsonUtility.ToJson(dts);
dbRef.Child("users").Child(userId).SetRawJsonValueAsync(json);
}
public void LoadDataFn()
{
StartCoroutine(LoadDataEnum());
}
IEnumerator LoadDataEnum()
{
var serverData = dbRef.Child("users").Child(userId).GetValueAsync();
yield return new WaitUntil(predicate: () => serverData.IsCompleted);
print("process is complete");
DataSnapshot snapshot = serverData.Result;
string jsonData = snapshot.GetRawJsonValue();
if (jsonData != null)
{
print("server data found");
dts = JsonUtility.FromJson<dataToSave>(jsonData);
}
else {
print("no data found");
}
}
}