private void Start() { entryContainer = transform.Find("highscoreEntryContainer"); entryTemplate = entryContainer.Find("highscoreEntryTemplate"); entryTemplate.gameObject.SetActive(false); Highscores highscores = new Highscores(); highscores.highscoreEntryList = ScoreSaver.LoadScore(); Debug.Log(ScoreSaver.LoadScore().Count); // Sort entry list by Score for (int i = 0; i < highscores.highscoreEntryList.Count; i++) { for (int j = i + 1; j < highscores.highscoreEntryList.Count; j++) { if (highscores.highscoreEntryList[j].score > highscores.highscoreEntryList[i].score) { // Swap ScoreCointaner tmp = highscores.highscoreEntryList[i]; highscores.highscoreEntryList[i] = highscores.highscoreEntryList[j]; highscores.highscoreEntryList[j] = tmp; } } } highscoreEntryTransformList = new List <Transform>(); foreach (ScoreCointaner highscoreEntry in highscores.highscoreEntryList) { CreateHighscoreEntryTransform(highscoreEntry, entryContainer, highscoreEntryTransformList); } }
public static void SaveScore(Score score) { BinaryFormatter binaryFormatter = new BinaryFormatter(); string path = Application.persistentDataPath + "/score2.fun"; FileStream stream = new FileStream(path, FileMode.OpenOrCreate); ScoreCointaner cointaner = new ScoreCointaner(score); List <ScoreCointaner> ScoreTable; if (File.Exists(path)) { try { ScoreTable = binaryFormatter.Deserialize(stream) as List <ScoreCointaner>; Debug.Log("Po wczytaniu" + ScoreTable.Count); } catch (System.Exception e) { Debug.LogError("Not open"); ScoreTable = new List <ScoreCointaner>(); } bool exist = false; foreach (ScoreCointaner item in ScoreTable) { Debug.Log("Wejście do pentli"); if (item.Name == cointaner.Name) { Debug.Log("Znalazło"); exist = true; if (item.score < cointaner.score) { item.score = cointaner.score; } } } if (!exist) { Debug.Log("dodało"); ScoreTable.Add(cointaner); } } else { Debug.LogError("File Dont"); ScoreTable = new List <ScoreCointaner>(); ScoreTable.Add(cointaner); } binaryFormatter.Serialize(stream, ScoreTable); stream.Close(); }
private void AddHighscoreEntry(int score, string name) { // Create ScoreCointaner ScoreCointaner highscoreEntry = new ScoreCointaner(score, name); // Load saved Highscores string jsonString = PlayerPrefs.GetString("highscoreTable"); Highscores highscores = JsonUtility.FromJson <Highscores>(jsonString); if (highscores == null) { // There's no stored table, initialize highscores = new Highscores() { highscoreEntryList = new List <ScoreCointaner>() }; } // Add new entry to Highscores highscores.highscoreEntryList.Add(highscoreEntry); }
private void CreateHighscoreEntryTransform(ScoreCointaner highscoreEntry, Transform container, List <Transform> transformList) { float templateHeight = 31f; Transform entryTransform = Instantiate(entryTemplate, container); RectTransform entryRectTransform = entryTransform.GetComponent <RectTransform>(); entryRectTransform.anchoredPosition = new Vector2(0, -templateHeight * transformList.Count); entryTransform.gameObject.SetActive(true); int rank = transformList.Count + 1; string rankString; switch (rank) { default: rankString = rank + "TH"; break; case 1: rankString = "1ST"; break; case 2: rankString = "2ND"; break; case 3: rankString = "3RD"; break; } entryTransform.Find("posText").GetComponent <Text>().text = rankString; int score = highscoreEntry.score; entryTransform.Find("scoreText").GetComponent <Text>().text = score.ToString(); string name = highscoreEntry.Name; entryTransform.Find("nameText").GetComponent <Text>().text = name; // Set background visible odds and evens, easier to read entryTransform.Find("background").gameObject.SetActive(rank % 2 == 1); // Highlight First if (rank == 1) { entryTransform.Find("posText").GetComponent <Text>().color = Color.green; entryTransform.Find("scoreText").GetComponent <Text>().color = Color.green; entryTransform.Find("nameText").GetComponent <Text>().color = Color.green; } // Set tropy switch (rank) { default: entryTransform.Find("trophy").gameObject.SetActive(false); break; case 1: entryTransform.Find("trophy").GetComponent <Image>().color = UtilsClass.GetColorFromString("FFD200"); break; case 2: entryTransform.Find("trophy").GetComponent <Image>().color = UtilsClass.GetColorFromString("C6C6C6"); break; case 3: entryTransform.Find("trophy").GetComponent <Image>().color = UtilsClass.GetColorFromString("B76F56"); break; } transformList.Add(entryTransform); }