unity save data for phone code example
Example: how to save data on cellphone unity android
using System.IO public class scoreFile { private int[] level; private bool[] completed; private float[] completionTime; private string[] scores; private string fileName; //This class stores information for each of the levels. public scoreFile(int num) { fileName = "scores.txt"; try { if (!File.Exists(fileName)) { level = new int[num]; completionTime = new float[num]; completed = new bool[num]; scores = new string[num]; Debug.Log("Opened file!"); for (int i = 0; i < num; i++) { level[i] = i + 1; completionTime[i] = 0; completed[i] = false; scores[i] = level[i] + " " + 0 + " " + completionTime[i]; } Debug.Log("About to write into file!"); File.WriteAllLines(fileName, scores); } else { Debug.Log("File is exist! Loading!"); loadFile(); } } catch (System.Exception e) { Debug.Log(e); } } public bool isComplete(int i) { return completed[i]; } public int levels(int i) { return level[i - 1] + 1; } public float timeScore(int i) { return completionTime[i]; } public void loadFile() { Debug.Log("Reading"); string[] levelsInfo = File.ReadAllLines(fileName); int num = levelsInfo.Length; level = new int[num]; completionTime = new float[num]; completed = new bool[num]; for (int i = 0; i < num; i++) { //Debug.Log(levelsInfo[i]); string str = levelsInfo[i]; level[i] = (int)int.Parse(str.Substring(0, 1)); int f = int.Parse(str.Substring(2, 1)); if (f == 0) completed[i] = false; else completed[i] = true; completionTime[i] = (float)float.Parse(str.Substring(4)); } } }