Пример #1
0
        //saves player progress at the end of each level and loads next level
        public PlayerProgress SaveAndLoadPlayerProgress(string currentLevelKubicode = "", string nextLevelKubiCode = "", bool isGolden = false)
        {
            string folder    = Application.persistentDataPath + "/UserSaves";
            string levelFile = "PlayerProgress";
            string path      = Path.Combine(folder, levelFile) + ".json";

            if (!Directory.Exists(folder))
            {
                Directory.CreateDirectory(folder);
            }

            if (File.Exists(path))
            {
                string         json = File.ReadAllText(path);
                PlayerProgress loadedPlayerProgress = JsonUtility.FromJson <PlayerProgress>(json);

                //checks if you're saving or loading the level
                if (currentLevelKubicode != "" && nextLevelKubiCode != "" && isGolden != false)
                {
                    loadedPlayerProgress.nextLevelKubicode = nextLevelKubiCode;
                    if (!loadedPlayerProgress.beatenLevels.Contains(currentLevelKubicode))
                    {
                        loadedPlayerProgress.beatenLevels.Add(currentLevelKubicode);
                    }
                    if (isGolden && !loadedPlayerProgress.goldenLevels.Contains(currentLevelKubicode))
                    {
                        loadedPlayerProgress.goldenLevels.Add(currentLevelKubicode);
                    }
                }

                string newJson = JsonUtility.ToJson(loadedPlayerProgress);
                File.WriteAllText(path, newJson);

                return(loadedPlayerProgress);
            }

            // if the file doesn't exist, create it
            else
            {
                PlayerProgress newPlayerProgress = new PlayerProgress();

                newPlayerProgress.nextLevelKubicode = "Worl101";

                string json = JsonUtility.ToJson(newPlayerProgress);

                if (File.Exists(path))
                {
                    File.Delete(path);
                }

                File.WriteAllText(path, json);

                return(newPlayerProgress);
            }
        }
Пример #2
0
 private PlayerProgress CreatePlayerProgressData()
 {
     playerProgress = new PlayerProgress();
     return(playerProgress);
 }