private void Awake() { Instance = this; entryContainer = transform.Find("highscoreEntryContainer"); entryTemplate = entryContainer.Find("highscoreEntryTemplate"); entryTemplate.gameObject.SetActive(false); //PlayerPrefs.DeleteKey("highscoreTable"); string jsonString = PlayerPrefs.GetString("highscoreTable"); Highscores1 highscores1 = JsonUtility.FromJson <Highscores1>(jsonString); transform.Find("Reset").GetComponent <Button_UI>().ClickFunc = () => { highscores1 = null; PlayerPrefs.SetString("highscoreTable", ""); PlayerPrefs.Save(); // Reset Scores SceneManager.LoadScene(0); }; if (highscores1 == null) { // There's no stored table, initialize //Debug.Log("Initializing table with default values..."); AddHighscoreEntry1(30, "MINH"); AddHighscoreEntry1(40, "LINH"); // Reload jsonString = PlayerPrefs.GetString("highscoreTable"); highscores1 = JsonUtility.FromJson <Highscores1>(jsonString); } RefreshHighscoreTable1(); Hide(); }
private void RefreshHighscoreTable1() { string jsonString = PlayerPrefs.GetString("highscoreTable"); Highscores1 highscores1 = JsonUtility.FromJson <Highscores1>(jsonString); // Sort entry list by Score for (int i = 0; i < highscores1.highscoreEntryList1.Count; i++) { for (int j = i + 1; j < highscores1.highscoreEntryList1.Count; j++) { if (highscores1.highscoreEntryList1[j].score < highscores1.highscoreEntryList1[i].score) { // Swap HighscoreEntry1 tmp = highscores1.highscoreEntryList1[i]; highscores1.highscoreEntryList1[i] = highscores1.highscoreEntryList1[j]; highscores1.highscoreEntryList1[j] = tmp; } } } if (highscoreEntryTransformList1 != null) { foreach (Transform highscoreEntryTransform1 in highscoreEntryTransformList1) { Destroy(highscoreEntryTransform1.gameObject); } } highscoreEntryTransformList1 = new List <Transform>(); foreach (HighscoreEntry1 highscoreEntry1 in highscores1.highscoreEntryList1) { CreateHighscoreEntryTransform1(highscoreEntry1, entryContainer, highscoreEntryTransformList1); } }
public void AddHighscoreEntry1(int score, string name) { // Create HighscoreEntry HighscoreEntry1 highscoreEntry1 = new HighscoreEntry1 { score = score, name = name }; // Load saved Highscores string jsonString = PlayerPrefs.GetString("highscoreTable"); Highscores1 highscores = JsonUtility.FromJson <Highscores1>(jsonString); if (highscores == null) { // There's no stored table, initialize highscores = new Highscores1() { highscoreEntryList1 = new List <HighscoreEntry1>() }; } // Add new entry to Highscores highscores.highscoreEntryList1.Add(highscoreEntry1); // Save updated Highscores string json = JsonUtility.ToJson(highscores); PlayerPrefs.SetString("highscoreTable", json); PlayerPrefs.Save(); RefreshHighscoreTable1(); }