Permalink
Switch branches/tags
Nothing to show
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
157 lines (119 sloc) 3.37 KB
using UnityEngine;
using System.Collections;
using System.IO;
using System;
using SimpleJSON;
public class ScriptDataStorage : MonoBehaviour {
public static ScriptDataStorage data;
public string URL;
string file;
void Start()
{
data = this;
file = Application.persistentDataPath +"/SavedSettings.json";
if(!File.Exists(file))
{
File.WriteAllText(file,"{}");
}
UpdateStringData("stringOrSomething","someValue");
UpdateFloatData("floatsAndStuff",12.34f);
UpdateIntData("floatsWithoutTheDecimals",1234);
}
/// <summary>
/// Updates the float data to the JSON File.
/// </summary>
/// <param name="keyName">Key name for e.g "Speed".</param>
/// <param name="FLOATvalue">value of that keyname like 10.</param>
public void UpdateFloatData(string keyName,float FLOATvalue)
{
StreamReader sr = new StreamReader(file);
string readtoend = sr.ReadToEnd();
sr.Close();
SimpleJSON.JSONNode node = SimpleJSON.JSONNode.Parse(readtoend);
node[keyName].AsFloat = FLOATvalue;
File.WriteAllText(file , node.ToString());
}
/// <summary>
/// Gets the float data.
/// </summary>
/// <returns>The float data.</returns>
/// <param name="keyName">Key name.</param>
public float GetFloatData(string keyName)
{
float tempValue = 0;
StreamReader sr = new StreamReader(file);
string readtoend = sr.ReadToEnd();
sr.Close();
JSONNode N = JSON.Parse(readtoend);
tempValue = float.Parse(N[keyName]);
return tempValue;
}
/// <summary>
/// Updates the int data.
/// </summary>
/// <param name="keyName">Key name.</param>
/// <param name="INTValue">INT value.</param>
public void UpdateIntData(string keyName,int INTValue)
{
StreamReader sr = new StreamReader(file);
string readtoend = sr.ReadToEnd();
sr.Close();
SimpleJSON.JSONNode node = SimpleJSON.JSONNode.Parse(readtoend);
node[keyName].AsInt = INTValue;
File.WriteAllText(file , node.ToString());
}
/// <summary>
/// Gets the int data.
/// </summary>
/// <returns>The int data.</returns>
/// <param name="keyName">Key name.</param>
public int GetIntData(string keyName)
{
int tempValue = 0;
StreamReader sr = new StreamReader(file);
string readtoend = sr.ReadToEnd();
sr.Close();
JSONNode N = JSON.Parse(readtoend);
tempValue = int.Parse(N[keyName]);
return tempValue;
}
/// <summary>
/// Updates the string data.
/// </summary>
/// <param name="keyName">Key name.</param>
/// <param name="STRINGvalue">STRIN gvalue.</param>
public void UpdateStringData(string keyName,string STRINGvalue)
{
StreamReader sr = new StreamReader(file);
string readtoend = sr.ReadToEnd();
sr.Close();
SimpleJSON.JSONNode node = SimpleJSON.JSONNode.Parse(readtoend);
node[keyName].Value = STRINGvalue;
File.WriteAllText(file, node.ToString());
}
/// <summary>
/// Gets the string data.
/// </summary>
/// <returns>The string data.</returns>
/// <param name="keyName">Key name.</param>
public string GetStringData(string keyName)
{
string tempValue = "";
StreamReader sr = new StreamReader(file);
string readtoend = sr.ReadToEnd();
sr.Close();
JSONNode N = JSON.Parse(readtoend);
tempValue = N[keyName];
return tempValue;
}
void GetUpdatedSavedSettingsFromInternet()
{
StartCoroutine(GetLatestJsonFile());
}
IEnumerator GetLatestJsonFile()
{
WWW textData = new WWW(URL);
yield return textData;
File.WriteAllText(file, textData.text.ToString());
}
}