Exemplo n.º 1
0
        /// <summary>
        /// Saves the leaderboard store into the save file.
        /// Also checks to see if the leaderboard directory exsists in your games default save location
        /// </summary>
        /// <param name="_storedData">LeaderboardStore to save in the file</param>
        public static void SaveLeaderboardStore(LeaderboardStore _storedData)
        {
            string SavePath = Application.persistentDataPath + "/Leaderboard/leaderboardsavefile.lsf";

            if (File.Exists(SavePath))
            {
                BinaryFormatter Formatter = new BinaryFormatter();
                FileStream      Stream    = new FileStream(SavePath, FileMode.OpenOrCreate);
                Formatter.Serialize(Stream, _storedData);
                Stream.Close();
            }
            else
            {
                if (!Directory.Exists(Application.persistentDataPath + "/Leaderboard"))
                {
                    Directory.CreateDirectory(Application.persistentDataPath + "/Leaderboard");
                    BinaryFormatter Formatter = new BinaryFormatter();
                    FileStream      Stream    = new FileStream(SavePath, FileMode.OpenOrCreate);
                    Formatter.Serialize(Stream, _storedData);
                    Stream.Close();
                }
                else
                {
                    BinaryFormatter Formatter = new BinaryFormatter();
                    FileStream      Stream    = new FileStream(SavePath, FileMode.OpenOrCreate);
                    Formatter.Serialize(Stream, _storedData);
                    Stream.Close();
                }
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// Adds a new entry into the leaderboard and saves it.
        /// </summary>
        /// <param name="name">the player name to add with the new entry</param>
        /// <param name="score">the player score to add with the new entry</param>
        public static void AddToLeaderboard(string name, float score)
        {
            string SavePath = Application.persistentDataPath + "/Leaderboard/leaderboardsavefile.lsf";

            if (File.Exists(SavePath))
            {
                LeaderboardStore _storedData = LoadLeaderboardStore();
                _storedData.leaderboardData.Add(new LeaderboardData(name, score));
                BinaryFormatter Formatter = new BinaryFormatter();
                FileStream      Stream    = new FileStream(SavePath, FileMode.OpenOrCreate);
                Formatter.Serialize(Stream, _storedData);
                Stream.Close();
            }
            else
            {
                SaveLeaderboardStore(new LeaderboardStore());
                LeaderboardStore _storedData = new LeaderboardStore();
                _storedData.leaderboardData.Add(new LeaderboardData(name, score));
                BinaryFormatter Formatter = new BinaryFormatter();
                FileStream      Stream    = new FileStream(SavePath, FileMode.OpenOrCreate);
                Formatter.Serialize(Stream, _storedData);
                Stream.Close();
                Debug.LogWarning("* Leaderboard Manager * | Warning Code 1 | No file, creating one.");
            }
        }
Exemplo n.º 3
0
        /// <summary>
        /// Removes an entry from the leaderboard
        /// </summary>
        /// <param name="playerName">(String) the players name to check for</param>
        /// <param name="playerScore">(Float) the player score to check for</param>
        public static void RemoveEntryFromLeaderboard(string playerName, float playerScore)
        {
            LeaderboardStore _store = LoadLeaderboardStore();

            for (int i = 0; i < _store.leaderboardData.Count; i++)
            {
                if (_store.leaderboardData[i].playerName.Equals(playerName) && _store.leaderboardData[i].playerScore.Equals(playerScore))
                {
                    _store.leaderboardData.RemoveAt(i);
                    break;
                }
            }

            SaveLeaderboardStore(_store);
        }
Exemplo n.º 4
0
        /// <summary>
        /// Loads the saved LeaderboardData from the save file used for the leaderboard
        /// </summary>
        /// <returns></returns>
        public static LeaderboardData[] LoadLeaderboardData()
        {
            string SavePath = Application.persistentDataPath + "/Leaderboard/leaderboardsavefile.lsf";

            if (File.Exists(SavePath))
            {
                BinaryFormatter Formatter = new BinaryFormatter();
                FileStream      Stream    = new FileStream(SavePath, FileMode.Open);

                LeaderboardStore _data = Formatter.Deserialize(Stream) as LeaderboardStore;

                Stream.Close();

                return(_data.leaderboardData.ToArray());
            }
            else
            {
                Debug.LogError("* Leaderboard Manager * | Error Code 1 | Leaderboard save file not found!");
                return(null);
            }
        }