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 zardini123 · Apr 14, 2013 at 04:55 PM · variableshideplayerpref

Hide variables in PlayerPrefs

My game uses the PlayerPrefs file to save important info of characters like health. The problem is that anybody can get into the file by going to the directory, and when they get there, they can edit the info to cheat and such. Is there a way to hide the variables in the PlayerPref's file so that people can't see the variable with the naked eye? If not, is there a way thats similar?

Comment
Add comment
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

2 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by Eric5h5 · Apr 14, 2013 at 05:25 PM

You can use ArrayPrefs2 to save data with PlayerPrefs, where values are converted to bytes which are stored as strings using Base64. Technically they can still be edited, but would be much harder (for non-programmers anyway). Also, even if you're not actually using arrays, you can make a dummy array and hide the "real" value in it, which would further increase the difficulty.

Comment
Add comment · Show 1 · 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 zardini123 · Apr 17, 2013 at 12:31 AM 0
Share

this works perfectly! thanks!

avatar image
1

Answer by janzdott · Apr 14, 2013 at 05:11 PM

If you want a good way of saving data and keeping players from cheating, I would recommend using binary serialization. It's very easy to set up, and creates a nice save file in binary, so players can't edit it. Heres one that I wrote.

 using System.Runtime.Serialization;
 using System.Runtime.Serialization.Formatters.Binary;
 using System.IO;
 
 public static class BinarySerializer {
     public static void serialize<T>(T data, string filePath) {
         Stream stream = File.Open(filePath, FileMode.Create);
         BinaryFormatter binaryFormatter = new BinaryFormatter();
         binaryFormatter.Binder = new VersionDeserializationBinder();
         binaryFormatter.Serialize(stream, data);
         stream.Close();
     }
     public static T deserialize<T>(string filePath) {
         try {
             Stream stream = File.Open(filePath, FileMode.Open);
             BinaryFormatter binaryFormatter = new BinaryFormatter();
             binaryFormatter.Binder = new VersionDeserializationBinder();
             T data = (T)binaryFormatter.Deserialize(stream);
             stream.Close();
             return data;
         }
         catch (FileNotFoundException exception) {
             throw exception;
         }
     }
 }

And this second class is just to make sure the save files can be loaded after unity recompiles scripts

 using System.Runtime.Serialization;
 using System.Reflection;
 
 public class VersionDeserializationBinder : SerializationBinder {
     public override System.Type BindToType(string assemblyName, string typeName) {
         if (!string.IsNullOrEmpty(assemblyName) &&     !string.IsNullOrEmpty(typeName)) {
             System.Type typeToDeserialize = null;    
             assemblyName = System.Reflection.Assembly.GetExecutingAssembly().FullName;
             typeToDeserialize = System.Type.GetType(string.Format("{0}, {1},", typeName, assemblyName));
             return typeToDeserialize;
         }
         return null;
     }
 }


And heres an example class that can be serialized and deserialized.

 using UnityEngine;
 using System.Collections;
 using System.Runtime.Serialization;
 
 [System.Serializable()]
 public class MySaveData : ISerializable {
     public int savedInt = 0;
     
     public MySaveData() {}
     
     public MySaveData(SerializationInfo info, StreamingContext context) {
         savedInt = (int)info.GetValue("savedInt", typeof(int));
     }
         
     public void GetObjectData(SerializationInfo info, StreamingContext context) {
         info.AddValue("savedInt", savedInt);    
     }
 }

And here is an example showing how to save and load a file

 MySaveData saveData = new MySaveData();
 saveData.savedInt = 12345;
 
 string filePath = "blah/blah/blah"
 
 //Saves saveData
 BinarySerializer.serialize<MySaveData>(saveData, filePath);
 
 //Load saveData
 saveData = BinarySerializer.deserialize<MySaveData>(filePath);
 

   
 
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 whydoidoit · Apr 14, 2013 at 05:43 PM 0
Share

Guessing you'd need to check for things compiled in first pass on Plugins for changes too though right (does your code support serializing those? Looks like perhaps it doesn't?) Na$$anonymous$$g shouldn't be strong in any case - so do you actually find it necessary to do this binding?

avatar image zardini123 · Apr 14, 2013 at 05:45 PM 0
Share

Uh, can you convert to JavaScript please?

avatar image janzdott · Apr 14, 2013 at 07:10 PM 0
Share

whydoidoit - I have never used the Plugins folder for anything, so I never thought about if it would make a difference or not. And yes its necessary to use the custom binder, otherwise a file cannot be deserialized after Unity has recompiled. It is not necessary for the final build. But you cannot test it in the editor without using the custom binder, since Unity recompiles every time you click the play button.

GameDude - I don't know JS, but shouldn't be hard at all to convert. Just gotta get rid of the generic stuff, which isn't needed. Or you could find an example on Google probably

avatar image whydoidoit · Apr 14, 2013 at 07:20 PM 0
Share

So the Plugins and the Javascript assemblies are different and so the ExecutingAssembly stuff wouldn't work for that - normally you'd iterate across the assemblies in AppDomain.CurrentDomain.GetAssemblies() to try all of them. I'm confused as to why a recompile would cause a problem as the assemblies are not strongly named? Put it this way - I use it without a typename resolver without issue and without causing a problem of referencing one of the other assemblies.

I do seem to remember they accidentally made UnityEngine strongly named a version back - but that's been fixed again.

avatar image janzdott · Apr 15, 2013 at 03:11 PM 0
Share

Hmm before I wrote this, I read somewhere that you needed the custom binder or it wouldn't work. When I get home, I'll comment out those lines and see if it still works!

Show more comments

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

14 People are following this question.

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

Related Questions

How can you hide variables within a region in a c# script? 1 Answer

Obfuscate variables in release build? 1 Answer

Public variables not updating? 1 Answer

cleanup variables on editor BUG? 1 Answer

How to fix unstable boolean values? [possible bug] 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