示例#1
0
    public void SaveScores()
    {
        List <Score> allScores = new List <Score> ();

        foreach (Level level in levels)
        {
            List <Score> levelScores = level.scores;
            foreach (Score score in levelScores)
            {
                allScores.Add(score);
            }
        }

        RawScoreData scoreData = new RawScoreData();

        scoreData.scores = allScores.ToArray();
        string jsonString = JsonUtility.ToJson(scoreData);
        string filePath   = GetFilePath();

        File.WriteAllText(filePath, jsonString);
    }
示例#2
0
    public void LoadScores()
    {
        string filePath = GetFilePath();

        if (File.Exists(filePath))
        {
            string       jsonString = File.ReadAllText(filePath);
            RawScoreData scoreData  = JsonUtility.FromJson <RawScoreData> (jsonString);

            foreach (Score score in scoreData.scores)
            {
                foreach (Level level in levels)
                {
                    if (level.id == score.level_id)
                    {
                        level.AddScore(score);
                    }
                }
            }
        }
        // else nothing because there is no scores yet!
    }