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

How can I save data from List via PlayerPrefs ?

Discussion in 'Scripting' started by hellpirat, Aug 3, 2015.

Thread Status:
Not open for further replies.
  1. hellpirat

    hellpirat

    Joined:
    Jun 12, 2015
    Posts:
    26
    Hello, everybody.
    Can you get me some examples how save List via PlayerPrefs?
     
    guetta18 likes this.
  2. LaneFox

    LaneFox

    Joined:
    Jun 29, 2011
    Posts:
    7,384
  3. sz-Bit-Barons

    sz-Bit-Barons

    Joined:
    Nov 12, 2013
    Posts:
    150
    you can!

    example for an Int-List:
    Code (csharp):
    1. List<int> myList;
    2. // ...
    3. PlayerPrefs.SetInt("myList_count", myList.Count);
    4.  
    5. for(int i = 0; i < myList.Count; i++)
    6.     PlayerPrefs.SetInt("myList_" + i, myList[i]);
     
  4. hellpirat

    hellpirat

    Joined:
    Jun 12, 2015
    Posts:
    26
    Well, I can keep the int, but what if I have a list with GameObject or not standard List with 2+ types?
     
  5. SomeGuy22

    SomeGuy22

    Joined:
    Jun 3, 2011
    Posts:
    722
    Code (JavaScript):
    1.     var myList : List.<int>;
    2.     // ...
    3.     PlayerPrefs.SetInt("myList_count", myList.Count);
    4.    
    5.     for(var i = 0; i < myList.Count; i++)
    6.     {
    7.         if (myList[i].GetType == typeof(int))
    8.         PlayerPrefs.SetInt("myList_" + i, myList[i]);
    9.  
    10.         else if (myList[i].GetType == typeof(String))
    11.         PlayerPrefs.SetString("myList_" + i, myList[i]);
    12.        
    13.     }
    14.  
    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:

    Code (JavaScript):
    1. if (myList[i].GetType == typeof(int))
    2. PlayerPrefs.SetInt("myList_int_" + i, myList[i]);
    3. //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 :)
     
  6. sz-Bit-Barons

    sz-Bit-Barons

    Joined:
    Nov 12, 2013
    Posts:
    150
    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).
     
  7. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    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
     
  8. hellpirat

    hellpirat

    Joined:
    Jun 12, 2015
    Posts:
    26
    Can I use something like this?
    Code (CSharp):
    1. public List<Items> Itemses = new List<Items>();
    2.  
    3.   PlayerPrefs.SetInt("InventoryCount", Itemses.Count);
    4.  
    5.                 for (var i = 0; i < Itemses.Count; i++)
    6.                 {
    7.                 PlayerPrefs.SetInt("price" + i, Itemses[i].Price);
    8.                 PlayerPrefsX.SetBool("IsBought" + i, Itemses[i].IsBought);
    9.                 PlayerPrefsX.SetBool("IsActive" + i, Itemses[i].IsActive);
    10.                 PlayerPrefs.Save();
    11.                 }
    12.  
    But, as I would later be able to get all this information through Get?
     
    Last edited: Aug 4, 2015
  9. sz-Bit-Barons

    sz-Bit-Barons

    Joined:
    Nov 12, 2013
    Posts:
    150
    yes, you can do it like that.
    BTW: it should be enough to save after the loop ;)

    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.
     
  10. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    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.
     
  11. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    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
     
    Lorrak likes this.
  12. sparrow

    sparrow

    Joined:
    May 6, 2012
    Posts:
    17
    Thanks Eric, that is a perfect solution.
     
  13. canklot

    canklot

    Joined:
    Jul 3, 2020
    Posts:
    3
Thread Status:
Not open for further replies.