Beispiel #1
0
    public void CreateHighscores()
    {
        recentName = Recapitalize(nameField.text);
        Highscores     highscores           = JsonUtility.FromJson <Highscores>(PlayerPrefs.GetString("highscores"));
        HighscoreEntry recentHighscoreEntry = new HighscoreEntry {
            score = GameManager.Instance.score, name = recentName
        };

        highscores.highscores.Add(recentHighscoreEntry);
        List <HighscoreEntry> tmp = highscores.highscores.OrderByDescending(o => o.score).ToList();

        highscores.highscores = tmp;
        WriteNewHighscoresEntry(highscores);
        int totalEntries = 0;

        foreach (HighscoreEntry entry in highscores.highscores)
        {
            totalEntries++;
        }
        Debug.Log("total entries: " + totalEntries);
        Vector3 direction = bottomOfContainer.position - topOfContainer.position;

        for (int i = 0; i < visibleEntries; i++)
        {
            if (i < highscores.highscores.Count)
            {
                GameObject         entryGO   = Instantiate(entry, topOfContainer.position + (direction / visibleEntries) * i, transform.rotation, transform);
                HighscoreEntryData entryData = entryGO.GetComponent <HighscoreEntryData>();

                if (i == visibleEntries - 1)
                {
                    if (GameManager.Instance.score <= highscores.highscores[i].score)
                    {
                        entryData.SetName(recentName);
                        entryData.SetScore(GameManager.Instance.score);
                        entryData.SetPosition(tmp.IndexOf(recentHighscoreEntry) + 1);
                    }
                    else
                    {
                        entryData.SetName(highscores.highscores[i].name);
                        entryData.SetScore(highscores.highscores[i].score);
                        entryData.SetPosition(i + 1);
                        entries.Add(entryData);
                    }
                }
                else
                {
                    entryData.SetName(highscores.highscores[i].name);
                    entryData.SetScore(highscores.highscores[i].score);
                    entryData.SetPosition(i + 1);
                    entries.Add(entryData);
                }
            }
        }
    }
    public void AddScore(HighscoreEntryData entry)
    {
        TrimLists();
        highscoreListMain.highscores.Add(entry);
        highscoreListMain.highscores.Sort();
        lastAddedIndexMain = highscoreListMain.highscores.IndexOf(entry);
        if (lastAddedIndexMain != maxEntries)
        {
            TrimList(highscoreListMain.highscores);
        }

        highscoreListDaily.highscores.Add(entry);
        highscoreListDaily.highscores.Sort();
        lastAddedIndexDaily = highscoreListDaily.highscores.IndexOf(entry);
        if (lastAddedIndexDaily != maxEntries)
        {
            TrimList(highscoreListDaily.highscores);
        }
    }
 public void SetHighscoreData(HighscoreEntryData data)
 {
     PlayerText.text = data.PlayerName;
     ScoreText.text  = data.Score.ToString();
 }