2

I have an array and I would like to save it to PlayerPrefs. I heard, I can do this:

PlayerPrefs.SetStringArray('title', anArray);

but for some reason it does not work. Maybe I'm not using some library like using UnityEngine.PlayerPrefs;?

Can someone help me?

Thanks in advance

Some-Guy
  • 21
  • 1
  • 3

1 Answers1

6

You can't. PlayerPrefs doesn't support arrays.

But you could use a special separator and do e.g.

PlayerPrefs.SetString("title", string.Join("###", anArray));

and then for reading use

var anArray = PlayerPrefs.SetString("title").Split(new []{"###"}, StringSplitOptions.None);

Or if you know the content and in particular which character is never used you could also use a single char e.g.

PlayerPrefs.SetString("title", string.Join("/n", anArray));

and then for reading use

var anArray = PlayerPrefs.SetString("title").Split('/n');

Yes as TEEBQNE mentioned there is PlayerPrefsX.cs which might be the source of the confusion.

I would NOT recommend it though! It simply converts all the different input types into byte[] and from there to Base64 strings.

That might be cool and all for int[], bool[], etc. But for string[] this is absolutely inefficient since the Base64 bytes representation of a string is way longer than the string itself!

It might be a valid alternative though if you can not rely on your strings contents and you can not be sure that your separator sequence is never actually a content of any string.

derHugo
  • 83,094
  • 9
  • 75
  • 115
  • There is [PlayerPrefsX](http://wiki.unity3d.com/index.php/ArrayPrefs2?_ga=2.231179423.1064057212.1620065692-152301318.1542389082), which I believe what OP is referring to when referencing `SetStringArray`. It is not vanilla PlayerPrefs, but I think they are just getting them confused. – TEEBQNE May 10 '21 at 19:00
  • That uses underlying bytes and base64 strings .. which sounds pretty inefficient though .. so basically in the end it uses SetString and GetString for everything but the base64 bytes representation of a string is usually longer the the original string itself afaik – derHugo May 10 '21 at 19:02
  • I prefer your solution. I just wanted to mention it to clear up where `SetStringArray` comes from. – TEEBQNE May 10 '21 at 19:05
  • 1
    @TEEBQNE yeah I see, good to know of course. Just wanted to make clear for OP that it's not really the best idea to use that ;) – derHugo May 10 '21 at 20:01
  • How exactly would I use PlayerPrefsX? Any Libraries? – Some-Guy May 10 '21 at 20:23
  • 2
    @Some-Guy as said it's not good to use that .. if you really want to anyway - against all advices .. well you simply go to the link of TEEBQNE and copy the content of the `PlayerPrefsX.cs` scripts as a new script into your project .. I wouldn't though but rather use mentioned Join/Split which is way more efficient – derHugo May 10 '21 at 20:25
  • I'm kind of confused. I don't mind which method I use by i want to be able to save an array to PlayerPrefs. How can i do this? – Some-Guy May 13 '21 at 19:05
  • @Some-Guy so what did not work in what I said? – derHugo May 13 '21 at 21:23
  • Hey @derHugo can you please suggest some hack for a 2-D array as well. – abhishek ranjan May 26 '21 at 07:11
  • @abhishekranjan a 2D array (`[,]`) can simply be converted from and to a normal flat `[]` ;) Maybe you would want to store the dimensions (e.g. in separate Prefs keys) as well and then [flatten the array](https://stackoverflow.com/questions/641499/convert-2-dimensional-array). And later for reading first read the dimensions and the flat array and convert it back to a [2D array](https://stackoverflow.com/questions/28113015/c-sharp-convert-1d-array-to-2d). Or alternatively do not flatten or back convert the array but rather only use a flat array and change the index to e.g. (`x + y * width`) – derHugo May 26 '21 at 07:32