Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by zerikscythe · Jul 17, 2014 at 05:40 PM · c#playerprefsloading

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.

  1. I've successfully created code to save my player data off using a BinaryFormatter. (Check)

  2. 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?

Comment
Add comment · Show 4
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Kiwasi · Jul 17, 2014 at 06:41 PM 0
Share

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.

avatar image zerikscythe · Jul 17, 2014 at 06:57 PM 0
Share

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

avatar image zerikscythe · Jul 17, 2014 at 06:59 PM 0
Share

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.

avatar image Sisso · Jul 17, 2014 at 07:30 PM 0
Share

If you can, provide a small but full test file so we can download and verify.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

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.

http://unitygems.com/saving-data-1-remember-me/

Comment
Add comment · Show 4 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image zerikscythe · Jul 17, 2014 at 06:16 PM 0
Share

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 :)

avatar image zerikscythe · Jul 17, 2014 at 06:21 PM 0
Share

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?

avatar image Sisso · Jul 17, 2014 at 06:30 PM 0
Share

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.

avatar image zerikscythe · Jul 17, 2014 at 06:34 PM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges