/// <summary> /// Updates the cooresponding property in PMData with the value of the command /// </summary> /// <param name="command">The command</param> /// <param name="pmData">The PMData object to update</param> public static void UpdatePMData(this ICommand command, PMData pmData) { if (pmData == null) { return; } if (command.Value == null) { return; } PropertyInfo?property = pmData.GetType().GetProperty(command.Name); if (property == null) { return; } if (Nullable.GetUnderlyingType(property.PropertyType) != command.Value.GetType()) { return; } property.SetValue(pmData, command.Value); }
//SAVING AND LOADING public void save(string stageName) { BinaryFormatter bf = new BinaryFormatter(); FileStream file = File.Create(Application.persistentDataPath + "/" + stageName + "Garden.dat"); PMData data = new PMData(); data.plants = new int[plantMax, saveNum]; for (int i = 0; i < plants.Length; i++) { for (int j = 0; j < saveNum; j++) { data.plants[i, j] = scripts[i].save()[j]; } } data.scoreNum = scoreNum; data.plantCount = plantCount; bf.Serialize(file, data); file.Close(); }
public void load(string stageName) { if (File.Exists(Application.persistentDataPath + "/" + stageName + "Garden.dat")) { BinaryFormatter bf = new BinaryFormatter(); FileStream file = File.Open(Application.persistentDataPath + "/" + stageName + "Garden.dat", FileMode.Open); PMData data = bf.Deserialize(file) as PMData; file.Close(); for (int i = 0; i < plants.Length; i++) { int[] stats = new int[saveNum]; for (int j = 0; j < saveNum; j++) { stats[j] = data.plants[i, j]; } scripts[i].load(stats); } scoreNum = data.scoreNum; plantCount = data.plantCount; } }