// Use this for initialization
    void Start()
    {
        // Checks if a score file exist
        if (File.Exists(Application.dataPath + "/score.txt"))
        {
            // Loads the score list if file exist
            string      scoreString = File.ReadAllText(Application.dataPath + "/score.txt");
            JsonWrapper wrapper     = JsonUtility.FromJson <JsonWrapper>(scoreString);
            m_listOfScores   = wrapper.scoreList;
            minimumThreshold = m_listOfScores[m_listOfScores.Count - 1].score;
        }
        else
        {
            // Creates a new score list if file doesn't exist
            for (int i = 0; i < maxAmount; i++)
            {
                ScoreData newData = new ScoreData();
                m_listOfScores.Add(newData);
            }
            m_listOfScores.TrimExcess();
        }

        m_scorePrefab.gameObject.SetActive(false);

        int iteration = 0;

        foreach (ScoreData scoreData in m_listOfScores)
        {
            ScoreEntryScript scoreEntry = Instantiate(m_scorePrefab, m_highscoreContainer.transform);
            RectTransform    rectTrans  = scoreEntry.transform.GetComponent <RectTransform>();
            rectTrans.anchoredPosition = new Vector2(0, -templateHeight * iteration);
            rectTrans.gameObject.SetActive(true);

            // Set the scores from list into UI
            scoreEntry.nameText.text  = scoreData.name;
            scoreEntry.scoreText.text = scoreData.score.ToString();
            m_scoreUIList.Add(scoreEntry);
            iteration++;
        }
    }
Example #2
0
    public void CreateEntries(Score[] scores)
    {
        foreach (Transform child in entryParent.transform)
        {
            Destroy(child.gameObject);
        }

        Array.Sort(scores, new ScoreComparer());

        for (int i = 0; i < scores.Length; i++)
        {
            Score score = scores[i];

            GameObject entry = Instantiate(entryPrefab, new Vector3(0f, 0f, 0f), Quaternion.identity);
            entry.transform.SetParent(entryParent.transform);

            ScoreEntryScript entryScript = entry.GetComponent <ScoreEntryScript>();
            entryScript.username = score.name;
            entryScript.score    = score.score;
        }

        entryParent.GetComponent <RectTransform>().sizeDelta = new Vector2(0f, (scores.Length - 1) * 50);
    }