public void WriteDemoFile() { string validChars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"; ScoreEntries demoEntries = new ScoreEntries(); for (int i = 0; i < 10; i++) { // Zufälligen 3 char String erzeugen char[] nameChars = new char[3]; for (int j = 0; j < nameChars.Length; j++) { nameChars[j] = validChars[UnityEngine.Random.Range(0, validChars.Length)]; } string name = new string(nameChars); demoEntries.entryNo.Add(i + 1); demoEntries.value.Add(855 - i * 75); demoEntries.playerName.Add(name); } File.WriteAllText(scoreFilePath, demoEntries.GetString()); Debug.Log(scoreFilePath); }
private void PostNewScore() { ScoreEntries scoreList = ReadScoreFile(); int posOfNewScore = 0; int newScore = PlayerPrefs.GetInt("lastScore", 0); // Rank der neuen Score ermitteln: if (scoreList.value.Count < 10) { posOfNewScore = scoreList.value.Count; } else { for (int i = 0; i < scoreList.value.Count; i++) { if (newScore > scoreList.value[i]) { posOfNewScore = i; break; } } } // Platz 10 löschen, falls nötig: if (scoreList.entryNo.Count == 10) { scoreList.entryNo.RemoveAt(9); scoreList.value.RemoveAt(9); scoreList.playerName.RemoveAt(9); } // Neuen Eintrag machen: scoreList.entryNo.Insert(posOfNewScore, posOfNewScore + 1); scoreList.value.Insert(posOfNewScore, newScore); scoreList.playerName.Insert(posOfNewScore, playerNameText.text.ToUpper()); // Nachfolgende Positionsnummern um 1 erhöhen: for (int i = (posOfNewScore + 1); i < scoreList.value.Count; i++) { scoreList.entryNo[i] += 1; } // Datei überschreiben: File.WriteAllText(scoreFilePath, scoreList.GetString()); PlayerPrefs.SetInt("lastScore", 0); SceneManager.LoadScene("Scores"); }