10
10
using System . IO ;
11
11
using UnityEditor ;
12
12
13
- namespace JSONSerializerPackage
13
+ namespace JSON
14
14
{
15
15
[ Serializable ]
16
16
public class JSON
@@ -20,7 +20,7 @@ public class JSON
20
20
List < JSONFloat > floatList ;
21
21
List < JSONString > stringList ;
22
22
23
- TextAsset jsonFile ;
23
+ TextAsset jsonFile = null ;
24
24
25
25
public JSON ( )
26
26
{
@@ -97,6 +97,7 @@ void ParseJSON(TextAsset jsonFile)
97
97
{
98
98
string [ ] lineSplit = lines [ i ] . Split ( new string [ ] { ":" } , StringSplitOptions . None ) ;
99
99
lineSplit [ 0 ] = lineSplit [ 0 ] . Replace ( " \" " , "" ) ;
100
+ lineSplit [ 0 ] = lineSplit [ 0 ] . Replace ( "\t " , "" ) ;
100
101
lineSplit [ 0 ] = lineSplit [ 0 ] . Replace ( "\" " , "" ) ;
101
102
lineSplit [ 1 ] = lineSplit [ 1 ] . Substring ( 1 , lineSplit [ 1 ] . Length - 2 ) ;
102
103
@@ -129,44 +130,38 @@ void ParseJSON(TextAsset jsonFile)
129
130
130
131
public void WriteToFile ( )
131
132
{
133
+ if ( jsonFile == null )
134
+ {
135
+ Debug . LogError ( "ERROR: JSON TextAsset file never set!" ) ;
136
+ return ;
137
+ }
138
+
132
139
File . WriteAllText ( AssetDatabase . GetAssetPath ( jsonFile ) , ToString ( ) ) ;
133
140
EditorUtility . SetDirty ( jsonFile ) ;
134
141
}
135
142
136
- #region Add Functions
137
-
138
- public void AddBool ( string newItemName , bool newItemValue )
139
- {
140
- boolList . Add ( new JSONBoolean ( newItemName , newItemValue ) ) ;
141
- }
142
-
143
- public void AddInt ( string newItemName , int newItemValue )
144
- {
145
- intList . Add ( new JSONInteger ( newItemName , newItemValue ) ) ;
146
- }
147
-
148
- public void AddFloat ( string newItemName , float newItemValue )
143
+ public void WriteToFile ( TextAsset jsonFile )
149
144
{
150
- floatList . Add ( new JSONFloat ( newItemName , newItemValue ) ) ;
151
- }
145
+ if ( jsonFile != null )
146
+ {
147
+ Debug . LogError ( $ "ERROR: Object already has a JSON TextAsset file: '{ jsonFile . name } .json'") ;
148
+ return ;
149
+ }
152
150
153
- public void AddString ( string newItemName , string newItemValue )
154
- {
155
- stringList . Add ( new JSONString ( newItemName , newItemValue ) ) ;
151
+ File . WriteAllText ( AssetDatabase . GetAssetPath ( jsonFile ) , ToString ( ) ) ;
152
+ EditorUtility . SetDirty ( jsonFile ) ;
156
153
}
157
154
158
- #endregion Add Functions
159
-
160
155
#region Get Funcitons
161
156
162
157
public bool GetBool ( string variableName )
163
158
{
164
159
bool found = false ;
165
160
bool ret = false ;
166
161
167
- for ( int i = 0 ; i < intList . Count ; i ++ )
162
+ for ( int i = 0 ; i < boolList . Count ; i ++ )
168
163
{
169
- if ( intList [ i ] . name == variableName )
164
+ if ( boolList [ i ] . name == variableName )
170
165
{
171
166
found = true ;
172
167
ret = boolList [ i ] . value ;
@@ -214,9 +209,9 @@ public float GetFloat(string variableName)
214
209
bool found = false ;
215
210
float ret = 0.0f ;
216
211
217
- for ( int i = 0 ; i < intList . Count ; i ++ )
212
+ for ( int i = 0 ; i < floatList . Count ; i ++ )
218
213
{
219
- if ( intList [ i ] . name == variableName )
214
+ if ( floatList [ i ] . name == variableName )
220
215
{
221
216
found = true ;
222
217
ret = floatList [ i ] . value ;
@@ -239,9 +234,9 @@ public string GetString(string variableName)
239
234
bool found = false ;
240
235
string ret = null ;
241
236
242
- for ( int i = 0 ; i < intList . Count ; i ++ )
237
+ for ( int i = 0 ; i < stringList . Count ; i ++ )
243
238
{
244
- if ( intList [ i ] . name == variableName )
239
+ if ( stringList [ i ] . name == variableName )
245
240
{
246
241
found = true ;
247
242
ret = stringList [ i ] . value ;
@@ -260,6 +255,111 @@ public string GetString(string variableName)
260
255
}
261
256
262
257
#endregion Get Functions
258
+
259
+ #region Set Funcitons
260
+
261
+ public void SetBool ( string variableName , bool newBool )
262
+ {
263
+ bool found = false ;
264
+
265
+ for ( int i = 0 ; i < boolList . Count ; i ++ )
266
+ {
267
+ if ( boolList [ i ] . name == variableName )
268
+ {
269
+ found = true ;
270
+ boolList [ i ] . value = newBool ;
271
+ }
272
+ }
273
+
274
+ if ( ! found )
275
+ {
276
+ Debug . LogError ( $ "ERROR: No variable names { variableName } found!") ;
277
+ }
278
+ }
279
+
280
+ public void SetInt ( string variableName , int newInt )
281
+ {
282
+ bool found = false ;
283
+
284
+ for ( int i = 0 ; i < intList . Count ; i ++ )
285
+ {
286
+ if ( intList [ i ] . name == variableName )
287
+ {
288
+ found = true ;
289
+ intList [ i ] . value = newInt ;
290
+ }
291
+ }
292
+
293
+ if ( ! found )
294
+ {
295
+ Debug . LogError ( $ "ERROR: No variable names { variableName } found!") ;
296
+ }
297
+ }
298
+
299
+ public void SetFloat ( string variableName , float newFloat )
300
+ {
301
+ bool found = false ;
302
+
303
+ for ( int i = 0 ; i < floatList . Count ; i ++ )
304
+ {
305
+ if ( floatList [ i ] . name == variableName )
306
+ {
307
+ found = true ;
308
+ floatList [ i ] . value = newFloat ;
309
+ }
310
+ }
311
+
312
+ if ( ! found )
313
+ {
314
+ Debug . LogError ( $ "ERROR: No variable names { variableName } found!") ;
315
+ }
316
+ }
317
+
318
+ public void SetString ( string variableName , string newString )
319
+ {
320
+ bool found = false ;
321
+
322
+ for ( int i = 0 ; i < stringList . Count ; i ++ )
323
+ {
324
+ if ( stringList [ i ] . name == variableName )
325
+ {
326
+ found = true ;
327
+ stringList [ i ] . value = newString ;
328
+ }
329
+ }
330
+
331
+ if ( ! found )
332
+ {
333
+ Debug . LogError ( $ "ERROR: No variable names { variableName } found!") ;
334
+ }
335
+ }
336
+
337
+ #endregion Get Functions
338
+
339
+ #region Add Functions
340
+
341
+ public void AddBool ( string newItemName , bool newItemValue )
342
+ {
343
+ boolList . Add ( new JSONBoolean ( newItemName , newItemValue ) ) ;
344
+ }
345
+
346
+ public void AddInt ( string newItemName , int newItemValue )
347
+ {
348
+ intList . Add ( new JSONInteger ( newItemName , newItemValue ) ) ;
349
+ }
350
+
351
+ public void AddFloat ( string newItemName , float newItemValue )
352
+ {
353
+ floatList . Add ( new JSONFloat ( newItemName , newItemValue ) ) ;
354
+ }
355
+
356
+ public void AddString ( string newItemName , string newItemValue )
357
+ {
358
+ stringList . Add ( new JSONString ( newItemName , newItemValue ) ) ;
359
+ }
360
+
361
+ #endregion Add Functions
362
+
263
363
}
264
364
265
365
#region JSON Variables
0 commit comments