/** * Saves an object to state storage. * * @param compression If enabled object will be compressed before it is saved, and decompressed on read */ public static void SaveData(string key, object source, bool compressed = false) { if (source == null) { Trace.LogWarning("Warning, tried to save null data to key: " + key + "."); return; } // check if this is an dataObject, if so use our built in serializer StringWriter sw; if (source is DataObject) { // use our own serializer DataObject data = (source as DataObject); sw = new StringWriter(); var node = data.CreateNode(); node.SetAttributeValue("Version", data.ClassAttributes.Version.ToString("0.0")); sw.Write(node); } else if (source is XElement) { /// write an XML file sw = new StringWriter(); sw.Write((source as XElement).ToString()); } else { throw new Exception("Can only save objects of type XML or DataObject"); } // apply compression if (compressed) { var oldStringWriter = sw; sw = new StringWriter(); sw.Write("<" + COMPRESSED_TAG + ">"); sw.Write(Compressor.Compress(oldStringWriter.ToString())); sw.Write("</" + COMPRESSED_TAG + ">"); } switch (SaveDataLocation) { case StorageLocation.PlayerPrefs: PlayerPrefs.SetString(key, sw.ToString()); break; case StorageLocation.PersistentFolder: SaveXML(getPercistentURLFromKey(key), sw.ToString()); break; } }