Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 TakahuoneenSami · May 02, 2021 at 07:31 PM · xmlserializer

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

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

0 Replies

· Add your reply
  • Sort: 

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

117 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

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


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