This is the project when learning about Json.
・Files similar to C# dictionaries, with a method of managing data by name and key.
・To use Json on Unity, you need to use the JsonUtility provided by Unity.
・Javascript originated
・Often used in unity with save functions.
{
//Tie the name and value to each other.
"name": "Makaroni",
"velocity": 12.5,
"isFlag": true,
}
//Can be managed in arrays.
{
"Userinfo": [
{
"id": 1,
"name": "saito",
"isHuman": true
},
{
"id": 2,
"name": "takahashi",
"isHuman": false
},
{
"id": 3,
"name": "monaka",
"isHuman": false
}
]
}
・Simple structure with only three functions.
- JsonUtility.FromJson //Converting Json data to Unity objects.
- JsonUtility.FromJsonOverwrite //overwrite function
- JsonUtility.ToJson() //Converting Unity objects to Json data.
//JSON -> Instance
//Read Json data in the Resources folder.
string json = Resources.Load<TextAsset>("Data1").ToString();
//Instantiate the data read in.
Data01 data01 = JsonUtility.FromJson<Data01>(json);
Debug.Log("name:"+data01.player[0].name+" age:"+data01.player[0].playerinfo.age);
//(JSON,Instance)
//JSON -> Instance
JsonUtility.FromJsonOverwrite(data01toJson, data02toJson);
Debug.Log(data02toJson);
//Instance -> JSON
string data01toJson = JsonUtility.ToJson(data01).ToString();
Debug.Log(Data01toJson);
・PlayerPrefs is not used.
・FromJsonOverwrite is not used, StreamWriter is used
・Use singletons because you want to share values across scenes
・File.WriteAllText(path, fileName) //Exporting files
・StreamReader streamReader = new StreamReader (filePath, character code) //Read file
streamReader.WriteLine(string)
・StreamWriter streamWriter = new StreamWriter (filePath, append : true) //Exporting files
・Method to write the data to be saved to a Json file.
・Methods to read data from Json files present in the Asset folder.
・Register the value you want to keep as Public.
・[System.Serializable] needs to be attached to the class as it is required for converting instances to Json.
[Serializable]
public class Data
{
public string name = "DefaultName";
public int id = -1 ;
}
*Saved data must be encrypted to prevent misuse by users(The way to do it is through binaries and so on.).
There are many different encryption methods, but this section introduces the use of XOR(The most famous encryption is the use of binaries and so on).
https://kan-kikuchi.hatenablog.com/entry/JsonUtility
https://www.hanachiru-blog.com/entry/2019/04/24/211642
https://docs.unity3d.com/ja/2018.4/ScriptReference/JsonUtility.html
https://kurokumasoft.com/2022/01/03/unity-savesystem-using-json/
https://www.youtube.com/watch?v=MZAU7HW4S8Q
https://www.slideshare.net/torisoup/unityzenject