Download presentation
Presentation is loading. Please wait.
1
Keeping Score Richard Gesick
2
Objectives Create stats for the player
Implement those stats in our scripts Create a stat tracker for the stats Create an achievement system Use PlayerPrefs to save our stats Use GUI methods to show the stats and achievements Create/assign the stats
3
Possible Stats Kills Deaths Total gold Current gold Gold spent Level
Rounds won Rounds lost Kill-death ratio Win-lose ratio Time played Naming convention: pKills, pDeaths, etc Script necessary?
4
PlayerPrefs Allow storage of small amounts of info (strings, ints, floats) (about 1 MB total) Save data using: PlayerPrefs.SetFloat(“keyname”,variable name); Load data into game using: int pKills=PlayerPrefs.GetInt(“playerKills”);
5
StatTracker Script Methods to initialize, reset, update the stats during the game. Methods to set/reset PlayerPrefs for storage between games Display methods and later to read/write files
6
Setting Specific Stats
void SetStat(string stat, int intValue = 0) //see opt code { switch(stat) case "Kills": pKills+= intValue; float fKills = pKills; float fDeaths = pDeaths; if(fKills != 0) pKDR = fDeaths / fKills; break; case "Deaths": pDeaths+= intValue;
7
Resetting Stats void ResetStats() { pKills = 0; pDeaths = 0; … }
8
Resetting PlayerPref void ResetAllPrefs() { PlayerPrefs.SetInt("PlayerKills", 0); PlayerPrefs.SetInt("PlayerDeaths", 0); PlayerPrefs.SetInt("PlayerTotalGold", 0); … PlayerPrefs.Save(); }
9
PlayerPrefs.Save() From unity documentation:
Writes all modified preferences to disk. By default Unity writes preferences to disk during OnApplicationQuit(). In cases when the game crashes or otherwise prematurely exits, you might want to write the PlayerPrefs at sensible 'checkpoints' in your game. This function will write to disk potentially causing a small hiccup, therefore it is not recommended to call during actual gameplay. Note: There is no need to call this function manually inside OnApplicationQuit().
10
Saving the player prefs
void SaveAllPrefs() { PlayerPrefs.SetInt("PlayerKills", pKills); PlayerPrefs.SetInt("PlayerDeaths", pDeaths); PlayerPrefs.SetInt("PlayerTotalGold", pTotalGold); … PlayerPrefs.Save(); }
11
Displaying stats void OnGUI() { if(showStats) statsRect = GUI.Window(0, statsRect, StatsGUI, "Stats"); } void StatsGUI(int ID) GUILayout.BeginArea(new Rect(15, 25, 400, 400)); GUILayout.BeginVertical(); GUILayout.Label("Level - " + pLevel); GUILayout.Label("Gold - " + pCurrentGold); …
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.