- Home /
Saving Stream to PlayerPref then unpack in new scene.
So maybe my methodology is a bit off, but this is what I'm trying to do.
I've successfully created code to save my player data off using a BinaryFormatter. (Check)
De-serialize said save. (also Check)
and here's where i deviate from the norm. I'm trying to pass the saved info off to another scene that gets loaded by assigning all the saved info into the PlayerPrefs.SetString() method.
this is the block of code I'm using to do so:
void SaveToPref(Stream s)
{
stream.Position = 0;
StreamReader sr = new StreamReader(s);
string buffer = sr.ReadToEnd();
PlayerPrefs.SetString("_sav", buffer);
PlayerPrefs.Save();
}
in theory this should place the stream into my playerpref to be unpacked later. When my new scene loads it will apply all my characters personl attributes like so;
void UnPackPref()
{
string sav = PlayerPrefs.GetString("_sav");
byte[] loadStream = Encoding.UTF8.GetBytes(sav);
MemoryStream ms = new MemoryStream(loadStream);
BinaryFormatter bf = new BinaryFormatter();
ps_ = (PlayerStats_)bf.Deserialize(ms);
}
but all the Debug.Log() files are telling me that my PlayerPref.GetString("_sav") are empty. It's not properly converting the stream to a string like i want. Any ideas?
I achieved a successful result using $$anonymous$$emoryStream.ToArray before converting to a string. Not sure if this helps for your application.
Have you tried a debug.log to see if you actually have a string during your save method.
I'm beginning to think this is a fruitless endeavor, and i should reassess how i go about persisting my data from scene to scene and game saves....
I've converted the stream to a Char[] and saw the info, but once i attempt to concatenate all the chars together back into a string var, it falls apart again.
If you can, provide a small but full test file so we can download and verify.
Answer by Sisso · Jul 17, 2014 at 06:01 PM
I didn't know exactly what is the problem, but you use word binary and are doing direct conversions to string. This have a very bad smell.
To convert a byte array to a string use a proper encode like base64.
http://answers.unity3d.com/questions/40568/base64-encodedecoding.html
This tutorial could solve your problem.
If my code even made it down that far the pipeline i'd attempt to modify it. However as it is the DataStream i converted into a string isn't getting saved into the PlayerPref.SetString() method. I just tried adding a stream.Position = 0; line but that still hasn't helped.
If it gets past that i'll check out the encodings, thanks :)
maybe i'm using the wrong StreamReader method, does ReadToEnd() not copy the contents of the stream into the property you assign it?
string myString = sr.ReadToEnd();
does that not make myString a copy of the stream?
if you add a Debug.Log(myString) it prints nothing?
If DataStream is bytes and ReadToEnd try to read as string (because it return a string) could be causing the problem.
yes, i have tried that, i get nothing. Well the "DataStream" is a file saved on the computer that i open, inject into a stream, and before i de-serialize it i'm attempting to convert that stream into a string. but so far yea, nothing is working. Thanks for the replies BTW :)
Your answer
![](https://koobas.hobune.stream/wayback/20220613154332im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Save/Load Animation State of Instantiated Prefabs 0 Answers
Loading Next Scene based on scores 0 Answers
How To Add PlayerPrefs Scores? 1 Answer