- Home /
save and load json score to web/server
hello all, a somewhat newbie to unity, i'm looking to create a simple educational maze game for my workplace and all i wish to do is save the score to a location on the web or server. Presently it saves perfectly locally as a txt/json file. Any hints on adapting existing below script for save/load to web/url ? Ive been reading up on www functions and IEnumerators but i cant get it to work. thanks in advance.
public Playerdata playerscore;
private string scorepath;
void Start () {
scorepath = Path.Combine (Application.dataPath, "score.txt");
//Save ();
}
// Update is called once per frame
void Update () {
if (Input.GetKey(KeyCode.S))
Save ();
if (Input.GetKey(KeyCode.L))
Load ();
}
void Save () {
string jsonscorekeep = JsonUtility.ToJson (playerscore);
File.WriteAllText (scorepath,jsonscorekeep);
}
void Load () {
string jsonscorekeep = File.ReadAllText (scorepath);
JsonUtility.FromJsonOverwrite (jsonscorekeep, playerscore);
}
}
Hi, most likely you need script on server side handles send request. How you handle is up to you. I found example on Unity docs, see 2nd sample from fallowing link:
https://docs.unity3d.com/ScriptReference/WWWForm.html
You should refrain from connecting to database directly from your game script, however you can save your data that way either.
Answer by Skylander17 · Jul 04, 2018 at 05:03 AM
You need 2 sided coding. One side is unity. Other side is server. On Unity side, you should try connect to server and talk with it. On Server side, you should listen any incoming communication request and answer them.
If you don't want to write server coding or something, there are other basic options. Options which I know:
Using normal http web server. Like google.com or unity.com. And write some php or asp code to communucate with unity program.
Firebase Services. You need plugin for unity to use this.
These are basic solutions. If you want hard way. You should rent a server and install your server side platforms and code it to answer for incoming messages. And deal with attacks like ddos or hacking.
thank you for your reply Skylander17, i guess i'm in over my head with this one!...i think a bit of research is in order for me, but i struggle finding beginner tutorials. The basic option you mention sounds just right for my simple purposes, so ill look into those "normal http web server" options you suggested, after all this is just an internal project and I just wanted to save the highest score to my work http address using xml or json.