You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
108 lines
4.0 KiB
108 lines
4.0 KiB
// This code is part of the Fungus library (http://fungusgames.com) maintained by Chris Gregan (http://twitter.com/gofungus). |
|
// It is released for free under the MIT open source license (https://github.com/snozbot/fungus/blob/master/LICENSE) |
|
|
|
using UnityEngine; |
|
|
|
namespace Fungus |
|
{ |
|
/// <summary> |
|
/// Save an Boolean, Integer, Float or String variable to persistent storage using a string key. |
|
/// The value can be loaded again later using the Load Variable command. You can also |
|
/// use the Set Save Profile command to manage separate save profiles for multiple players. |
|
/// </summary> |
|
[CommandInfo("Variable", |
|
"Save Variable", |
|
"Save an Boolean, Integer, Float or String variable to persistent storage using a string key. " + |
|
"The value can be loaded again later using the Load Variable command. You can also " + |
|
"use the Set Save Profile command to manage separate save profiles for multiple players.")] |
|
[AddComponentMenu("")] |
|
public class SaveVariable : Command |
|
{ |
|
[Tooltip("Name of the saved value. Supports variable substition e.g. \"player_{$PlayerNumber}")] |
|
[SerializeField] protected string key = ""; |
|
|
|
[Tooltip("Variable to read the value from. Only Boolean, Integer, Float and String are supported.")] |
|
[VariableProperty(typeof(BooleanVariable), |
|
typeof(IntegerVariable), |
|
typeof(FloatVariable), |
|
typeof(StringVariable))] |
|
[SerializeField] protected Variable variable; |
|
|
|
#region Public members |
|
|
|
public override void OnEnter() |
|
{ |
|
if (key == "" || |
|
variable == null) |
|
{ |
|
Continue(); |
|
return; |
|
} |
|
|
|
var flowchart = GetFlowchart(); |
|
|
|
// Prepend the current save profile (if any) |
|
string prefsKey = SetSaveProfile.SaveProfile + "_" + flowchart.SubstituteVariables(key); |
|
|
|
System.Type variableType = variable.GetType(); |
|
|
|
if (variableType == typeof(BooleanVariable)) |
|
{ |
|
BooleanVariable booleanVariable = variable as BooleanVariable; |
|
if (booleanVariable != null) |
|
{ |
|
// PlayerPrefs does not have bool accessors, so just use int |
|
PlayerPrefs.SetInt(prefsKey, booleanVariable.Value ? 1 : 0); |
|
} |
|
} |
|
else if (variableType == typeof(IntegerVariable)) |
|
{ |
|
IntegerVariable integerVariable = variable as IntegerVariable; |
|
if (integerVariable != null) |
|
{ |
|
PlayerPrefs.SetInt(prefsKey, integerVariable.Value); |
|
} |
|
} |
|
else if (variableType == typeof(FloatVariable)) |
|
{ |
|
FloatVariable floatVariable = variable as FloatVariable; |
|
if (floatVariable != null) |
|
{ |
|
PlayerPrefs.SetFloat(prefsKey, floatVariable.Value); |
|
} |
|
} |
|
else if (variableType == typeof(StringVariable)) |
|
{ |
|
StringVariable stringVariable = variable as StringVariable; |
|
if (stringVariable != null) |
|
{ |
|
PlayerPrefs.SetString(prefsKey, stringVariable.Value); |
|
} |
|
} |
|
|
|
Continue(); |
|
} |
|
|
|
public override string GetSummary() |
|
{ |
|
if (key.Length == 0) |
|
{ |
|
return "Error: No stored value key selected"; |
|
} |
|
|
|
if (variable == null) |
|
{ |
|
return "Error: No variable selected"; |
|
} |
|
|
|
return variable.Key + " into '" + key + "'"; |
|
} |
|
|
|
public override Color GetButtonColor() |
|
{ |
|
return new Color32(235, 191, 217, 255); |
|
} |
|
|
|
#endregion |
|
} |
|
} |