hi, i have seen PlayerPrefs,
but im making a game like Minecraft.
and storing the save in the registry isn’t a good idea for this…
im searching way to store the save in PLAIN FILES.
any help? ![]()
If building for Mac or PC, look up System.IO and how to save text files, plenty of examples.
I made this quickly a few days ago. Very very simple but you can learn from it and make a more advanced one that suits your needs.
Code:
public class SimpleSettings {
private StreamReader fileRead;
private StreamWriter fileWrite;
private string filePath;
private bool saveRequired;
private Dictionary<string,string> data = new Dictionary<string,string>();
public SimpleSettings(string path)
{
filePath = path;
LoadData();
}
private void LoadData()
{
if (File.Exists(filePath))
{
fileRead = new StreamReader(filePath);
string readData = "";
string key;
string val;
while (fileRead.Peek() >= 0)
{
readData = fileRead.ReadLine();
if (readData.Contains("="))
{
key = readData.Substring(0,readData.IndexOf("="));
if (readData.Length > key.Length + 1)
{
val = readData.Substring(key.Length+1,readData.Length - (key.Length + 1));
data.Add(key,val);
}
}
}
fileRead.Close();
fileRead.Dispose();
}
}
public void Save()
{
if (saveRequired)
{
fileWrite = new StreamWriter(filePath,false);
foreach (string s in data.Keys)
{
fileWrite.WriteLine(s + "=" + data[s]);
}
fileWrite.Close();
fileWrite.Dispose();
saveRequired = false;
}
}
public string GetValue(string key)
{
if (data.ContainsKey(key))
{
return data[key];
}
else
{
return "";
}
}
public void SetValue(string key, string val)
{
if (data.ContainsKey(key))
{
if (data[key] != val)
{
data[key] = val;
saveRequired = true;
}
}
else
{
data.Add(key,val);
saveRequired = true;
}
}
}
Usage:
SimpleSettings settings = new SimpleSettings(Application.dataPath + "/settings.txt");
settings.SetValue("Hello","World!");
string returnValue = settings.GetValue("Hello!");
thx will try