Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Question Saving an array, list, or dictionary

Discussion in 'Cloud Save' started by hakankaraduman, Jan 14, 2023.

  1. hakankaraduman

    hakankaraduman

    Joined:
    Aug 27, 2012
    Posts:
    353
    Hi,

    How can I serialize and save a list of strings?

    I tried to create a struct and have a field as Dictionary or a List, and used [Serializable] on the struct, but jsonutility fails to serialize the property. What would be a solution to this?
     
  2. devingunity

    devingunity

    Unity Technologies

    Joined:
    May 26, 2021
    Posts:
    33
    Hi hakankaraduman,

    Using Cloud Save, you should be able to simply save a List (or Dictionary) against a key, and the Cloud Save SDK will perform the serialization for you.

    Sample Code:
    Code (CSharp):
    1. var stringList = new List<string> { "string1", "string2", "string3" };
    2. var data = new Dictionary<string, object> { { "myStringList", stringList } };
    3.  
    4. await CloudSaveService.Instance.Data.ForceSaveAsync(data);
    5.  
    6. var retrievedData = await CloudSaveService.Instance.Data.LoadObjectsAsync(new HashSet<string> { "myStringList" });
    7. var retrievedStringList = data["myStringList"];
    Here you're saving your list against the key "myStringList" in Cloud Save, and retrieving it by its key.
     
  3. halley

    halley

    Joined:
    Aug 26, 2013
    Posts:
    2,364
    The key type AND the value type of a Dictionary need to be serializable, for the Dictionary to actually be serializable.