Skip to main content
added 62 characters in body
Source Link
ezez
  • 326
  • 1
  • 3

To be able to serialize some data using Unity's built in serializer, you will have to contain your data in a class. You can't simply serialize a list as is.

Something like this should work. You can make some sort of generic class like here, if you only need to serialize lists containing different types of data:

using System.Collections.Generic;
using UnityEngine;

public class SerializationDemo : MonoBehaviour
{
    [SerializeField] List<string> yourList;

    void Start()
    {
        // Your list with some data
        yourList = new List<string> { "foo", "bar" };

        // Store your data into a container class, 
        // this can be serialized
        var data = new SaveData<string>(yourList);

        // Class contents to json using Unity's serializer
        var jsonText = JsonUtility.ToJson(data);

        // Save your json data here instead of logging it...
        // JSON: {"list":["foo","bar"]}
        Debug.Log("JSON: " + jsonText.ToString());
    }
}


[System.Serializable]
public class SaveData<T>
{
    public List<T> list;

    public SaveData(List<T> data)
    {
        this.list = data;
    }
}

To be able to serialize some data using Unity's built in serializer, you will have to contain your data in a class. You can't simply serialize a list as is.

Something like this should work. You can make some sort of generic class like here, if you only need to serialize lists containing different types of data:

using System.Collections.Generic;
using UnityEngine;

public class SerializationDemo : MonoBehaviour
{
    [SerializeField] List<string> yourList;

    void Start()
    {
        // Your list with some data
        yourList = new List<string> { "foo", "bar" };

        // Store your data into a container class, 
        // this can be serialized
        var data = new SaveData<string>(yourList);

        // Class contents to json using Unity's serializer
        var jsonText = JsonUtility.ToJson(data);

        // JSON: {"list":["foo","bar"]}
        Debug.Log("JSON: " + jsonText.ToString());
    }
}


[System.Serializable]
public class SaveData<T>
{
    public List<T> list;

    public SaveData(List<T> data)
    {
        this.list = data;
    }
}

To be able to serialize some data using Unity's built in serializer, you will have to contain your data in a class. You can't simply serialize a list as is.

Something like this should work. You can make some sort of generic class like here, if you only need to serialize lists containing different types of data:

using System.Collections.Generic;
using UnityEngine;

public class SerializationDemo : MonoBehaviour
{
    [SerializeField] List<string> yourList;

    void Start()
    {
        // Your list with some data
        yourList = new List<string> { "foo", "bar" };

        // Store your data into a container class, 
        // this can be serialized
        var data = new SaveData<string>(yourList);

        // Class contents to json using Unity's serializer
        var jsonText = JsonUtility.ToJson(data);

        // Save your json data here instead of logging it...
        // JSON: {"list":["foo","bar"]}
        Debug.Log("JSON: " + jsonText.ToString());
    }
}


[System.Serializable]
public class SaveData<T>
{
    public List<T> list;

    public SaveData(List<T> data)
    {
        this.list = data;
    }
}
Source Link
ezez
  • 326
  • 1
  • 3

To be able to serialize some data using Unity's built in serializer, you will have to contain your data in a class. You can't simply serialize a list as is.

Something like this should work. You can make some sort of generic class like here, if you only need to serialize lists containing different types of data:

using System.Collections.Generic;
using UnityEngine;

public class SerializationDemo : MonoBehaviour
{
    [SerializeField] List<string> yourList;

    void Start()
    {
        // Your list with some data
        yourList = new List<string> { "foo", "bar" };

        // Store your data into a container class, 
        // this can be serialized
        var data = new SaveData<string>(yourList);

        // Class contents to json using Unity's serializer
        var jsonText = JsonUtility.ToJson(data);

        // JSON: {"list":["foo","bar"]}
        Debug.Log("JSON: " + jsonText.ToString());
    }
}


[System.Serializable]
public class SaveData<T>
{
    public List<T> list;

    public SaveData(List<T> data)
    {
        this.list = data;
    }
}