Skip to content

Commit 0c8795f

Browse files
committed
Adds JsonData to import static JSON files
1 parent e65faa8 commit 0c8795f

File tree

5 files changed

+156
-4
lines changed

5 files changed

+156
-4
lines changed

Assets/_Project/Scenes/tally-viz.unity

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1668,6 +1668,7 @@ GameObject:
16681668
- component: {fileID: 481131867}
16691669
- component: {fileID: 481131868}
16701670
- component: {fileID: 481131869}
1671+
- component: {fileID: 481131870}
16711672
m_Layer: 0
16721673
m_Name: DataManager
16731674
m_TagString: Untagged
@@ -1719,6 +1720,20 @@ MonoBehaviour:
17191720
eventCountText: {fileID: 2073581459}
17201721
playerCountText: {fileID: 140658078}
17211722
eventNumberText: {fileID: 1315012613}
1723+
--- !u!114 &481131870
1724+
MonoBehaviour:
1725+
m_ObjectHideFlags: 0
1726+
m_CorrespondingSourceObject: {fileID: 0}
1727+
m_PrefabInstance: {fileID: 0}
1728+
m_PrefabAsset: {fileID: 0}
1729+
m_GameObject: {fileID: 481131866}
1730+
m_Enabled: 1
1731+
m_EditorHideFlags: 0
1732+
m_Script: {fileID: 11500000, guid: 521923641a68f46a1bd04e8613dd29c9, type: 3}
1733+
m_Name:
1734+
m_EditorClassIdentifier:
1735+
gradientsByMidFile: {fileID: 4900000, guid: f4a676a691655430d953af115d057ffe, type: 3}
1736+
monstersByMidFile: {fileID: 4900000, guid: a7352c8bcc0a14491abce1b3efc6f528, type: 3}
17221737
--- !u!1 &489705912
17231738
GameObject:
17241739
m_ObjectHideFlags: 0
@@ -2307,7 +2322,7 @@ MonoBehaviour:
23072322
m_HandleRect: {fileID: 489705913}
23082323
m_Direction: 2
23092324
m_Value: 0
2310-
m_Size: 0.50000036
2325+
m_Size: 0.9860249
23112326
m_NumberOfSteps: 0
23122327
m_OnValueChanged:
23132328
m_PersistentCalls:
@@ -4614,7 +4629,7 @@ RectTransform:
46144629
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
46154630
m_AnchorMin: {x: 0, y: 1}
46164631
m_AnchorMax: {x: 1, y: 1}
4617-
m_AnchoredPosition: {x: 0, y: -96.25155}
4632+
m_AnchoredPosition: {x: 0, y: -99.99958}
46184633
m_SizeDelta: {x: 0, y: 0}
46194634
m_Pivot: {x: 0.5, y: 0.5}
46204635
--- !u!114 &1617634874

