I'm trying to save double by playerprefs.
not sure what I'm wrong. Help!
public double m_gold = 0.0;
private double m_goldPerTouch = 1.0;
private static string DoubleToString(double target){
return target.ToString ("R");
}
private static double StringToDouble(string target){
if (string.IsNullOrEmpty (target))
return 0d;
return double.Parse (target);
}
public static void SetDouble(string key, double value){
PlayerPrefs.SetString (key, DoubleToString (value));
}
public static double GetDouble(string key, double defaultValue){
string defaultVal = DoubleToString(defaultValue);
return StringToDouble(PlayerPrefs.GetString(key, defaultVal));
}
public static double GetDouble(string key){
return GetDouble (key, 0d);
}
void Awake(){
//PlayerPrefs.DeleteAll ();
m_gold = GetDouble ("Gold",m_gold);
m_goldPerTouch = GetDouble ("GoldPerTouch",m_goldPerTouch);
}
private static DataController instance;
public static DataController GetInstance(){
if (instance == null) {
instance = FindObjectOfType ();
if (instance == null) {
GameObject container = new GameObject ("DataController");
instance = container.AddComponent ();
}
}
return instance;
}
public void SetGold(double newGold){
m_gold = newGold;
SetDouble ("Gold", m_gold);
}
public void AddGold(double newGold){
m_gold += newGold;
SetGold (m_gold);
}
public void SubGold(double newGold){
m_gold -= newGold;
SetGold (m_gold);
}
public double GetGold(){
return m_gold;
}
public double GetGoldPerTouch(){
return m_goldPerTouch;
}
public void SetGoldPerTouch(double newGoldPerTouch){
m_goldPerTouch = newGoldPerTouch;
SetDouble("GoldPerTouch", m_goldPerTouch);
}
public void AddGoldPerTouch(double newGoldPerTouch){
m_goldPerTouch += newGoldPerTouch;
SetGoldPerTouch (m_goldPerTouch);
}
public void LoadUpgrade(Boosters boosters){
string key = boosters.boosterName;
boosters.level = PlayerPrefs.GetInt (key + "U_Level", 1);
boosters.gpt = GetDouble("GPC",boosters.gpt);
boosters.cost = GetDouble("U_Cost",boosters.cost);
}
public void SaveUpgrade(Boosters boosters){
string key = boosters.boosterName;
PlayerPrefs.SetInt (key + "U_Level", boosters.level);
SetDouble("GPC", boosters.gpt);
SetDouble("U_Cost", boosters.cost);
}
}
↧