Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 glae · Oct 06, 2012 at 07:33 PM · gamesaveload

Save and Load Game

Hi guys.I want to ask something about save and load game. In my game there are two level and I did scene that you can choose level. And I want to if you don't complete the first level , you can't choose second level. And you complete the first level the game save outomaticly and you can choose second level another time in scene. How can I do? Thank you in advance. (If you can, please javascript)

Comment
Add comment · Show 5
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 DannyB · Oct 06, 2012 at 08:33 PM 0
Share

Are you talking about saving a lot of data, like what objects the player has collected etc., or just to save the fact that the player has completed the level?

avatar image glae · Oct 07, 2012 at 11:07 AM 0
Share

Yes I want to saving a lot of data, like time and score

avatar image DannyB · Oct 07, 2012 at 11:17 AM 0
Share

Time and score is not a lot. An array of all objects in the game, along with their state - that is a lot.

The reason for me asking was that if you just want to maintain some data elements between game sessions, like score, time, highest level achieved and so on - the easiest way to do it is to use PlayerPrefs.

avatar image glae · Oct 07, 2012 at 11:59 AM 0
Share

Okey but how can I use PlayerPrefs ? For now I want to do only level control. Namely if you complete first level , you can play second level after open the game(you can choose 2nd level in scene of level)if you don't , you can't choose

avatar image DannyB · Oct 07, 2012 at 12:14 PM 0
Share

I have added an answer below. For starters, I would recommend you create a new empty script, and play with PlayerPrefs a little to understand how it works. Then you should be able to easily see how (and if) it fits your need.

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by tw1st3d · Oct 06, 2012 at 10:37 PM

You will need to learn about a specific function, known as UTF8Encoding();

This isn't exactly what you want, but this IS a load/save script.

 import System;
 import System.Collections;
 import System.Xml;
 import System.Xml.Serialization;
 import System.IO;
 import System.Text;
 
 class TempData
 {
     var x : float;
     var y : float;
     var z : float;
     var name : String;
 }
 
 class UsernameData
 {
    public var _iUser : TempData = new TempData();
    function UsernameData() { }
 }
 private var _Save : Rect;
 private var _Load : Rect;
 private var _SaveMSG : Rect;
 private var _LoadMSG : Rect;
 private var _FileLocation : String;
 private var _FileName : String = "SaveData.xml";
 
 var _Player : GameObject;
 var _PlayerName : String = "Joe Schmoe";
 
 private var myData : UsernameData;
 private var _data : String;
 
 private var VPosition : Vector3;
 
 function Awake () { 
       _FileLocation=Application.dataPath;
       myData=new UsernameData();
 }
 
 function UTF8ByteArrayToString(characters : byte[] )
 {     
    var encoding : UTF8Encoding  = new UTF8Encoding();
    var constructedString : String  = encoding.GetString(characters);
    return (constructedString);
 }
 
 function StringToUTF8ByteArray(pXmlString : String)
 {
    var encoding : UTF8Encoding  = new UTF8Encoding();
    var byteArray : byte[]  = encoding.GetBytes(pXmlString);
    return byteArray;
 }
 function SerializeObject(pObject : Object)
 {
    var XmlizedString : String  = null;
    var memoryStream : MemoryStream  = new MemoryStream();
    var xs : XmlSerializer = new XmlSerializer(typeof(UsernameData));
    var xmlTextWriter : XmlTextWriter  = new XmlTextWriter(memoryStream, Encoding.UTF8);
    xs.Serialize(xmlTextWriter, pObject);
    memoryStream = xmlTextWriter.BaseStream;
    XmlizedString = UTF8ByteArrayToString(memoryStream.ToArray());
    return XmlizedString;
 }
 function DeserializeObject(pXmlizedString : String)   
 {
    var xs : XmlSerializer  = new XmlSerializer(typeof(UsernameData));
    var memoryStream : MemoryStream  = new MemoryStream(StringToUTF8ByteArray(pXmlizedString));
    var xmlTextWriter : XmlTextWriter  = new XmlTextWriter(memoryStream, Encoding.UTF8);
    return xs.Deserialize(memoryStream);
 }
 function CreateXML()
 {
    var writer : StreamWriter;
    var t : FileInfo = new FileInfo(_FileLocation+"/"+ _FileName);
    if(!t.Exists)
    {
       writer = t.CreateText();
    }
    else
    {
       t.Delete();
       writer = t.CreateText();
    }
    writer.Write(_data);
    writer.Close();
    Debug.Log("File written.");
 }
    
 function LoadXML()
 {
    var r : StreamReader = File.OpenText(_FileLocation+"/"+ _FileName);
    var _info : String = r.ReadToEnd();
    r.Close();
    _data=_info;
    Debug.Log("File Read");
 }

Best of luck.

Comment
Add comment · Show 1 · 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 glae · Oct 07, 2012 at 11:07 AM 0
Share

Thank you I try it

avatar image
0

Answer by DannyB · Oct 07, 2012 at 12:12 PM

Based on your specific use case and the comments to the question, I would like to suggest PlayerPrefs.

When the user completes the level, you save the highest unlocked level, like this:

 PlayerPrefs.SetInt("HighestLevel", 2);


When you load the game, you check what is the highest unlocked level, like this:

 HighestLevel = PlayerPrefs.GetInt("HighestLevel", 1);


The second argument is optional, to say what is the default value.

Finally, in your screen where the player can choose a level to play, you check against this HighestLevel variable to see which buttons you need to display.

In the same way of course, you can store the score instead of (or in addition to-) the highest level.

Lastly, here is a quick link to the PlayerPrefs documentation

Let me know if something is not clear.

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

11 People are following this question.

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

Related Questions

Load last checkpoint from main menu 2 Answers

Save game and load 2 Answers

Save/ Load game script in main menu 1 Answer

The name "Game Foundation" does not exist in the current context 1 Answer

saving the game in position 1 Answer


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