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
1
Question by BiTT_JL · Mar 07, 2012 at 11:36 PM · playerprefssaveloadloadingsaving

Saving and loading game with PlayerPrefs

Ok so I've gone over a bunch of different ways of saving a loading a game and none of them have really helped.

I have a bunch of player variables (like stats, and items). I do not need to save the player's location, only the stats.

With that said, PlayerPrefs were are easy fix, and they work. However, they are extremely easy to open up and change information. Not only that, but is there a way I can change the save location of the PlayerPrefs?

If anyone knows how to do this, or has a better idea than using PlayerPrefs to save only variables, that would be greatly appreciated!

Comment
Add comment · Show 1
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 Fattie · Apr 27, 2013 at 02:15 PM 0
Share

Note - if anyone's still reading this. It looks like there is now a handy package on the AssetStore,

http://forum.unity3d.com/threads/157606-Secured-PlayerPrefs-Release

"Secured PlayerPrefs" -- which is encrypted player prefs. This solves 99% of the problem at hand, hope it helps.

2 Replies

· Add your reply
  • Sort: 
avatar image
2

Answer by raoz · Mar 08, 2012 at 10:04 AM

Well, I am using C# serialization and it works great. You can save the information as binary and it is not the easiest thing to change. Here is an example that saves two variables, int1 and string1

 using UnityEngine;
 
 using System.Collections;
 
 using System.IO;
 
 using System.Runtime.Serialization;
 
 using System.Runtime.Serialization.Formatters;
 
 using System.Runtime.Serialization.Formatters.Binary;
 
 
 
 [System.Serializable]
 
 class VariablesforSaving
 
 {
 
     public int int1;
 
     public string string1;
 
     public VariablesforSaving()
 
     {
 
         int1 = 0;
 
         string1 = "";
 
     }
 
     //Deserialization constructor
 
     public VariablesforSaving(SerializationInfo info, StreamingContext ctxt)
 
     {
 
         //Get the values from info and assign them to the appropriate properties
 
         int1 = (int)info.GetValue("int1", typeof(int));
 
         string1 = (string)info.GetValue("mystring1a", typeof(string));
 
     }
 
         
 
     //Serialization function.
 
     public void GetObjectData(SerializationInfo info, StreamingContext ctxt)
 
     {
 
         //You can use any name/value pair, as long as you read them with same names
 
         info.AddValue("int1", int1);
 
         info.AddValue("mystring1a", string1);
 
     }
 
 }
 
 
 
 public class Example : MonoBehaviour {
 
     int int1 = 5;
 
     string string1 = "Hello";
 
     public void Save()
 
     {
 
         VariablesforSaving MyVariables = new VariablesforSaving();
 
         MyVariables.int1 = int1;
 
         MyVariables.string1 = string1;
 
         Stream stream = File.Open("MyVariables.randomfileextension", FileMode.Create);
 
         BinaryFormatter bformatter = new BinaryFormatter();            
 
         Debug.Log("Saving variables");
 
         bformatter.Serialize(stream, MyVariables);
 
         stream.Close();
 
     }
 
     public void Load()
 
     {
 
         VariablesforSaving MyVariables = new VariablesforSaving();
 
         Stream stream = File.Open("MyVariables.randomfileextension", FileMode.Open);
 
         BinaryFormatter bformatter = new BinaryFormatter();          
 
         UnityEngine.Debug.Log("Loading variables");
 
         MyVariables = (VariablesforSaving)bformatter.Deserialize(stream);
 
         Debug.Log(MyVariables.int1);
 
         Debug.Log(MyVariables.string1);
 
         stream.Close();
 
     }
 
     public    void Update()
 
     {
 
         if(Input.GetKeyDown("s"))
 
             Save();
 
         if(Input.GetKeyDown("l"))
 
             Load();
 
     }
 
 }

Yes, it is pretty long, but it is easy to add new variables. Google 'C# serializing' for more information.

Comment
Add comment · Show 6 · 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 BiTT_JL · Mar 08, 2012 at 03:28 PM 0
Share

I've taken a look at that as well, and it seems to be the better option. I'm using javascript so will I need to change anything to make this work? It doesn't interfere with anything other than the variables it's saving right?

avatar image raoz · Mar 08, 2012 at 08:15 PM 0
Share

I don't know about javascript, I generally use C#. I think this is a C# feature... However, you could make a javascript script access this C# script, I think. Javascript example(You need to have the both scripts attached: http://paste.bradleygill.com/index.php?paste_id=358823

For more information, check GameObject.GetComponent from unity script reference And scroll to the bottom

avatar image BiTT_JL · Mar 08, 2012 at 08:34 PM 0
Share

Thank you for the help! I greatly appreciate it!

avatar image raoz · Mar 08, 2012 at 10:14 PM 0
Share

Could you please then mark it as right ansver?

avatar image BrigadeJosh · Nov 05, 2012 at 09:51 AM 0
Share

I'm a bit late to the party, but this answer works well in conjunction with this page I found searching for Serialization in general. Hope it helps someone else too!

http://www.switchonthecode.com/tutorials/csharp-tutorial-serialize-objects-to-a-file

Show more comments
avatar image
0

Answer by BiG · Mar 07, 2012 at 11:41 PM

Well, even if you want to save just a few variables of your player, it's an easy task: take a look at this answer of mine, I think that it could help you.

And no, you can't change the save location of the PlayerPrefs. The Documentation says where it's located, but it doesn't speak about the possibility of changing the path. You have to use a database or a text file to decide on your own the location.

Comment
Add comment · Show 2 · 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 BiTT_JL · Mar 08, 2012 at 04:33 AM 1
Share

Yes I'm currently using PlayerPrefs, but the bad thing about them is that they are extremely easy to get into and change. I don't want the player to change any of their stats to what they shouldn't be.

If there isn't a way I can change the path to it, is there a way I can encrypt or at least make it more difficult to access the data? I just don't want the player altering their level or experience, or the items they have.

avatar image syclamoth · Mar 08, 2012 at 04:36 AM 0
Share

Well, you could try encrypting the data before you enter it into the playerprefs! That way, the playerprefs file contains an encrypted version of the information, that you decrypt after loading it. If the player tries to modify it, they will destroy all their data unless they have some means of decoding it (which I guess makes them very deter$$anonymous$$ed).

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

9 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Sharing Violation On Path 1 Answer

Saving boolean values using PlayerPrefs 1 Answer

Save works for computer but not once i create the build and load it on the android 1 Answer

Saving with PlayerPrefs? How it works? 1 Answer

Problem with saving the transform of the player 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