How can I save data from List via PlayerPrefs ?

Hello, everybody.
Can you get me some examples how save List via PlayerPrefs?

1 Like

You can’t.

3 Likes

you can!

example for an Int-List:

List<int> myList;
// ...
PlayerPrefs.SetInt("myList_count", myList.Count);

for(int i = 0; i < myList.Count; i++)
    PlayerPrefs.SetInt("myList_" + i, myList[i]);
6 Likes

Well, I can keep the int, but what if I have a list with GameObject or not standard List with 2+ types?

    var myList : List.<int>;
    // ...
    PlayerPrefs.SetInt("myList_count", myList.Count);
    
    for(var i = 0; i < myList.Count; i++)
    {
        if (myList[i].GetType == typeof(int))
        PlayerPrefs.SetInt("myList_" + i, myList[i]);

        else if (myList[i].GetType == typeof(String))
        PlayerPrefs.SetString("myList_" + i, myList[i]);
        
    }

Of course then you would need a way of determining what the return value type is going to be when you re-access that data. This is especially a problem since PlayerPrefs.HasKey is non-type specific. So you’ll just need to pass in more data when you save:

if (myList[i].GetType == typeof(int))
PlayerPrefs.SetInt("myList_int_" + i, myList[i]);
//etc...

Which means you’ll have to split the string when recovering the data (you would’ve had to do that anyways to get each list index.

Also you should know that you can’t “save” a GameObject. PlayerPrefs (and XML and other common read/write file types) don’t have GameObject as a type. They only have standard variables–int, float, String, and boolean. Meaning you can only save the name of the game object as a String, or the game object’s position/rotation/etc. Or you can assign the GameObject an index and save that index for later use. But you can’t pass a type GameObject into a file without some manual workarounds :slight_smile:

PlayerPrefs are not meant to save game objects or even whole game states. It is possible in theory but you would end up using some serialization and put that into the variables as string…
Player prefs are for some settings you want to save between sessions, such as audio volume…

If you wanna save more complex data you should use a serializer for that. I would recommend to use a Json serializer because json is readable and they seem to become the non-official standard for this purpose.

If you don’t want to spend money, you can use MiniJson (Not sure how good it is… never really used it).
If you can spend money, I would recommend Json .Net for Unity which is super powerful, super easy to use and which even works on iOS (most serializers have problems with AOT platforms like iOS).

Use ArrayPrefs2. It works with arrays rather than Lists, but it’s trivial to convert List to array when saving and array to List when loading.

–Eric

Can I use something like this?

public List<Items> Itemses = new List<Items>();

  PlayerPrefs.SetInt("InventoryCount", Itemses.Count);

                for (var i = 0; i < Itemses.Count; i++)
                {
                PlayerPrefs.SetInt("price" + i, Itemses[i].Price);
                PlayerPrefsX.SetBool("IsBought" + i, Itemses[i].IsBought);
                PlayerPrefsX.SetBool("IsActive" + i, Itemses[i].IsActive);
                PlayerPrefs.Save();
                }

But, as I would later be able to get all this information through Get?

yes, you can do it like that.
BTW: it should be enough to save after the loop :wink:

And yes, you can load it later. Read the count first, then you can iterate with a for loop and add elements to the list and assign all the data inside the elements almost the same way you save them.

Anything can be save to player prefs. Anything at all. Simply serialise the data to a bite array. Then convert the byte array to a string. Then save.

You can also use other serialise to string formats, like JSON or XML. As long as you can convert your data to a string, you can save it to player prefs.

Note: I didn’t say saving everything to player prefs was a good idea, just that it was possible.

That’s not very efficient. Just use ArrayPrefs2, and convert Lists to/from arrays as needed. It’s completely trivial; don’t make things so hard when they are easy.

–Eric

1 Like

Thanks Eric, that is a perfect solution.

You can put your list in a class and use JSONSerialization to convert your class to json and save it that way