- Home /
Problem Saving game progress
Hi, i'm new in Unity, and i have a question about saving game progress. I create a simple code to write on a text file to save Levels Number, so i do this -System.IO.File.WriteAllText();- it works perfectly in Unity, but once i want to build it for ios, i have an error message about AssetDatabase.Refresh(); unknown, when i remove this, my code didn't work anymore, i can load data information from the TextAsset file, but can't write or can't refresh the asset. So i wonder if this is the best way to do that, and if there is a solution to this.
PS : Working on 2D game for ios (not an online Game)
Thanks. Best regards
The Code :
import System.IO;
function Update() { if (Input.GetMouseButtonDown(0)) { var hit : RaycastHit2D = Physics2D.Raycast(Camera.main.ScreenToWorldPoint(Input.mousePosition), Vector2.zero); if(hit.collider.name == "Button-Enter") { System.IO.File.WriteAllText(Application.dataPath + "/Save-Levels.txt", "3"); AssetDatabase.Refresh(); } } }
Answer by fafase · May 05, 2014 at 09:08 AM
You cannot use PlayerPrefs?
https://docs.unity3d.com/Documentation/ScriptReference/PlayerPrefs.html
Sorry to ask this, what is that and i really don't know if i can use it or not. i read the URL, and it seems to be a kind of storage to save. PlayerPrefs.Save, is that what i should look for ? thanks again.
It is unity saving class. It saves information in some deep registry of your machine. I would also think it is encrypted. Good points, it works the same regardless the platform and makes hacking a tight little harder (though not impossible). Litle downside, you can only save int, float or string. But you can create a method to store many info in one string
"Pl:Name,L:100,W:Sw"
See this line above save the player's name, his life value and the weapon he is using (sword). When retrieving, you just parse the other way around.
Thanks that work. here is the script i used :
To set : PlayerPrefs.SetInt("SaveLevel", 1); To load level number : PlayerPrefs.GetInt("SaveLevel");
Thanks again
Your answer
Follow this Question
Related Questions
Drag and Drop menu and Save load game. 0 Answers
Unity load last level (save and load game) 0 Answers
General mobile game developing questions 0 Answers
Save Game - What do I need? 1 Answer
Save/ Load game script in main menu 1 Answer