void OnGUI() { GUILayout.Label("JSON Creator", EditorStyles.boldLabel); GUILayout.Space(10f); #region JSON Data Display ///Display and addition of new JSON data with Root as parent is managed here. GUILayout.BeginHorizontal(GUILayout.Width(100f)); GUILayout.Label("Root", EditorStyles.label); if (GUILayout.Button("+", GUILayout.Width(50f))) { ///This logic takes care that each time a unique key string is added to the Root. JSON does not support 2 keys of the same name in the same hierarchy. ///For example, a key named "Int" in Root and a key named "Int" as a child of an Object is valid, but 2 keys named "Int" in Root or as children of the same Object are invalid. bool isKeyValid = true; Dictionary <string, List <string> > distinctKeys = new Dictionary <string, List <string> > (); int count = 0; foreach (JSONDataClass j in jsonData) { if (!distinctKeys.ContainsKey(j.parent.key)) { List <string> tempKey = new List <string> (); tempKey.Add(j.key); distinctKeys.Add(j.parent.key, tempKey); count++; } else { if (!distinctKeys [j.parent.key].Contains(j.key)) { distinctKeys [j.parent.key].Add(j.key); count++; } } } distinctKeys.Clear(); for (int i = 0; i < jsonData.Count; i++) { if (string.IsNullOrEmpty(jsonData [i].key) || count < jsonData.Count) { isKeyValid = false; break; } else { isKeyValid = true; } } ///If all your last entered keys are unique, then a drop-down for new entry is shown, otherwise an error message is displayed. if (isKeyValid) { JSONCreatorHelper.DropdownMenu(); } else { EditorUtility.DisplayDialog("Invalid Key Name(s)", "Please don't leave key name empty and choose a unique key name.", "OK"); } } if (GUILayout.Button("Clear", GUILayout.Width(50f))) { if (EditorUtility.DisplayDialog("Clear", "Are you sure you want to clear all data?", "Yes", "No")) { jsonData.Clear(); } } GUILayout.EndHorizontal(); DisplayData(); #endregion GUILayout.Space(30f); #region Compiling-Saving JSON GUILayout.BeginHorizontal(GUILayout.Width(200f)); GUILayout.Label("JSON File Name: ", EditorStyles.boldLabel); fileName = GUILayout.TextField(fileName, GUILayout.Width(150f)); if (GUILayout.Button("Save JSON", GUILayout.Width(150f))) { CompileJSONString(); if (File.Exists(Path.Combine(path, fileName + ".json"))) { if (EditorUtility.DisplayDialog("JSON Exists", "A JSON file with the name same as \"" + fileName + "\" already exists. Would you like to overwrite?", "Yes", "No")) { File.Delete(Path.Combine(path, fileName + ".json")); File.WriteAllText(Path.Combine(path, fileName + ".json"), finalJSONString.ToString()); EditorUtility.DisplayDialog("Saved", "JSON written at " + path, "OK"); } } else { File.WriteAllText(Path.Combine(path, fileName + ".json"), finalJSONString.ToString()); EditorUtility.DisplayDialog("Saved", "JSON written at " + path, "OK"); } } GUILayout.EndHorizontal(); #endregion }
void DisplayData() { try { for (int i = 0; i < jsonData.Count; i++) { if (jsonData [i].valueDataType != DataTypes.Array && jsonData [i].valueDataType != DataTypes.Object) /// Where the data is not an Array/Object type, both, key and value are shown. { GUILayout.BeginHorizontal(); GUILayout.Space(jsonData [i].indent); GUILayout.Label("(" + jsonData [i].valueDataType.ToString() [0] + ")", GUILayout.Width(25f)); jsonData [i].key = GUILayout.TextField(jsonData [i].key, GUILayout.Width(150f)); GUILayout.Label(":", GUILayout.Width(10f)); ///If the value of a data is a Boolean, for consistency reasons, only a Toggle box is shown in the sheet. /// If this box is checked, the value is 'true', otherwise 'false'. if (jsonData [i].valueDataType != DataTypes.Bool) { jsonData [i].value = GUILayout.TextField(jsonData [i].value.ToString(), GUILayout.Width(150f)); } else { jsonData [i].value = GUILayout.Toggle(bool.Parse(jsonData [i].value.ToString()), bool.Parse(jsonData [i].value.ToString()).ToString(), GUILayout.Width(50f)).ToString(); } ///Data delete button. if (GUILayout.Button("x", GUILayout.Width(25f))) { JSONCreatorHelper.DeleteData(i, jsonData [i].parent.key == "root"); break; } GUILayout.EndHorizontal(); } else /// Where the data is an Array/Object type, only the key is shown. The value is kept null. { GUILayout.BeginHorizontal(); GUILayout.Space(jsonData [i].indent); jsonData [i].key = GUILayout.TextField(jsonData [i].key, GUILayout.Width(150f)); ///This logic takes care that each time a unique key string is added to the given Array/Object parent. JSON does not support 2 keys of the same name in the same hierarchy. ///For example, a key named "Int" in Root and a key named "Int" as a child of an Object is valid, but 2 keys named "Int" in Root or as children of the same Object are invalid. if (GUILayout.Button("+", GUILayout.Width(50f))) { bool isKeyValid = true; Dictionary <string, List <string> > distinctKeys = new Dictionary <string, List <string> > (); int count = 0; foreach (JSONDataClass j in jsonData) { if (!distinctKeys.ContainsKey(j.parent.key)) { List <string> tempKey = new List <string> (); tempKey.Add(j.key); distinctKeys.Add(j.parent.key, tempKey); count++; } else { if (!distinctKeys [j.parent.key].Contains(j.key)) { distinctKeys [j.parent.key].Add(j.key); count++; } } } distinctKeys.Clear(); for (int j = 0; j < jsonData.Count; j++) { if (string.IsNullOrEmpty(jsonData [j].key) || count < jsonData.Count) { isKeyValid = false; break; } else { isKeyValid = true; } } ///If all your last entered keys withn the given subset are unique, then a drop-down for new entry is shown, otherwise an error message is displayed. if (isKeyValid) { JSONCreatorHelper.DropdownMenu(false, jsonData [i]); } else { EditorUtility.DisplayDialog("Invalid Key Name(s)", "Please don't leave key name empty and choose a unique key name.", "OK"); } } if (GUILayout.Button("x", GUILayout.Width(25f))) { JSONCreatorHelper.DeleteData(i, jsonData [i].parent.key == "root"); break; } GUILayout.EndHorizontal(); } } } catch (Exception e) { Debug.Log("Whoops! Problem with reading data. ERROR: " + e); } }