• 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 RealMTG · Nov 24, 2014 at 09:11 PM · fileencrypt

Encrypt/Decrypt game level data

Hi!

I have a level editor in the works and I don't want players to change anything outside the editor and right now I am basically saving it as a txt with a custom file format. So when they open it they can change the game time, level author e.t.c. When it is loaded I create a new object and assign the parameters to it. When I save it is basically file.WriteLine(object stuff);. So how would I encrypt the whole file when it is saved and then decrypt it when it is going to be loaded?

Thanks in advance

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

1 Reply

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

Answer by Cherno · Nov 24, 2014 at 10:11 PM

Use a BinaryFormatter to serialize your data to a file that can't be modified without deserializing it again first (which would only happen in your editor's saved/load functions).

Here is an example taken from my own leveleditor's map loading and saving. Game is a class that holds all the data you want to save. Every class that is to be serialized needs to be [System.Serializable]. The Load function takes a string that is just the name of the savegame file that is to be loaded. It assigns the loadedGame variable (you can also make it return it) so other scripts can access it and pull any data they need for loading the level (player hitpoints, for example). Be aware that not every type is serializable. Things that are specific to Unity (GameObject, Transform, Vector3 and such) need to be flagged with [System.NonSerialized] before their variable declaration. This counts for EVERY class that will be used when saving and loading.

 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 using System.Runtime.Serialization.Formatters.Binary;
 using System.IO;
 
 public static class SaveLoad {
 
     public static Game loadedGame = new Game();
     public static List<Game> savedGames = new List<Game>();
 
     //it's static so we can call it from anywhere
     public static void Save(Game saveGame) {
         
         BinaryFormatter bf = new BinaryFormatter();
         FileStream file = File.Create ("C:/Savegames/" + saveGame.savegameName + ".sav"); //you can call it anything you want
         bf.Serialize(file, saveGame);
         file.Close();
         Debug.Log("Saved Game: " + saveGame.savegameName);
 
     }    
     
     public static void Load(string gameToLoad) {
         if(File.Exists("C:/Savegames/" + gameToLoad + ".sav")) {
             BinaryFormatter bf = new BinaryFormatter();
             FileStream file = File.Open("C:/Savegames/" + gameToLoad + ".gd", FileMode.Open);
             loadedGame = (Game)bf.Deserialize(file);
             file.Close();
             Debug.Log("Loaded Game: " + loadedGame.savegameName);
         }
     }
 
 
 }
 

Theoretically, you could just take the string that would be put into the txt file of yours, and store it aas a string variable inside the instance of the Game variable that is used as a savegame.

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 RealMTG · Nov 25, 2014 at 05:48 PM 0
Share

I'll be honest, I don't understand a lot of this. I made a test with this code and I got a weird error.

Code:

 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 using System.Runtime.Serialization.Formatters.Binary;
 using System.IO;
 
 [System.Serializable]
 public class EncryptTest : $$anonymous$$onoBehaviour {
 
     string text = "";
 
     void OnGUI () {
         text = GUI.TextArea(new Rect(0, 0, 200, 200), text);
 
         if(GUI.Button(new Rect(0, 200, 100, 30), "Save")){
             Save ();
         }
 
         if(GUI.Button(new Rect(100, 200, 100, 30), "Load")){
             Load ();
         }
     }
 
     public void Save(){
         BinaryFormatter bf = new BinaryFormatter();
         FileStream file = File.Create ("C:/QS/encrypt_test.qslvl");
         bf.Serialize(file, this);
         file.Close();
     }
 
     public void Load(){
         if(File.Exists("C:/QS/encrypt_test.qslvl")){
             BinaryFormatter bf = new BinaryFormatter();
             FileStream file = File.Open("C:/QS/encrypt_test.qslvl", File$$anonymous$$ode.Open);
             text = (string)bf.Deserialize(file);
             file.Close();
         }
     }
 }
 

Error: SerializationException: Type UnityEngine.$$anonymous$$onoBehaviour in assembly UnityEngine, Version=0.0.0.0, Culture=neutral, Public$$anonymous$$eyToken=null is not marked as serializable.

Could you please help me with this because I honestly don't know what I should do.

avatar image Cherno · Nov 25, 2014 at 05:58 PM 0
Share

It looks like you are trying to serialize the very script that calls the serialization functons... I don't know if that's a good idea. Better create a seperate Game class that stores the data and serialize that.

I also think that you can't serialize any classes that inherit form $$anonymous$$onoBehavior, which is why you would need so-called container classes that hold things for, say, a character monobehavior class.

Furthermore, this might be outside of UA's scope so I recommend just doing a google search on C# Serialization and read some tutorials. You won't get it to work in half an hour, I think it took me several days to understand what does what, so you just need to read, test, read, test :)

avatar image RealMTG · Nov 25, 2014 at 06:08 PM 0
Share

@Cherno Ah. I see. Well thanks for the code and tips! I will do as you say. :)

avatar image Cherno · Nov 25, 2014 at 06:23 PM 0
Share

To test things out, do the following:

Take the script from my answer as-is, and keep it somewhere in your Assets folder.

Create a new Script and also keep it somewhere in your Asset folder:

 using UnityEngine;
 using System.Collections;
 
 [System.Serializable]
 public class Game { //don't need ": $$anonymous$$onobehaviour" because we are not attaching it to a game object
 
     public string savegameName;
     public string testString;
 
         
 }
 

then, create a simple monoBehavior script for your save and load menu, like you did in your comment above. However, in the save and load GUI buttons, insert the folling code when they are clicked:

Save button:

 Game newSavedGame = new Game();
 newSavedGame.savegameName = "$$anonymous$$ySaveGame";
 newSavedGame.testString = "Hello!";
 SaveLoad.Save(newSavedGame);


Load button:

 SaveLoad.Load("$$anonymous$$ySaveGame");
 Debug.Log(SaveLoad.loadedGame.testString);

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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

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

Load and save file. IOS problem 0 Answers

How do I remove missing Assets from Build / Import (Reimport All) ? 5.2.2.2f1 0 Answers

How do I pass a parameter for trigger animations in the animator controller unity 5 file 0 Answers

Editing Animation creates .bck file 0 Answers

Showing file extension on Project tab 2 Answers


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