Browse Source
- New commands SetInteger(), SetFloat(), SetBoolean(). - Simplified save game system.master
chrisgregan
11 years ago
16 changed files with 307 additions and 310 deletions
Binary file not shown.
@ -1,87 +0,0 @@
|
||||
using UnityEngine; |
||||
using System.Collections; |
||||
using System.Collections.Generic; |
||||
using System.Text.RegularExpressions; |
||||
|
||||
namespace Fungus |
||||
{ |
||||
/** |
||||
* Stores long or frequently repeated strings in a dictionary. |
||||
* Strings can then be retrieved using a short key string. |
||||
*/ |
||||
public class StringTable |
||||
{ |
||||
public Dictionary<string, string> stringDict = new Dictionary<string, string>(); |
||||
|
||||
/** |
||||
* Removes all strings from the string table. |
||||
*/ |
||||
public void ClearStringTable() |
||||
{ |
||||
stringDict.Clear(); |
||||
} |
||||
|
||||
/** |
||||
* Retrieves a string from the table by key. |
||||
*/ |
||||
public string GetString(string key) |
||||
{ |
||||
if (stringDict.ContainsKey(key)) |
||||
{ |
||||
return stringDict[key]; |
||||
} |
||||
return ""; |
||||
} |
||||
|
||||
/** |
||||
* Adds or updates a string in the table. |
||||
*/ |
||||
public void SetString(string key, string value) |
||||
{ |
||||
stringDict[key] = value; |
||||
} |
||||
|
||||
/** |
||||
* Replace keys in the input string with the string table entry. |
||||
* Example format: "This {string_key} string" |
||||
*/ |
||||
public string SubstituteStrings(string text) |
||||
{ |
||||
string subbedText = text; |
||||
|
||||
// Instantiate the regular expression object. |
||||
Regex r = new Regex("{.*?}"); |
||||
|
||||
// Match the regular expression pattern against a text string. |
||||
var results = r.Matches(text); |
||||
foreach (Match match in results) |
||||
{ |
||||
string stringKey = match.Value.Substring(1, match.Value.Length - 2); |
||||
string stringValue = GetString(stringKey); |
||||
|
||||
subbedText = subbedText.Replace(match.Value, stringValue); |
||||
} |
||||
|
||||
return subbedText; |
||||
} |
||||
|
||||
/** |
||||
* Chops a string at the first new line character encountered. |
||||
* This is useful for link / button strings that must fit on a single line. |
||||
*/ |
||||
public string FormatLinkText(string text) |
||||
{ |
||||
string trimmed; |
||||
if (text.Contains("\n")) |
||||
{ |
||||
trimmed = text.Substring(0, text.IndexOf("\n")); |
||||
} |
||||
else |
||||
{ |
||||
trimmed = text; |
||||
} |
||||
|
||||
return trimmed; |
||||
} |
||||
} |
||||
} |
@ -0,0 +1,175 @@
|
||||
using UnityEngine; |
||||
using System.Collections; |
||||
using System.Text.RegularExpressions; |
||||
|
||||
namespace Fungus |
||||
{ |
||||
/** |
||||
* Peristent data storage class for tracking game state. |
||||
* Provides a basic save game system. |
||||
*/ |
||||
public class Variables |
||||
{ |
||||
static string saveName = "_fungus"; |
||||
|
||||
/** |
||||
* Sets a name to prefix before all keys used with Set & Get methods. |
||||
* You can use this to support multiple save data profiles, (e.g. for multiple users or a list of checkpoints). |
||||
*/ |
||||
static public void SetSaveName(string _saveName) |
||||
{ |
||||
saveName = _saveName; |
||||
} |
||||
|
||||
/** |
||||
* Save the variable state to persistent storage. |
||||
* The currently loaded scene name is stored so that Game.LoadGame() will automatically move to the appropriate scene. |
||||
*/ |
||||
static public void Save() |
||||
{ |
||||
SetString("_scene", Application.loadedLevelName); |
||||
PlayerPrefs.Save(); |
||||
} |
||||
|
||||
/** |
||||
* Deletes all stored variables. |
||||
*/ |
||||
static public void DeleteAll() |
||||
{ |
||||
PlayerPrefs.DeleteAll(); |
||||
} |
||||
|
||||
/** |
||||
* Deletes a single stored variable. |
||||
*/ |
||||
static public void DeleteKey(string key) |
||||
{ |
||||
PlayerPrefs.DeleteKey(AddPrefix(key)); |
||||
} |
||||
|
||||
/** |
||||
* Returns the float variable associated with the key. |
||||
*/ |
||||
static public float GetFloat(string key) |
||||
{ |
||||
return PlayerPrefs.GetFloat(AddPrefix(key)); |
||||
} |
||||
|
||||
/** |
||||
* Returns the integer variable associated with the key. |
||||
*/ |
||||
static public int GetInteger(string key) |
||||
{ |
||||
return PlayerPrefs.GetInt(AddPrefix(key)); |
||||
} |
||||
|
||||
/** |
||||
* Returns the boolean variable associated with the key. |
||||
*/ |
||||
static public bool GetBoolean(string key) |
||||
{ |
||||
return (bool)(PlayerPrefs.GetInt(AddPrefix(key)) != 0); |
||||
} |
||||
|
||||
/** |
||||
* Returns the string variable associated with the key. |
||||
*/ |
||||
static public string GetString(string key) |
||||
{ |
||||
return PlayerPrefs.GetString(AddPrefix(key)); |
||||
} |
||||
|
||||
/** |
||||
* Returns true if a variable has been stored with this key. |
||||
*/ |
||||
static public bool HasKey(string key) |
||||
{ |
||||
return PlayerPrefs.HasKey(AddPrefix(key)); |
||||
} |
||||
|
||||
/** |
||||
* Stores a float variable using the key. |
||||
*/ |
||||
static public void SetFloat(string key, float value) |
||||
{ |
||||
PlayerPrefs.SetFloat(AddPrefix(key), value); |
||||
} |
||||
|
||||
/** |
||||
* Stores an integer variable using the key. |
||||
*/ |
||||
static public void SetInteger(string key, int value) |
||||
{ |
||||
PlayerPrefs.SetInt(AddPrefix(key), value); |
||||
} |
||||
|
||||
/** |
||||
* Stores a boolean variable using the key. |
||||
*/ |
||||
static public void SetBoolean(string key, bool value) |
||||
{ |
||||
PlayerPrefs.SetInt(AddPrefix(key), value ? 1 : 0); |
||||
} |
||||
|
||||
/** |
||||
* Stores a string variable using the key. |
||||
*/ |
||||
static public void SetString(string key, string value) |
||||
{ |
||||
PlayerPrefs.SetString(AddPrefix(key), value); |
||||
} |
||||
|
||||
/** |
||||
* Replace keys in the input string with the string table entry. |
||||
* Example format: "This {string_key} string" |
||||
*/ |
||||
static public string SubstituteStrings(string text) |
||||
{ |
||||
string subbedText = text; |
||||
|
||||
// Instantiate the regular expression object. |
||||
Regex r = new Regex("{.*?}"); |
||||
|
||||
// Match the regular expression pattern against a text string. |
||||
var results = r.Matches(text); |
||||
foreach (Match match in results) |
||||
{ |
||||
string stringKey = match.Value.Substring(1, match.Value.Length - 2); |
||||
string stringValue = GetString(stringKey); |
||||
|
||||
subbedText = subbedText.Replace(match.Value, stringValue); |
||||
} |
||||
|
||||
return subbedText; |
||||
} |
||||
|
||||
/** |
||||
* Chops a string at the first new line character encountered. |
||||
* This is useful for link / button strings that must fit on a single line. |
||||
*/ |
||||
static public string FormatLinkText(string text) |
||||
{ |
||||
string trimmed; |
||||
if (text.Contains("\n")) |
||||
{ |
||||
trimmed = text.Substring(0, text.IndexOf("\n")); |
||||
} |
||||
else |
||||
{ |
||||
trimmed = text; |
||||
} |
||||
|
||||
return trimmed; |
||||
} |
||||
|
||||
static string AddPrefix(string key) |
||||
{ |
||||
if (saveName.Length == 0) |
||||
{ |
||||
return key; |
||||
} |
||||
|
||||
return saveName + "." + key; |
||||
} |
||||
} |
||||
} |
@ -1,5 +1,5 @@
|
||||
fileFormatVersion: 2 |
||||
guid: 43c44334e66c44af1a071056e4fde1f9 |
||||
guid: 6552eb7d3f623479097c813cc27dcf92 |
||||
MonoImporter: |
||||
serializedVersion: 2 |
||||
defaultReferences: [] |
Binary file not shown.
Loading…
Reference in new issue