Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Help with Xml Serialization

Discussion in 'Scripting' started by Geoki, Aug 16, 2015.

  1. Geoki

    Geoki

    Joined:
    Mar 21, 2014
    Posts:
    13
    So I have been trying to save a string list with xml serialization but unfortunately I couldn't figure out how it works with my poor knowledge on scripting.

    It would be really nice if someone could help me write a script which will load & save my list with this way.
    public List<string> Collected = new List<string>();

    Thank you very much in advance!
     
  2. GroZZleR

    GroZZleR

    Joined:
    Feb 1, 2015
    Posts:
    3,201
    What aspects are you struggling with? What code have you written? How do you want this list to be formatted?

    We're more than happy to help but no one's going to write an entire script for you - you wouldn't learn anything.
     
  3. Geoki

    Geoki

    Joined:
    Mar 21, 2014
    Posts:
    13
    That's my code. My list handles the progress of my player on the entire game so it is very important to be able to save it and load it whenever he/she wants. I don't know how many ways can a list be formatted but I would like to be able to save this list on all mobile platforms (iOS, Android, Windows and blackberry - optional)


    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using System;
    4. using System.Collections.Generic;
    5.  
    6. public class GameController : MonoBehaviour {
    7.  
    8.     public static GameController Controller;
    9.     public int Lives;
    10.     public int Wumpas;
    11.     public int Health;
    12.    
    13.     // Use this for initialization
    14.     void Awake () {
    15.         if (Controller == null) {
    16.             DontDestroyOnLoad (gameObject);
    17.             Controller = this;
    18.         }
    19.         else if (Controller != null) {
    20.             Destroy(gameObject);
    21.         }
    22.     }
    23.    
    24.     // Update is called once per frame
    25.     void Update () {
    26.    
    27.     }
    28.  
    29.     public void Save() {
    30.         PlayerPrefs.SetInt ("Wumpas", Wumpas);
    31.         PlayerPrefs.SetInt ("Lives", Lives);
    32.         PlayerPrefs.SetInt ("Health", Health);
    33.     }
    34.  
    35.     public void Load() {
    36.         Wumpas = PlayerPrefs.GetInt ("Wumpas");
    37.         Lives = PlayerPrefs.GetInt ("Lives");
    38.         Health = PlayerPrefs.GetInt ("Health");  
    39.     }
    40.  
    41.     public List<string> Uncollected = new List<string>();
    42.     public List<string> Collected = new List<string>();
    43.  
    44.     public void Earned(string name) {
    45.         Uncollected.Remove(name);
    46.         Collected.Add(name);
    47.     }
    48. }
     
  4. Geoki

    Geoki

    Joined:
    Mar 21, 2014
    Posts:
    13
    If anyone could help me understand the logic that would be really nice!