Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Gamebient · Nov 01, 2019 at 09:09 PM · savexmlsave datasettings

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

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

122 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges