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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Saving and loading

Discussion in 'Scripting' started by JaimieVos, Jul 18, 2015.

  1. JaimieVos

    JaimieVos

    Joined:
    Jun 21, 2015
    Posts:
    193
    How can i script that i can save a level when i click on a button and kan load it with clicking on a button. I want to make a code when i build this that it works when i close the game and start it again that he saved it.
     
  2. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,848
    It's not simple. You need to serialize whatever you consider to be important state in your level. There's no magic bullet. But here's a decent introduction to serialization.
     
  3. phoda

    phoda

    Joined:
    Nov 11, 2014
    Posts:
    384
  4. HandOfPriam

    HandOfPriam

    Joined:
    Aug 19, 2010
    Posts:
    34
    I've written and published a system like this on the Asset Store. Typically, if you're not using PlayerPrefs, you'd need a system that serializes each piece of data you want to save and writes them to disk. Both binary and XML serialization are provided by .NET, but there are some hurdles you'd need to clear. Unity's provided types (Vector3, Transform, etc) are not serializable by default and a full-featured save system is a pretty complex system to make. You basically need to convert an object to a memory stream, then serialize that stream to your format of choice.

    Unity PlayerPrefs are simpler, but they only support a pretty limited set of data by default (primitives and Unity types). You aren't able to change their default location, and they have a maximum size limit.

    Here's a link to PlayerPrefs: http://docs.unity3d.com/ScriptReference/PlayerPrefs.html
     
  5. JaimieVos

    JaimieVos

    Joined:
    Jun 21, 2015
    Posts:
    193
    Its now to hard for me maybe later not :).

    I checking now the Save(); but how does it works kan i press a button then do the Save(); and close the game and come 1 hour later back?
     
  6. JaimieVos

    JaimieVos

    Joined:
    Jun 21, 2015
    Posts:
    193
    I watched some video's and playerprefs are very simpel with saving score :).