Skip to content

Instantly share code, notes, and snippets.

@gkagm2
Last active June 12, 2019 08:36
Show Gist options
  • Save gkagm2/fef9dd192a4d69e0e6780d6c81f984cf to your computer and use it in GitHub Desktop.
Save gkagm2/fef9dd192a4d69e0e6780d6c81f984cf to your computer and use it in GitHub Desktop.
SimpleJSON사용예제
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.IO;
using SimpleJSON;
public class PlayerState : MonoBehaviour
{
public string name;
public int hp;
public int sp;
public int str;
public int spd;
private void Update()
{
if (Input.GetKeyDown(KeyCode.S))
{
Save();
}
if (Input.GetKeyDown(KeyCode.L))
{
Load();
}
}
public void Save()
{
JSONObject playerJson = new JSONObject();
playerJson.Add("Name", name);
playerJson.Add("SP", sp);
playerJson.Add("STR", str);
playerJson.Add("SPD", spd);
// position
JSONArray position = new JSONArray();
position.Add(transform.position.x);
position.Add(transform.position.y);
position.Add(transform.position.z);
playerJson.Add("Position", position);
Debug.Log(playerJson.ToString());
// save json in computer
string path = Application.persistentDataPath + "/PlayerSave.json";
File.WriteAllText(path, playerJson.ToString());
}
public void Load()
{
Debug.Log("1");
string path = Application.persistentDataPath + "/PlayerSave.json";
Debug.Log("2");
string jsonString = File.ReadAllText(path);
Debug.Log("3");
JSONObject playerJson = (JSONObject)JSON.Parse(jsonString);
Debug.Log("4");
// set values
name = playerJson["Name"];
hp = playerJson["HP"];
sp = playerJson["SP"];
str = playerJson["STR"];
spd = playerJson["SPD"];
// position
transform.position = new Vector3(playerJson["Position"].AsArray[0], playerJson["Position"].AsArray[1], playerJson["Position"].AsArray[2]);
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.IO;
using SimpleJSON;
public class PlayerState : MonoBehaviour
{
public string name;
public int hp;
public int sp;
public int str;
public int spd;
private void Update()
{
if (Input.GetKeyDown(KeyCode.S))
{
Save();
}
if (Input.GetKeyDown(KeyCode.L))
{
Load();
}
}
public void Save()
{
JSONObject playerJson = new JSONObject();
playerJson.Add("Name", name);
playerJson.Add("SP", sp);
playerJson.Add("STR", str);
playerJson.Add("SPD", spd);
// position
JSONArray position = new JSONArray();
position.Add(transform.position.x);
position.Add(transform.position.y);
position.Add(transform.position.z);
playerJson.Add("Position", position);
Debug.Log(playerJson.ToString());
// save json in computer
string path = Application.persistentDataPath + "/PlayerSave.json";
File.WriteAllText(path, playerJson.ToString());
}
public void Load()
{
Debug.Log("1");
string path = Application.persistentDataPath + "/PlayerSave.json";
Debug.Log("2");
string jsonString = File.ReadAllText(path);
Debug.Log("3");
JSONObject playerJson = (JSONObject)JSON.Parse(jsonString);
Debug.Log("4");
// set values
name = playerJson["Name"];
hp = playerJson["HP"];
sp = playerJson["SP"];
str = playerJson["STR"];
spd = playerJson["SPD"];
// position
transform.position = new Vector3(playerJson["Position"].AsArray[0], playerJson["Position"].AsArray[1], playerJson["Position"].AsArray[2]);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment