1010using System . IO ;
1111using UnityEditor ;
1212
13- namespace JSONSerializerPackage
13+ namespace JSON
1414{
1515 [ Serializable ]
1616 public class JSON
@@ -20,7 +20,7 @@ public class JSON
2020 List < JSONFloat > floatList ;
2121 List < JSONString > stringList ;
2222
23- TextAsset jsonFile ;
23+ TextAsset jsonFile = null ;
2424
2525 public JSON ( )
2626 {
@@ -97,6 +97,7 @@ void ParseJSON(TextAsset jsonFile)
9797 {
9898 string [ ] lineSplit = lines [ i ] . Split ( new string [ ] { ":" } , StringSplitOptions . None ) ;
9999 lineSplit [ 0 ] = lineSplit [ 0 ] . Replace ( " \" " , "" ) ;
100+ lineSplit [ 0 ] = lineSplit [ 0 ] . Replace ( "\t " , "" ) ;
100101 lineSplit [ 0 ] = lineSplit [ 0 ] . Replace ( "\" " , "" ) ;
101102 lineSplit [ 1 ] = lineSplit [ 1 ] . Substring ( 1 , lineSplit [ 1 ] . Length - 2 ) ;
102103
@@ -129,44 +130,38 @@ void ParseJSON(TextAsset jsonFile)
129130
130131 public void WriteToFile ( )
131132 {
133+ if ( jsonFile == null )
134+ {
135+ Debug . LogError ( "ERROR: JSON TextAsset file never set!" ) ;
136+ return ;
137+ }
138+
132139 File . WriteAllText ( AssetDatabase . GetAssetPath ( jsonFile ) , ToString ( ) ) ;
133140 EditorUtility . SetDirty ( jsonFile ) ;
134141 }
135142
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 )
149144 {
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+ }
152150
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 ) ;
156153 }
157154
158- #endregion Add Functions
159-
160155 #region Get Funcitons
161156
162157 public bool GetBool ( string variableName )
163158 {
164159 bool found = false ;
165160 bool ret = false ;
166161
167- for ( int i = 0 ; i < intList . Count ; i ++ )
162+ for ( int i = 0 ; i < boolList . Count ; i ++ )
168163 {
169- if ( intList [ i ] . name == variableName )
164+ if ( boolList [ i ] . name == variableName )
170165 {
171166 found = true ;
172167 ret = boolList [ i ] . value ;
@@ -214,9 +209,9 @@ public float GetFloat(string variableName)
214209 bool found = false ;
215210 float ret = 0.0f ;
216211
217- for ( int i = 0 ; i < intList . Count ; i ++ )
212+ for ( int i = 0 ; i < floatList . Count ; i ++ )
218213 {
219- if ( intList [ i ] . name == variableName )
214+ if ( floatList [ i ] . name == variableName )
220215 {
221216 found = true ;
222217 ret = floatList [ i ] . value ;
@@ -239,9 +234,9 @@ public string GetString(string variableName)
239234 bool found = false ;
240235 string ret = null ;
241236
242- for ( int i = 0 ; i < intList . Count ; i ++ )
237+ for ( int i = 0 ; i < stringList . Count ; i ++ )
243238 {
244- if ( intList [ i ] . name == variableName )
239+ if ( stringList [ i ] . name == variableName )
245240 {
246241 found = true ;
247242 ret = stringList [ i ] . value ;
@@ -260,6 +255,111 @@ public string GetString(string variableName)
260255 }
261256
262257 #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+
263363 }
264364
265365 #region JSON Variables
0 commit comments