Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Best way to save name and score

Discussion in 'Scripting' started by boowman, Aug 2, 2014.

  1. boowman

    boowman

    Joined:
    Jan 3, 2014
    Posts:
    57
    Hello
    I was wondering what is the best way of saving the players name,points,score whatever else I need.
    I was going to use PlayerPref but I just found out that players can edit them since they are mostly used for testing/debug purposes.
    I also thought of using databases with php but not sure how I could retrieve from the database only certain columns.
    For example I create a table that holds (name,points,score), how I would be able to put in a GUI.Label only the score without the name and points.
    Also if I use PlayerPref how easy would it be to edit them and how are other games(or your game) storing things like this?

    PS:It's for an android game.
     
  2. GarthSmith

    GarthSmith

    Joined:
    Apr 26, 2012
    Posts:
    1,240
    Anything you use will generally be super easy to hack unless you encrypt the data. Then it's still hackable but much harder.

    Easy option: Save a hash of your save data. When you load the data, generate a hash again. If the hash has changed, then you know the save data has been tampered with.

    Pro option: Use the Cryptography namespace to encrypt your data. Then write the encrypted data to a JSON file or something similar.
     
  3. hammil

    hammil

    Joined:
    Jun 5, 2013
    Posts:
    56
    The traditional way is to do something like this:
    Code (CSharp):
    1. using System.Runtime.Serialization.Formatters.Binary;
    2. using System.IO;
    3.  
    4. [Serializable]
    5. class MyUserData
    6. {
    7.     public int score;
    8.     public string name;
    9. }
    10.  
    11. void SaveGame(MyUserData data, string filePath)
    12. {
    13.     var fp = File.OpenWrite(filePath);
    14.     var serializer = new BinaryFormatter();
    15.     serializer.Serialize(fp, data);
    16.     fp.Close();
    17. }
    18.  
    19. MyUserData LoadGame(string filePath)
    20. {
    21.     var fp = File.OpenRead(filePath);
    22.     var serializer = new BinaryFormatter();
    23.     var ret = serializer.Deserialize(filePath) as MyUserData;
    24.     return ret;
    25. }
    26.  
    However, I'm unsure if this will work on android - feel free to give it a shot, though.
     
    GarthSmith likes this.
  4. GarthSmith

    GarthSmith

    Joined:
    Apr 26, 2012
    Posts:
    1,240
    Hammil's option is much easier! And it'll block anyone who doesn't know how to use a hex editor quite well. And if they can use a hex editor... how secure do you really need to keep a score?
     
  5. boowman

    boowman

    Joined:
    Jan 3, 2014
    Posts:
    57
    Well so far I don't really need to keep anything secured since the game is singleplayer and you can only enter your name and the score is entered at the end of the round.
    But further on I want to add score and with the score you will be able to customize the character.
     
  6. boowman

    boowman

    Joined:
    Jan 3, 2014
    Posts:
    57
    Also how would you edit the PlayerPref?
    I found my data folder and inside I had com.<CompanyName>.<GameName> which are the ones I gave to my game and I found a cache file. Is the what you need to edit for PlayerPref?
     
  7. boowman

    boowman

    Joined:
    Jan 3, 2014
    Posts:
    57
    Where are the files going to be saved?
     
  8. GarthSmith

    GarthSmith

    Joined:
    Apr 26, 2012
    Posts:
    1,240
    boowman likes this.