Assets/_Project/Scripts/Data/DataManager.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -110,14 +110,15 @@ public void GetNewData ()
110110
public static IEnumerator GetRequest (string uri)
111111
{
112112
using (UnityWebRequest webRequest = UnityWebRequest.Get (uri)) {
113+
113114
// Request and wait for the desired page.
114115
yield return webRequest.SendWebRequest ();
115116

116117
//string [] pages = uri.Split ('/');
117118
//int page = pages.Length - 1;
118119

119-
if (webRequest.isNetworkError) {
120-
Debug.Log ("Error: " + webRequest.error);
120+
if (webRequest.isNetworkError || webRequest.isHttpError) {
121+
Debug.Log (DebugManager.GetSymbol ("asterisk") + " Error: " + webRequest.error);
121122
} else {
122123
Debug.Log (DebugManager.GetSymbol ("asterisk") + " DataManager.GetNewData() " +
123124
uri + "\n" + webRequest.downloadHandler.text);
Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
using UnityEngine;
2+
using Newtonsoft.Json.Linq;
3+
using System.Collections.Generic;
4+
5+
6+
/**
7+
* Deserialize external JSON file using Newtonsoft.Json
8+
* - File is located in Assets/ and referenced using TextAsset in Inspector
9+
* - JSON is an object of objects so code uses JObject
10+
* - Stores objects in Dictionary for later use
11+
*/
12+
13+
14+
// define a custom class to hold each JSON object in the data returned
15+
[System.Serializable]
16+
public class GradientsByMidPoint {
17+
public int mid { get; set; }
18+
public string hex1 { get; set; }
19+
public string hex2 { get; set; }
20+
public int tier1 { get; set; }
21+
}
22+
[System.Serializable]
23+
public class MonstersByMidPoint {
24+
public int mid { get; set; }
25+
public string slug { get; set; }
26+
public int tier1id { get; set; }
27+
public int parent { get; set; }
28+
public string name { get; set; }
29+
public string tags { get; set; }
30+
public float accuracy { get; set; }
31+
public float attack { get; set; }
32+
public float defense { get; set; }
33+
public float evasion { get; set; }
34+
public float health { get; set; }
35+
public float stamina { get; set; }
36+
public int cp { get; set; }
37+
}
38+
39+
40+
41+
42+
43+
44+
public class JsonData : MonoBehaviour {
45+
46+
// GradientsByMid
47+
public TextAsset gradientsByMidFile;
48+
public Dictionary<string, GradientsByMidPoint> GradientsByMid;
49+
50+
// MonstersByMid
51+
public TextAsset monstersByMidFile;
52+
public Dictionary<string, MonstersByMidPoint> MonstersByMid;
53+
54+
55+
void Awake ()
56+
{
57+
// GradientsByMid
58+
GradientsByMid = new Dictionary<string, GradientsByMidPoint> ();
59+
ParseGradientsByMid (gradientsByMidFile.text);
60+
61+
// MonstersByMid
62+
MonstersByMid = new Dictionary<string, MonstersByMidPoint> ();
63+
ParseMonstersByMid (monstersByMidFile.text);
64+
}
65+
66+
67+
void ParseGradientsByMid (string text)
68+
{
69+
// parse the string as JObject
70+
JObject jsonData = JObject.Parse (text);
71+
72+
print ("---- GradientsByMidPoint -> count: " + jsonData.Count);
73+
74+
// loop through each, accessing it as a string:JToken
75+
foreach (KeyValuePair<string, JToken> item in jsonData) {
76+
//// the key and value
77+
//Debug.Log ("key/val = " + item.Key + " -->" + item.Value);
78+
//// access a value on the object
79+
//Debug.Log (jsonData [item.Key].Value<string> ("hex1"));
80+
81+
// store the value in a dict
82+
GradientsByMid.Add (item.Key, item.Value.ToObject<GradientsByMidPoint> ());
83+
}
84+
}
85+
86+
87+
void ParseMonstersByMid (string text)
88+
{
89+
// parse the string as JObject
90+
JObject jsonData = JObject.Parse (text);
91+
92+
print ("---- MonstersByMidPoint -> count: " + jsonData.Count);
93+
94+
// loop through each, accessing it as a string:JToken
95+
foreach (KeyValuePair<string, JToken> item in jsonData) {
96+
//// the key and value
97+
//Debug.Log ("key/val = " + item.Key + " -->" + item.Value);
98+
//// access a value on the object
99+
//Debug.Log (jsonData [item.Key].Value<string> ("hex1"));
100+
101+
// store the value in a dict
102+
MonstersByMid.Add (item.Key, item.Value.ToObject<MonstersByMidPoint> ());
103+
}
104+
}
105+
106+
107+
108+
}

Assets/_Project/Scripts/Data/JsonData.cs.meta

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Assets/_Project/Scripts/Players/PlayerManager.cs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,8 @@ private void Awake ()
6767
*/
6868
public void ResetPlayers ()
6969
{
70+
// remove any players first?
71+
7072
// clear the dictionary
7173
playerDict.Clear ();
7274

@@ -78,11 +80,26 @@ public void ResetPlayers ()
7880
// update player count
7981
playerCount = playerDict.Count;
8082

83+
// trigger data updated event
84+
EventManager.TriggerEvent ("PlayersUpdated");
85+
}
86+
87+
/**
88+
* Remove players who haven't been active in a while
89+
*/
90+
public void ClearNonActivePlayers ()
91+
{
92+
8193

8294
// trigger data updated event
8395
EventManager.TriggerEvent ("PlayersUpdated");
8496
}
8597

98+
99+
100+
101+
102+
86103
/**
87104
* Create a new player
88105
*/

0 commit comments

Comments
 (0)