Soifra

jsonParse.cs

Mar 5th, 2017
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.37 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using System.IO;
  5.  
  6. public class jsonParse : MonoBehaviour {
  7.  
  8.     //Variables.\\
  9.  
  10.     string path;
  11.     string jsonString;
  12.  
  13.     //Run on first frame.\\
  14.  
  15.     void Start ()
  16.     {
  17.         //Gives path the path of "streamingAssets/Creature.json".\\
  18.  
  19.         path = Application.streamingAssetsPath + "/Creature.json";
  20.  
  21.    
  22.         //Reads all of the text within the json file then closes it.\\
  23.  
  24.         jsonString = File.ReadAllText (path);
  25.  
  26.         //Create a creature named Akoi.\\
  27.  
  28.         Creature Akoi = JsonUtility.FromJson<Creature> (jsonString);
  29.  
  30.         //Acess different variables through creature for debugging/testing purposes.\\
  31.  
  32.         //If Json file is parsed correctly this will read out Akoi in your console.\\
  33.  
  34.         Debug.Log (Akoi.Name);
  35.  
  36.         //If Json file is parsed correctly this will read out 7 in your console.\\
  37.  
  38.         Debug.Log (Akoi.Level);
  39.  
  40.         //If Json file is parsed correctly this will read out 4,7 in your console.\\
  41.  
  42.         Debug.Log (Akoi.Stats);
  43.  
  44.     }
  45. }
  46.  
  47.  
  48. //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.\\
  49. [System.Serializable]
  50. public class Creature
  51. {
  52.     //Use variables rather than properties as variables are the only thing supported by Unity currently.\\
  53.  
  54.     public string Name;
  55.     public int Level;
  56.     public int[] Stats;
  57. }
Advertisement
Add Comment
Please, Sign In to add comment