- Home /
How can I use XML serializer to save this data.,How can I save this for multiple players?
How can I save multiple versions of the "completedTasks" list for each of my pawns into the "SaveData" class?
Here is the script containing the "completedTasks" list
using System;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
//This is for the player option screen that pop-ups when u select a player
public class PlayerOptions : MonoBehaviour
{
public static PlayerOptions playerOptions;
public Button forwardButton, backButton, markDoneButton;
public Text playerName;
private bool clicked;
int i = 0;
void Start()
{
playerOptions = this;
forwardButton.onClick.AddListener(onForwardClick);
backButton.onClick.AddListener(onBackClick);
markDoneButton.onClick.AddListener(onMarkDoneClick);
playerName.text = PawnNameStorage.instance.StoredNames[0];
}
//Draws all the lines.
//Put all the line rendering here
void Update()
{
RenderLines.clearLines();
Player selected = LiiikkumisScript.getPlayer(playerName.text);
if (selected == null)
{
return;
}
//Render done tasks with lime
foreach (int position in selected.completedTasks)
{
RenderLines.drawLine(GameObject.Find("Cube (" + (position + 2) + ")"), new Color(0, 200, 0), 0.3f, 0);
}
//Render current position with red. Also if theres a task rendered in the same spot then it renders it so they dont collide
float minusWidth = 0;
if (selected.completedTasks.Contains(selected.position))
{
minusWidth = 0.4f;
}
RenderLines.drawLine(GameObject.Find("Cube (" + (selected.position + 2) + ")"), new Color(200, 0, 0), 0.3f, minusWidth);
//Draw orange thing to the point where the mouse is at and move selected player to it if mouse is clicked
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
if (Physics.Raycast(ray, out hit))
{
if (hit.transform.gameObject.name.StartsWith("Cube ("))
{
int position = Int32.Parse(hit.transform.gameObject.name.Replace("Cube (", "").Replace(")", "")) - 2;
float minusWidth2 = 0;
if (selected.completedTasks.Contains(position))
{
minusWidth2 = 0.4f;
}
if (selected.position != position) RenderLines.drawLine(hit.transform.gameObject, new Color(0, 0, 200), 0.3f, minusWidth2);
//Move selected player to the point of the mouse if clicked
if (Input.GetMouseButton(0) && !clicked)
{
clicked = true;
GameObject gameObject = LiiikkumisScript.getPlayerObject(selected.name);
LiiikkumisScript.liiikkumisScript.Positions[selected.position].GetComponent<Stacker>().RemoveObject(gameObject);
selected.position = position;
LiiikkumisScript.liiikkumisScript.Positions[selected.position].GetComponent<Stacker>().AddObject(gameObject);
}
else if (!Input.GetMouseButton(0))
{
clicked = false;
}
}
}
}
//Sets the currently selected player
public void setPlayer(string name)
{
playerName.text = name;
forwardButton.gameObject.SetActive(true);
backButton.gameObject.SetActive(true);
markDoneButton.gameObject.SetActive(true);
playerName.gameObject.SetActive(true);
}
//Called when the Eteen button is clicked
public void onForwardClick()
{
LiiikkumisScript.liiikkumisScript.MoveForward(LiiikkumisScript.getPlayer(playerName.text));
}
//Called when the Taakse button is clicked
public void onBackClick()
{
LiiikkumisScript.liiikkumisScript.MoveBackwards(LiiikkumisScript.getPlayer(playerName.text));
}
//Called when the Merkitse tehdyksi button is clicked
public void onMarkDoneClick()
{
Player selected = LiiikkumisScript.getPlayer(playerName.text);
if (selected.completedTasks.Contains(selected.position))
{
selected.completedTasks.Remove(selected.position);
}
else
{
selected.completedTasks.Add(selected.position);
}
}
public class Player
{
public string name;
public int position;
public List<int> completedTasks = new List<int>();
public static List<Player> list = new List<Player>();
public Player(string name)
{
this.name = name;
list.Add(this);
}
}
}
and here is the XML serializer script that has the "SaveData" class.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using System.IO;
using System.Xml;
using System.Xml.Serialization;
public class SaveManager : MonoBehaviour
{
string tutorialText;
public static SaveManager instance;
public SaveData activeSave;
public bool hasLoaded;
private void Awake()
{
instance = this;
Load();
}
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
if (Input.GetKeyDown(KeyCode.K))
{
Save();
}
if (Input.GetKeyDown(KeyCode.L))
{
Load();
}
if (Input.GetKeyDown(KeyCode.J))
{
DeleteSaveData();
}
}
public void Save()
{
string dataPath = Application.persistentDataPath;
var serializer = new XmlSerializer(typeof(SaveData));
var stream = new FileStream(dataPath + "/" + activeSave.saveName + ".save", FileMode.Create);
serializer.Serialize(stream, activeSave);
stream.Close();
Debug.Log("Saved");
}
public void Load()
{
string dataPath = Application.persistentDataPath;
if (System.IO.File.Exists(dataPath + "/" + activeSave.saveName + ".save"))
{
var serializer = new XmlSerializer(typeof(SaveData));
var stream = new FileStream(dataPath + "/" + activeSave.saveName + ".save", FileMode.Open);
activeSave = serializer.Deserialize(stream) as SaveData;
stream.Close();
Debug.Log("Loaded");
hasLoaded = true;
}
}
public void DeleteSaveData()
{
string dataPath = Application.persistentDataPath;
if (System.IO.File.Exists(dataPath + "/" + activeSave.saveName + ".save"))
{
File.Delete(dataPath + "/" + activeSave.saveName + ".save");
Debug.Log("Save deleted");
}
}
}
[System.Serializable]
public class SaveData
{
public string saveName;
public int boardSize;
public List<string> pawnNames;
public List<int> tasksDone;
}
I can post other scripts of this project if needed, thanks.
,How can I save the "completedTasks" list for different players into the SaveData class in my save script?
Here is the script that has the list
using System;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
//This is for the player option screen that pop-ups when u select a player
public class PlayerOptions : MonoBehaviour
{
public static PlayerOptions playerOptions;
public Button forwardButton, backButton, markDoneButton;
public Text playerName;
private bool clicked;
int i = 0;
void Start()
{
playerOptions = this;
forwardButton.onClick.AddListener(onForwardClick);
backButton.onClick.AddListener(onBackClick);
markDoneButton.onClick.AddListener(onMarkDoneClick);
playerName.text = PawnNameStorage.instance.StoredNames[0];
}
//Draws all the lines.
//Put all the line rendering here
void Update()
{
RenderLines.clearLines();
Player selected = LiiikkumisScript.getPlayer(playerName.text);
if (selected == null)
{
return;
}
//Render done tasks with lime
foreach (int position in selected.completedTasks)
{
RenderLines.drawLine(GameObject.Find("Cube (" + (position + 2) + ")"), new Color(0, 200, 0), 0.3f, 0);
}
//Render current position with red. Also if theres a task rendered in the same spot then it renders it so they dont collide
float minusWidth = 0;
if (selected.completedTasks.Contains(selected.position))
{
minusWidth = 0.4f;
}
RenderLines.drawLine(GameObject.Find("Cube (" + (selected.position + 2) + ")"), new Color(200, 0, 0), 0.3f, minusWidth);
//Draw orange thing to the point where the mouse is at and move selected player to it if mouse is clicked
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
if (Physics.Raycast(ray, out hit))
{
if (hit.transform.gameObject.name.StartsWith("Cube ("))
{
int position = Int32.Parse(hit.transform.gameObject.name.Replace("Cube (", "").Replace(")", "")) - 2;
float minusWidth2 = 0;
if (selected.completedTasks.Contains(position))
{
minusWidth2 = 0.4f;
}
if (selected.position != position) RenderLines.drawLine(hit.transform.gameObject, new Color(0, 0, 200), 0.3f, minusWidth2);
//Move selected player to the point of the mouse if clicked
if (Input.GetMouseButton(0) && !clicked)
{
clicked = true;
GameObject gameObject = LiiikkumisScript.getPlayerObject(selected.name);
LiiikkumisScript.liiikkumisScript.Positions[selected.position].GetComponent<Stacker>().RemoveObject(gameObject);
selected.position = position;
LiiikkumisScript.liiikkumisScript.Positions[selected.position].GetComponent<Stacker>().AddObject(gameObject);
}
else if (!Input.GetMouseButton(0))
{
clicked = false;
}
}
}
}
//Sets the currently selected player
public void setPlayer(string name)
{
playerName.text = name;
forwardButton.gameObject.SetActive(true);
backButton.gameObject.SetActive(true);
markDoneButton.gameObject.SetActive(true);
playerName.gameObject.SetActive(true);
}
//Called when the Eteen button is clicked
public void onForwardClick()
{
LiiikkumisScript.liiikkumisScript.MoveForward(LiiikkumisScript.getPlayer(playerName.text));
}
//Called when the Taakse button is clicked
public void onBackClick()
{
LiiikkumisScript.liiikkumisScript.MoveBackwards(LiiikkumisScript.getPlayer(playerName.text));
}
//Called when the Merkitse tehdyksi button is clicked
public void onMarkDoneClick()
{
Player selected = LiiikkumisScript.getPlayer(playerName.text);
if (selected.completedTasks.Contains(selected.position))
{
selected.completedTasks.Remove(selected.position);
}
else
{
selected.completedTasks.Add(selected.position);
}
}
public class Player
{
public string name;
public int position;
public List<int> completedTasks = new List<int>();
public static List<Player> list = new List<Player>();
public Player(string name)
{
this.name = name;
list.Add(this);
}
}
}
and here is the XML-serializer save script that contains the SaveData class.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using System.IO;
using System.Xml;
using System.Xml.Serialization;
public class SaveManager : MonoBehaviour
{
string tutorialText;
public static SaveManager instance;
public SaveData activeSave;
public bool hasLoaded;
private void Awake()
{
instance = this;
Load();
}
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
if (Input.GetKeyDown(KeyCode.K))
{
Save();
}
if (Input.GetKeyDown(KeyCode.L))
{
Load();
}
if (Input.GetKeyDown(KeyCode.J))
{
DeleteSaveData();
}
}
public void Save()
{
string dataPath = Application.persistentDataPath;
var serializer = new XmlSerializer(typeof(SaveData));
var stream = new FileStream(dataPath + "/" + activeSave.saveName + ".save", FileMode.Create);
serializer.Serialize(stream, activeSave);
stream.Close();
Debug.Log("Saved");
}
public void Load()
{
string dataPath = Application.persistentDataPath;
if (System.IO.File.Exists(dataPath + "/" + activeSave.saveName + ".save"))
{
var serializer = new XmlSerializer(typeof(SaveData));
var stream = new FileStream(dataPath + "/" + activeSave.saveName + ".save", FileMode.Open);
activeSave = serializer.Deserialize(stream) as SaveData;
stream.Close();
Debug.Log("Loaded");
hasLoaded = true;
}
}
public void DeleteSaveData()
{
string dataPath = Application.persistentDataPath;
if (System.IO.File.Exists(dataPath + "/" + activeSave.saveName + ".save"))
{
File.Delete(dataPath + "/" + activeSave.saveName + ".save");
Debug.Log("Save deleted");
}
}
}
[System.Serializable]
public class SaveData
{
public string saveName;
public int boardSize;
public List<string> pawnNames;
public List<int> tasksDone;
}
I can post other scripts if needed.
Thanks
Your answer
![](https://koobas.hobune.stream/wayback/20220613033152im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
TextAsset vs StreamReader 2 Answers
DataContract serialization not compatible with Unity? 0 Answers
android serialization weird path issue " IsolatedStorageException ", javascript 0 Answers
I can load xml data, but I can't to save it, Here the script 2 Answers
How to read data with 'Saving and Loading Data : XmlSerializer' 1 Answer