Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • 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 iFuzii · Feb 19, 2017 at 11:08 PM · loading filesave sceneload levelsave game

How to Save and Load a Level by Serialization

Hello and thanks for coming to my question. I am currently developing a 2D platformer with 4 levels and I want to make it so that when a player beats a level and moves onto the next level, it saves the fact that they made it to the next level so that when they start the game again, they will be at the level that they got to. I want to have it so that the saving automatically saves once you beat the level and so that you can click load on the start screen to load the last scene that you made it to(I soon want to add a level select menu). I want to be able to do this through serialization and I tried it but it did not work because I was trying to do it based off of the player's position in the scene but the player position is different throughout scenes. All help will be greatly appreciated.

Comment
Add comment · Show 1
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 Cherno · Feb 20, 2017 at 12:06 AM 0
Share

Either have each scene keep some information on the player start position, for example by having a dedicated player start gameobject at the right position that you fin at scene start via GameObject.Find and similar, or by have a file that stores all the player start positions for each scene which you load into memory.

3 Replies

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

Answer by thePeine · Feb 20, 2017 at 12:17 AM

Personally, I think XML serialization is the easiest method to use while developing the game. It is obviously not as space efficient as a condensed binary format, but it's easy to visualize. It's also super convenient to be able to edit the XML file in a text editor, to test a variety of game states, without having to create them in game. Below is some code that should hopefully get you started. Just know when you intend to ship your game, you'd probably want to move to a more space-efficient format, like a custom binary stream.

 public void Save(GameData gd)
 {
     XmlSerializer ser = new XmlSerializer(typeof(GameData));
     using (var writer = new StreamWriter(File.Open("xmlsave.xml", FileMode.Create)))
     {
         ser.Serialize(writer, gd);
     }

 }
 public void Load()
 {
     var gd = new GameData();
     XmlSerializer ser = new XmlSerializer(typeof(GameData));
     using (var reader = new StreamReader(File.Open("xmlsave.xml", FileMode.Open)))
     {
         gd = ser.Deserialize(reader) as GameData;
     }
 }

Where in your case, a basic GameData would look something like

 public class GameData
 {
     public int Level { get; set; }
 } 


Comment
Add comment · 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
-1

Answer by EBhero · Feb 28, 2018 at 04:51 PM

If it's as simple as keeping a string or a float, use PlayerPrefs. https://docs.unity3d.com/ScriptReference/PlayerPrefs.html

Comment
Add comment · 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
0

Answer by CarterG81 · Feb 20, 2017 at 02:05 AM

The best thing to do is to just look into how to serialize data (a programmer skill).

It's actually surprisingly easy to do. I thought it would be challenging or difficult, but once I went through the documentation (not even a tutorial) I was shocked at how simple it was to Save/Load data to file.

https://msdn.microsoft.com/en-us/library/7ay27kt9(v=vs.110).aspx

Here's some of the types:

  • BinaryFormatter

  • XML (C# .net does this too)

  • Json (http://www.newtonsoft.com/json)

I have heard some users state that if the user of Binary/XML (.net) were to ever change their .Net version, it could potentially corrupt save data. This isn't something you really need to worry about, and I'm not sure how true it actually is in real life circumstances, but it is a yellow flag (some claim especially true on multiple platformers).

JSON however solves this problem.

  • It's highly readable

  • It's foolproof & crossplatform

I personally use BinaryFormatter because it's just really really easy. However that link to JSON.net also makes it look really easy too.


Anyway... understanding how Serialization works will make it pretty clear how you can handle anything dealing with saving/loading data. Believe me- it's worth learning.

Comment
Add comment · 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

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.

Follow this Question

Answers Answers and Comments

8 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

UnitySerializer not loading levels 0 Answers

How to make Advanced save and Load Files? 1 Answer

Create an autosave/checkpoint feature for my game? 1 Answer

Scene Saving In Between Runtimes 1 Answer

Go back and forth between procedurally generated levels? 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