Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Basic Practices for Saving in Unity

Discussion in 'Scripting' started by AwDogsGo2Heaven, Jan 18, 2014.

  1. AwDogsGo2Heaven

    AwDogsGo2Heaven

    Joined:
    Jan 17, 2014
    Posts:
    102
    I know most people use PlayerPrefs, but what is the best practice to follow for saving? Should each game object have a save and load function thats called on entering and exiting? Or is there a better way to do this?
     
  2. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    PlayerPrefs are for saving preferences mostly (hence the name, and the limited data types it works with), so I doubt that's what most people use for saving games. As a start, look into serialization.

    --Eric
     
  3. MDragon

    MDragon

    Joined:
    Dec 26, 2013
    Posts:
    329
    It depends on how complex your saving/loading is. Serialization or assets (in the store, I mean) are your best choices for more complicated things. However, for simpler things, you can use PlayerPrefs.

    At the moment - after my "massive" ordeal figuring out PlayerPrefs, ScriptableObjects and Unity's built-in serialization - I'm using both ScriptableObjects/built-in Serialization and PlayerPrefs... although I may not be using it the same way as everybody else. The ScriptableObjects simply hold data of each level (id, name, number of items in level, this or that that are constant and vary between levels) and the PlayerPrefs hold simple but variable data between levels (high scores, top time, unlocked, etc).

    Edit: Here ya go:
    http://forum.unity3d.com/threads/222602-Best-way-to-save-and-load-data-(simpler-medium-complexity)
    Guess it helps to save these old threads :D
     
    Last edited: Jan 18, 2014
  4. AwDogsGo2Heaven

    AwDogsGo2Heaven

    Joined:
    Jan 17, 2014
    Posts:
    102
    My first thought was to serialize my objects into JSON strings and save it into PlayerPrefs, but wasn't sure if that is a good idea.
     
  5. Dustin-Horne

    Dustin-Horne

    Joined:
    Apr 4, 2013
    Posts:
    4,568
    If you're going to serialize them you might as well just write to disk/device. You can use the Application.PersistentDataPath property to get the save path and write the file there.
     
  6. AwDogsGo2Heaven

    AwDogsGo2Heaven

    Joined:
    Jan 17, 2014
    Posts:
    102
    Does anyone know of any libraries that can serialize objects into JSON using reflection rather than attributes/annotations? Basically, I want to be lazy.
     
  7. Dustin-Horne

    Dustin-Horne

    Joined:
    Apr 4, 2013
    Posts:
    4,568