I want to save the list of type Myclass in PlayerPrefs.
I saw an example with converting in a String, how can I do that?
public class MyClass{
private int ex;
}
List<MyClass> l = new List<MyClass>();
//How to save it?
答案 0 :(得分:5)
Approach using JSON to serialize/deserialize your object into string is the better option in my personal opinion.
You can find a lot of JSON libs for C#, just research for it and find one that suits better for you.
Or take a look at unity wiki ArrayPrefs2 and extends or use it to create your own parser to save your object structures. I, personally, wouldn't recommend this approach to save entire objects but is an option to mention...
When your object is simple enough to use JsonConverter from Newtonsoft as suggested by Povl Eller:
To Save
Character char = new Character();
char.Name = "Apple";
char.Level = 1;
char.Health = 150;
char.Attributes = new int[] { 10, 10, 10 };
PlayerPrefs.SetString( "PlayerCharacter", JsonConvert.SerializeObject(char) );
To Load
Character char = JsonConvert.DeserializeObject<Character>( PlayerPrefs.GetString( "PlayerCharacter" ) );
If you need to control the serialization for more complex objects or something you can use JsonSerializer that is documented in this same link at newtonsoft website.
OBS: I write this code directly here and didn't test it, probably you'll need to adjust it to be useful...
答案 1 :(得分:1)
Perhaps something like
for(int i=0;i<list.length;i++)
{
PlayerPrefs.SetString(list(i).name, list(i).value);
}
答案 2 :(得分:-3)
Try to use newtonsoft.json.
They have extended examples and these two should get you started: http://www.newtonsoft.com/json/help/html/SerializeObject.htm http://www.newtonsoft.com/json/help/html/DeserializeObject.htm
Best Regards