- Home /
How To Make Settings File Over XML?
There are a lot of Games out there using easy .ini, .xml or any other file of that type for saving settings. They are easy to read and easy to modify. I tried hard to do something like that on my own but could't do it the right way. The way I do it now, every single bool/ int/ float and so on has to be called seperately. It doesn't only makes the file larger, but more complex and more time consuming when writing on it too. You don't have to read the script completely, there aren't any misstakes, I just want to know how to simplify that. I tried it with two scripts as follows: The XML Saving and loading Script;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.Xml;
using System.Xml.Serialization;
using System.IO;
using UnityEditor;
public class XMLManagerSettings : MonoBehaviour
{
public static XMLManagerSettings ins;
public bool settingsLoaded = false;
public void Awake()
{
ins = this;
if (Directory.Exists(Application.dataPath + "/StreamingAssets/Xml Files") == false
&& Application.platform != RuntimePlatform.Android)
{
Directory.CreateDirectory(Application.dataPath + "/StreamingAssets/Xml Files");
}
else if (Directory.Exists(Application.persistentDataPath + "/Xml Files") == false)
{
Directory.CreateDirectory(Application.persistentDataPath + "/Xml Files");
}
if (File.Exists(Application.dataPath + "/StreamingAssets/Xml Files/settings.xml") == false
&& File.Exists(Application.persistentDataPath + "/Xml Files/settings.xml") == false)
{
Debug.Log("New File Created");
settingDB.SettingsSelf.healthIsText = settingDB.DefautSettings.healthIsText;
SaveSettings();
LoadSettings();
}
settingsLoaded = true;
}
public SettingDatabase settingDB;
//save function
public void SaveSettings()
{
//open new xml file
XmlSerializer serializer = new XmlSerializer(typeof(SettingDatabase));
if (Application.platform == RuntimePlatform.Android)
{
FileStream stream = new FileStream(Application.persistentDataPath + "/Xml Files/settings.xml", FileMode.Create);
serializer.Serialize(stream, settingDB);
stream.Close();
}
else
{
FileStream stream = new FileStream(Application.dataPath + "/StreamingAssets/Xml Files/settings.xml", FileMode.Create);
serializer.Serialize(stream, settingDB);
stream.Close();
}
Debug.Log("Settings Saved");
}
//load function
public void LoadSettings()
{
XmlSerializer serializer = new XmlSerializer(typeof(SettingDatabase));
if (Application.platform == RuntimePlatform.Android)
{
FileStream stream = new FileStream(Application.persistentDataPath + "/Xml Files/settings.xml", FileMode.Open);
settingDB = serializer.Deserialize(stream) as SettingDatabase;
stream.Close();
}
else
{
FileStream stream = new FileStream(Application.dataPath + "/StreamingAssets/Xml Files/settings.xml", FileMode.Open);
settingDB = serializer.Deserialize(stream) as SettingDatabase;
stream.Close();
}
Debug.Log("Settings Loaded");
}
}
[System.Serializable]
public class SettingOptions
{
public bool healthIsText;
}
[System.Serializable]
public class DefautSettings
{
public bool healthIsText;
}
[System.Serializable]
public class SettingDatabase
{
[XmlElement("Settings_Self")]
public SettingOptions SettingsSelf;
public DefautSettings DefautSettings;
}
And the Script that checked whether someting changend or not
using System.Collections;
using System.Collections.Generic;
using System.IO;
using UnityEngine;
using UnityEngine.UI;
using System.Xml;
using System.Xml.Serialization;
public class SettingsMenuHandler : MonoBehaviour
{
public GameObject settingScreen;
public bool healthIsText;
public bool soudeEnabled;
public Toggle healthToggle;
public Toggle soundToggle;
private bool savedChange;
public DefautSettings defSet;
public XMLManagerSettings xmlManager;
public GameObject changedInfo;
public PreloadSettings preloadSet;
void Awake()
{
savedChange = true;
changedInfo.SetActive(true);
}
public void Update()
{
//called when Settings were loaded
if (xmlManager.settingsLoaded == true)
{
xmlManager.settingsLoaded = false;
xmlManager.LoadSettings();
healthToggle.isOn = xmlManager.settingDB.SettingsSelf.healthIsText;
}
healthIsText = healthToggle.isOn;
if (Input.GetKeyDown(KeyCode.Y))
{
XMLManagerSettings.ins.SaveSettings();
}
if (healthIsText != xmlManager.settingDB.SettingsSelf.healthIsText)
{
savedChange = false;
}
else
{
savedChange = true;
}
if (savedChange == false)
{
changedInfo.GetComponent<Text>().text = "There are unapplied settings! Please Press 'Apply'";
changedInfo.SetActive(true);
}
else if(savedChange == true)
{
changedInfo.SetActive(false);
}
}
public void RestoreButton()
{
xmlManager.settingDB.SettingsSelf.healthIsText = defSet.healthIsText;
healthToggle.isOn = defSet.healthIsText;
XMLManagerSettings.ins.SaveSettings();
XMLManagerSettings.ins.LoadSettings();
}
public void ApplyButton()
{
xmlManager.settingDB.SettingsSelf.healthIsText = healthIsText;
XMLManagerSettings.ins.SaveSettings();
XMLManagerSettings.ins.LoadSettings();
savedChange = true;
}
}
public class PreloadSettings
{
public bool healthIsText;
}
As I already said it is way too complicated, so I can not leave it like that. I've seen a few tutorials, but all of them were about creating lists, storing multiple datatypes such as for items that have all the same structure. Settings doesn't - at least mine.
Does someone has suggestions on how to write it easier? I already tried to set two classes equal to each other, but as I thought it didn't work.
Thanks in advance
Answer by widowert · Jul 22, 2020 at 08:14 PM
@JoshuasGameDev You can use static variables. First when you start your application extract the data from the file and save it in you static variables then if something is changed you can do 2 things:
Keep the changed values in the static variables and only save it when it is going to close.
Go saving every change that happens and save it in the statics variables too.
(static variables to have the same value along the scenes in the same script)
Hope it helps you.
Your answer
Follow this Question
Related Questions
options menu UI doesnt save when switching scenes 1 Answer
How to save options menu? 1 Answer
Save entire Transform objects with childs 3 Answers
Saving Melee Combat Template Pack (MY LAST PROBLEM) 1 Answer
Save Game Sceenes user quit aplication, Can I save the scene as a whole while leaving the game? 0 Answers