How to Create Write and Read a file in iphone without using playerprefs in unity3d

H

it is not that easy to read or write file in iphone we have to give the specific path in order to read or write.
here is the sample code

using UnityEngine;
using System.Collections;
usingĀ System.IO;
using System.Text;

public static class GameManager
{
private static string path;

public static void starting ()
{
    path = Application.dataPath.Substring (0, Application.dataPath.Length - 20 )+"Documents";
}

//---------------------file handleres------------------------------

public static void writeToFile(string filename , int value)
{
    StreamWriter sw = new StreamWriter(path+"/"+filename);
    string temp = value.ToString();
    sw.Write("");
    sw.WriteLine(temp);
    sw.Close();
}
public static int ReadFromFile(string filename )
{
    StreamReader sr = new StreamReader(path+"/"+filename);
    string temp = sr.ReadLine();
    int anothertemp = int.Parse(temp);
    sr.Close();
    return anothertemp;
}

//---------------------file handleres------------------------------

}

Each app only have access to “Documents” folder under it. That’s the place we could use to write or read or create.

About the author

mohan.rao
By mohan.rao

Category