Ejemplo n.º 1
0
        /// <summary>
        ///     Returns the json representation of this object.
        /// </summary>
        /// <returns></returns>
        public string ToJson()
        {
            string json = "{";

            for (int i = 0; i < Entries.Count; i++)
            {
                SimpleJsonEntry entry = Entries[i];

                json += $"\"{entry.Key}\":";

                if (entry.Type == SimpleJsonEntry.JsonTypes.STRING)
                {
                    json += $"\"{entry.Value}\"";
                }
                else if (entry.Type == SimpleJsonEntry.JsonTypes.BOOL)
                {
                    json += $"{entry.Value.ToLower()}";
                }
                else if (entry.Type == SimpleJsonEntry.JsonTypes.LIST)
                {
                    json += entry.List.ToJson();
                }
                else
                {
                    json += $"{entry.Value}";
                }

                if (i < Entries.Count - 1)
                {
                    json += ",";
                }
            }

            json += "}";

            return(json);
        }
Ejemplo n.º 2
0
        /// <summary>
        ///     Draws a simple json list.
        /// </summary>
        private void DrawSimpleJsonList(ref SimpleJsonList jsonList)
        {
            EditorGUILayout.BeginVertical(EditorStyles.helpBox);

            GUI.backgroundColor = blue;

            EditorGUILayout.BeginHorizontal();

            GUIStyle labelStyle = new GUIStyle(EditorStyles.label)
            {
                wordWrap = true
            };

            if (GUILayout.Button(jsonList.IsExpanded ? "↓" : "↑", GUILayout.Width(20f)))
            {
                jsonList.IsExpanded = !jsonList.IsExpanded;
            }

            EditorGUILayout.LabelField("Entry Type", labelStyle);
            EditorGUILayout.LabelField("Entry Key", labelStyle);
            EditorGUILayout.LabelField("Entry Value", labelStyle);

            EditorGUILayout.EndHorizontal();

            int removeEntryIndex = -1;

            if (jsonList.IsExpanded)
            {
                for (int i = 0; i < jsonList.Entries.Count; i++)
                {
                    SimpleJsonEntry entry = jsonList.Entries[i];
                    EditorGUILayout.BeginHorizontal();

                    entry.Type = (SimpleJsonEntry.JsonTypes)EditorGUILayout.EnumPopup(entry.Type);
                    entry.Key  = EditorGUILayout.TextField(entry.Key);

                    switch (entry.Type)
                    {
                    case SimpleJsonEntry.JsonTypes.STRING:
                        entry.Value = EditorGUILayout.TextField(entry.Value);

                        if (DrawJsonEntryDeleteButton())
                        {
                            removeEntryIndex = i;
                        }

                        EditorGUILayout.EndHorizontal();

                        break;

                    case SimpleJsonEntry.JsonTypes.INT:
                        int intValue;
                        int.TryParse(entry.Value, out intValue);

                        entry.Value = EditorGUILayout.IntField(intValue).ToString();

                        if (DrawJsonEntryDeleteButton())
                        {
                            removeEntryIndex = i;
                        }

                        EditorGUILayout.EndHorizontal();

                        break;

                    case SimpleJsonEntry.JsonTypes.FLOAT:
                        float floatValue;
                        float.TryParse(entry.Value, out floatValue);

                        entry.Value = EditorGUILayout.FloatField(floatValue).ToString(CultureInfo.InvariantCulture);

                        if (DrawJsonEntryDeleteButton())
                        {
                            removeEntryIndex = i;
                        }

                        EditorGUILayout.EndHorizontal();

                        break;

                    case SimpleJsonEntry.JsonTypes.BOOL:
                        bool boolValue;
                        bool.TryParse(entry.Value, out boolValue);

                        entry.Value = EditorGUILayout.Toggle(boolValue).ToString();

                        if (DrawJsonEntryDeleteButton())
                        {
                            removeEntryIndex = i;
                        }

                        EditorGUILayout.EndHorizontal();

                        break;

                    case SimpleJsonEntry.JsonTypes.LIST:

                        if (DrawJsonEntryDeleteButton())
                        {
                            removeEntryIndex = i;
                        }

                        EditorGUILayout.EndHorizontal();

                        EditorGUI.indentLevel++;
                        DrawSimpleJsonList(ref entry.List);
                        EditorGUI.indentLevel--;

                        break;

                    default:
                        entry.Value = EditorGUILayout.TextField(entry.Value);

                        if (DrawJsonEntryDeleteButton())
                        {
                            removeEntryIndex = i;
                        }

                        EditorGUILayout.EndHorizontal();

                        break;
                    }
                }

                if (EditorGUILayout.Foldout(false, "Add Entry", true, EditorStyles.miniButton))
                {
                    jsonList.Entries.Add(new SimpleJsonEntry());
                }
            }

            if (removeEntryIndex != -1)
            {
                jsonList.Entries.RemoveAt(removeEntryIndex);
            }

            EditorGUILayout.EndVertical();
        }