I’m using a List to store my data but I do not know how to save my List in order to retrieve my data.
I could convert the List using ToArray() but I need to convert it back to List when I Start the game.
Can someone help me?
I’m using a List to store my data but I do not know how to save my List in order to retrieve my data.
I could convert the List using ToArray() but I need to convert it back to List when I Start the game.
Can someone help me?
Save the list in a scene or to a file?
You could serialise it to XML but it depends on what is in the list and if that can be serialised.
http://stackoverflow.com/questions/6572828/serialize-listt-to-xml-and-reverse-the-xml-to-listt
Is a list of objects from a custom class I created with three attributes (an int, a string and a Texture2D).
Do you want to serialise the texture or just a reference to the texture?
How would converting the list to an array help serialise it?
Are you trying to save them in the scene or at runtime?
Are you classes Sysem.Serializable?
I just want to reference to the texture to use it with the GUI.
Convert it to array was just an idea.
Are you trying to save this during runtime or just in the inspector?
Ill assume the easy option which is in the inspector:
[System.Serializable] // Important
public class Item
{
public int i;
public string s;
public Texture2D texRef;
}
public MyBehaviour : MonoBehaviour
{
public List<Item> myItems;
}
Is this what you need?
Yes that’s my class. What do I need is to save it to retrieve the data.
You need to answer the question, whether you want to save it in the Unity Editor or at runtime. The code karljj1 posted is sufficient to store it in the Unity Editor, but at runtime another strategy is needed. It is usually not a bad idea to show code snippets. E.g. a private list won’t be serialized, unless you are using [SerializeField] for it. If you show the relevant code parts, it is a lot easier to spot issues.