/// <summary> /// Draws the persistence fold out. /// </summary> virtual protected void DrawPersistenceFoldOut() { Persistable myTarget = (Persistable)target; // TODO Add more options to allow users to edit these EditorGUILayout.BeginHorizontal(); // GUILayout.Button ("Edit"); if (GUILayout.Button("Reset")) { myTarget.ResetSaveData(); } // GUILayout.Button ("Load..."); EditorGUILayout.EndHorizontal(); EditorGUILayout.LabelField("Loading", EditorStyles.boldLabel); UpdateBool(ref myTarget.loadOnAwake, "Load on Awake"); UpdateBool(ref myTarget.loadOnStart, "Load on Start"); UpdateBool(ref myTarget.loadOnCharacterLoad, "Load on Character Load"); EditorGUILayout.LabelField("Saving", EditorStyles.boldLabel); UpdateBool(ref myTarget.saveOnDeath, "Save on Death"); UpdateBool(ref myTarget.saveOnGameOver, "Save on Game Over"); UpdateBool(ref myTarget.saveOnSceneExit, "Save on Scene Exit"); UpdateBool(ref myTarget.saveOnChange, "Save on Change"); EditorGUILayout.LabelField("Resetting", EditorStyles.boldLabel); UpdateBool(ref myTarget.resetOnDeath, "Reset on Death"); UpdateBool(ref myTarget.resetOnGameOver, "Reset on Game Over"); UpdateBool(ref myTarget.resetOnSceneExit, "Reset on Scene Exit"); }
/// <summary> /// Load the given persistable. /// </summary> /// <param name="p">Persistable to laod.</param> public static void Load(Persistable p) { object savedObject = LoadSavedData(p); if (savedObject != null) { p.ApplySaveData(savedObject); } else { p.ResetSaveData(); } }
/// <summary> /// Does the actual load and returns raw object. /// </summary> /// <returns>The saved data.</returns> /// <param name="p">P.</param> protected static object LoadSavedData(Persistable p) { string data = PlayerPrefs.GetString(BasePlayerPrefId + p.Identifier, ""); if (data.Length > 0) { using (StringReader reader = new StringReader(data)){ XmlSerializer serializer = new XmlSerializer(p.SavedObjectType()); object savedObject = serializer.Deserialize(reader); return(savedObject); } } return(null); }
/// <summary> /// Load the given persistable. /// </summary> /// <param name="p">Persistable to laod.</param> public static void Load(Persistable p) { string data = PlayerPrefs.GetString(BasePlayerPrefId + p.Identifier, ""); if (data.Length > 0) { using (StringReader reader = new StringReader(data)){ XmlSerializer serializer = new XmlSerializer(p.SavedObjectType()); object savedObject = serializer.Deserialize(reader); p.ApplySaveData(savedObject); } } else { p.ResetSaveData(); } }
/// <summary> /// Save the given persistable. /// </summary> /// <param name="p">Persistable to save.</param> public static void Save(Persistable p) { object saveData = p.SaveData; // We assume all saveData is annotated with [Serializable] we could add a condition for ISeriazable too. if (saveData.GetType().IsSerializable) { using (StringWriter writer = new StringWriter()) { XmlSerializer serializer = new XmlSerializer(saveData.GetType()); serializer.Serialize(writer, saveData); PlayerPrefs.SetString(BasePlayerPrefId + p.Identifier, writer.ToString()); } } else { Debug.LogError("Save data for " + p + " is not serializable."); } }
/// <summary> /// Resets the given persistable. /// </summary> /// <param name="p">Persistable to reset.</param> public static void Reset(Persistable p) { PlayerPrefs.DeleteKey(BasePlayerPrefId + p.Identifier); p.ResetSaveData(); }