Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using System.Collections.Generic;
- using System.Xml.Serialization;
- using System.IO;
- using UnityEngine;
- public class SaveAndLoad : MonoBehaviour
- {
- public VariableManager varMan; //Script which stores the variables you wish to save.
- static string coinFilePath = ReturnPath(); //Path to the loacllay stored save data.
- //Called when the object is created.
- void Awake()
- {
- //Make object transferable through scenes.
- DontDestroyOnLoad(this.gameObject);
- //If there is locally saved data, load it.
- if (CheckForExistingFile () == true)
- {
- LoadCurrency ();
- }
- }
- //Returns the path to the locally saved data
- static string ReturnPath()
- {
- string coinFilePath = Path.Combine(Application.persistentDataPath, "coins.xml");
- return coinFilePath;
- }
- //Checks if loadable data is present.
- bool CheckForExistingFile()
- {
- if (!File.Exists (coinFilePath))
- {
- return false;
- }
- else
- {
- return true;
- }
- }
- // Loads the current currency amounts
- void LoadCurrency ()
- {
- var serializer = new XmlSerializer(typeof(int));
- var stream = new FileStream(coinFilePath, FileMode.Open);
- int container = (int)serializer.Deserialize(stream);
- stream.Close();
- varMan.coins = container;
- Debug.Log("coins: " + varMan.coins);
- }
- // Saves the current currency amounts
- public void SaveCurrency()
- {
- var serializer = new XmlSerializer(typeof(int));
- var stream = new FileStream(coinFilePath, FileMode.Create);
- serializer.Serialize(stream, varMan.coins);
- stream.Close();
- string text = stream.ToString ();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement