Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using System.IO;
- public class jsonParse : MonoBehaviour {
- //Variables.\\
- string path;
- string jsonString;
- //Run on first frame.\\
- void Start ()
- {
- //Gives path the path of "streamingAssets/Creature.json".\\
- path = Application.streamingAssetsPath + "/Creature.json";
- //Reads all of the text within the json file then closes it.\\
- jsonString = File.ReadAllText (path);
- //Create a creature named Akoi.\\
- Creature Akoi = JsonUtility.FromJson<Creature> (jsonString);
- //Acess different variables through creature for debugging/testing purposes.\\
- //If Json file is parsed correctly this will read out Akoi in your console.\\
- Debug.Log (Akoi.Name);
- //If Json file is parsed correctly this will read out 7 in your console.\\
- Debug.Log (Akoi.Level);
- //If Json file is parsed correctly this will read out 4,7 in your console.\\
- Debug.Log (Akoi.Stats);
- }
- }
- //Creates a new class within this script, this can be done through a seperate script if need be. However for the purpose of this we will just do it this way.\\
- [System.Serializable]
- public class Creature
- {
- //Use variables rather than properties as variables are the only thing supported by Unity currently.\\
- public string Name;
- public int Level;
- public int[] Stats;
- }
Advertisement
Add Comment
Please, Sign In to add comment