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

Save large chunks of data

Discussion in 'Scripting' started by Vanz, Oct 15, 2014.

  1. Vanz

    Vanz

    Joined:
    Nov 19, 2013
    Posts:
    374
    What's the best way to save and retrieve large chunks of data in Unity/c#?
     
  2. RazorCut

    RazorCut

    Joined:
    May 7, 2009
    Posts:
    393
    From memory, local storage, the internet or what?
     
  3. Vanz

    Vanz

    Joined:
    Nov 19, 2013
    Posts:
    374
    Local store game data, save game... later to be loaded. Not internet...
     
  4. GarthSmith

    GarthSmith

    Joined:
    Apr 26, 2012
    Posts:
    1,240
  5. RazorCut

    RazorCut

    Joined:
    May 7, 2009
    Posts:
    393
    The C# File class is the most generic thing to do. On mobile you'd want to be reading and writing data to Application.persistentDataPath; perhaps it's the same on non-mobile, but I'm not sure. How you format the files is up to you and would depend on your needs, e.g. raw binary or text.
     
  6. Vanz

    Vanz

    Joined:
    Nov 19, 2013
    Posts:
    374
    Thanks RazorCut!

    Do you have any code for this?

    I've found:
    http://docs.unity3d.com/ScriptReference/Application-persistentDataPath.html
    http://forum.unity3d.com/threads/saving-and-loading-data-xmlserializer.85925/

    I would want to store a large array of integers to a file then be able to fetch them again (save/load game for player data).
     
  7. PolymorphiK Games

    PolymorphiK Games

    Joined:
    Oct 5, 2014
    Posts:
    51
    Is the data sensitive? When you save data locally, you want to make sure you encrypt it or do some sort of obfuscation to prevent "hackers" from getting to the data and changing values at will. Also, I recommend Application.dataPath, as it saves it internally where the application is saved at. Application.persistantDataPath is nice but if you are working in windows / unix clearing caches can also clear the game data. The only way the data can be cleared with Application.dataPath is by manually deleting with code or by removing the game itself.
     
  8. ZO5KmUG6R

    ZO5KmUG6R

    Joined:
    Jul 15, 2010
    Posts:
    489
    If it's non-sensitive data you can use a BinaryReader and BinaryWriter class. They are nice and fast for saving and loading data :)
    Code (CSharp):
    1. using System.IO;
    2.  
    3. //Save and load player health, and ammo
    4. int ammo = 0;
    5. int health = 100;
    6.  
    7. void Save(){
    8.      string savePath = Application.persistentDataPath + "/savefile.sav";
    9.      using(var w = new BinaryWriter(File.OpenWrite(savePath))){
    10.        w.Write(System.Convert.ToInt32(ammo)); //Write ammo as Int32 type
    11.        w.Write(System.Convert.ToInt32(health)); //Write health as Int32 type
    12.      }
    13.      Debug.Log("Save complete");
    14.    }
    15.    void Load(){
    16.      string loadPath = Application.persistentDataPath + "/savefile.sav";
    17.      using(var r = new BinaryReader(File.OpenRead(loadPath))){
    18.        //Load ammo and health
    19.        ammo = r.ReadInt32();
    20.        health = r.ReadInt32();
    21.      }
    22.      Debug.Log("Load complete");
    23.    }
    24.  
    25. void Start(){
    26.     health = 25;
    27.     Save();
    28.     health = 100;
    29.     Load();
    30.     Debug.Log("Health is " + health); //Will log 25
    31. }
     
    Last edited: Oct 16, 2014
  9. Vanz

    Vanz

    Joined:
    Nov 19, 2013
    Posts:
    374
    Thanks aaro4130! Much appreciated...
     
  10. ZO5KmUG6R

    ZO5KmUG6R

    Joined:
    Jul 15, 2010
    Posts:
    489
    Some tips for reading and writing Binary data are :

    -If you have a number that will NEVER be over 32767, you can use Int16. Or if it will never be > 255, you can use Byte
    -If you have a boolean you can use ReadBoolean and Write(<bool value>)
    -You can read and write strings directly too

    Happy coding :)
     
  11. Vanz

    Vanz

    Joined:
    Nov 19, 2013
    Posts:
    374
    Thanks again aaro4130, one more question what are the limits on the size of the array I can use?

    For example say I want to deploy to Windows Tablet vs. iPhone, how can I determine max size of array for each device? Maybe when deploying to certain devices I then make my large data size a bit smaller (it's a tiled region of exploration in a game)...
     
  12. ZO5KmUG6R

    ZO5KmUG6R

    Joined:
    Jul 15, 2010
    Posts:
    489
    Data on each device can be stored in this same way with usually the same size of data even. You shouldn't run into any issues even with large blocks . Just loading time will be longer on slower devices
     
  13. PolymorphiK Games

    PolymorphiK Games

    Joined:
    Oct 5, 2014
    Posts:
    51
    2^n - 1, where n is the bit of the OS, 2^32 - 1 is the biggest int size for a 32 bit OS 2174483647 is the biggest int value. So, your array cannot exceed that size if it is a 32 bit OS.
     
  14. DeveshPandey

    DeveshPandey

    Joined:
    Sep 30, 2012
    Posts:
    221
  15. AeroGames-GamesForFree

    AeroGames-GamesForFree

    Joined:
    Jan 3, 2019
    Posts:
    3
    will this work for a 2d Minecraft word. I want to store 3 values. X,Y coordinates(Not sure if I spelled it right) and an int for the block type. The world is 1000x40 blocks long. Will this be quick because with other methods my pc crashes or takes like an hour.