コード例 #1
0
    public void AddHighScoreEntry(int score, string name)
    {
        // check name length to only accept 3 letters, caps them all for the user
        if (name.Length > 3)
        {
            name = name.Trim();
            name = name.Substring(0, 3);
            name = name.ToUpper();
        }

        // create
        HighScoreEntry entry = new HighScoreEntry {
            score = score, name = name
        };

        // load
        string          jsonString = PlayerPrefs.GetString("highScoreTable");
        SavedHighscores highscores = JsonUtility.FromJson <SavedHighscores>(jsonString);

        // store
        highscores.highScoreEntries.Add(entry);

        for (int i = 0; i < highscores.highScoreEntries.Count; i++)
        {
            for (int j = i + 1; j < highscores.highScoreEntries.Count; j++)
            {
                if (highscores.highScoreEntries[j].score > highscores.highScoreEntries[i].score)
                {
                    HighScoreEntry tempEntry = highscores.highScoreEntries[i];
                    highscores.highScoreEntries[i] = highscores.highScoreEntries[j];
                    highscores.highScoreEntries[j] = tempEntry;
                }
            }
        }

        if (highscores.highScoreEntries.Count > 5)
        {
            highscores.highScoreEntries.RemoveAt(highscores.highScoreEntries.Count - 1);
        }

        // save
        string json = JsonUtility.ToJson(highscores);

        PlayerPrefs.SetString("highScoreTable", json);
        PlayerPrefs.Save();
    }
コード例 #2
0
    private void Awake()
    {
        entryContainer = transform.Find("highscoreEntryContainer");
        entryTemplate  = entryContainer.Find("highscoreEntryTemplate");

        entryTemplate.gameObject.SetActive(false);



        string          jsonString = PlayerPrefs.GetString("highScoreTable");
        SavedHighscores highscores = JsonUtility.FromJson <SavedHighscores>(jsonString);

        if (highscores == null)
        {
            highscores = new SavedHighscores();
            highscores.highScoreEntries = new List <HighScoreEntry>();

            string json = JsonUtility.ToJson(highscores);
            PlayerPrefs.SetString("highScoreTable", json);
            PlayerPrefs.Save();
        }

        // sort
        for (int i = 0; i < highscores.highScoreEntries.Count; i++)
        {
            for (int j = i + 1; j < highscores.highScoreEntries.Count; j++)
            {
                if (highscores.highScoreEntries[j].score > highscores.highScoreEntries[i].score)
                {
                    HighScoreEntry tempEntry = highscores.highScoreEntries[i];
                    highscores.highScoreEntries[i] = highscores.highScoreEntries[j];
                    highscores.highScoreEntries[j] = tempEntry;
                }
            }
        }

        highScoreEntryTransformList = new List <Transform>();

        for (int i = 0; i < highscores.highScoreEntries.Count; i++)
        {
            CreateHighScoreEntryTransform(highscores.highScoreEntries[i], entryContainer, highScoreEntryTransformList);
        }
    }
コード例 #3
0
    public bool CheckIfHighScore(int score)
    {
        // load
        string          jsonString = PlayerPrefs.GetString("highScoreTable");
        SavedHighscores highscores = JsonUtility.FromJson <SavedHighscores>(jsonString);

        if (highscores.highScoreEntries.Count < 5)
        {
            return(true);
        }

        for (int i = 0; i < highscores.highScoreEntries.Count; i++)
        {
            if (score > highscores.highScoreEntries[i].score)
            {
                return(true);
            }
        }
        return(false);
    }
コード例 #4
0
    private void Awake()
    {
        if (instance != null)
        {
            Destroy(gameObject);
            return;
        }
        instance = this;
        DontDestroyOnLoad(gameObject);

        string          jsonString = PlayerPrefs.GetString("highScoreTable");
        SavedHighscores highscores = JsonUtility.FromJson <SavedHighscores>(jsonString);

        if (highscores == null)
        {
            highscores = new SavedHighscores();
            highscores.highScoreEntries = new List <HighScoreEntry>();

            string json = JsonUtility.ToJson(highscores);
            PlayerPrefs.SetString("highScoreTable", json);
            PlayerPrefs.Save();
        }

        // sort
        for (int i = 0; i < highscores.highScoreEntries.Count; i++)
        {
            for (int j = i + 1; j < highscores.highScoreEntries.Count; j++)
            {
                if (highscores.highScoreEntries[j].score > highscores.highScoreEntries[i].score)
                {
                    HighScoreEntry tempEntry = highscores.highScoreEntries[i];
                    highscores.highScoreEntries[i] = highscores.highScoreEntries[j];
                    highscores.highScoreEntries[j] = tempEntry;
                }
            }
        }

        highScoreEntryTransformList = new List <Transform>();
    }