using System.Collections;
using System.Collections.Generic;
using UnityEditor;
using UnityEditorInternal;
using UnityEngine;
[CustomEditor(typeof(DialogueTrigger))]
public class DialogueTriggerEditor : Editor
{
private SerializedProperty _conversations;
private SerializedProperty _dialogues;
private SerializedProperty _dialogue;
private SerializedProperty name;
private SerializedProperty sentences;
private bool save = false;
private bool saved = false;
private void OnEnable()
{
_conversations = serializedObject.FindProperty("conversations");
}
public override void OnInspectorGUI()
{
serializedObject.Update();
if (_conversations.arraySize == 0)
{
_conversations.ClearArray();
save = false;
}
_conversations.arraySize = EditorGUILayout.IntField("Conversations Size", _conversations.arraySize);
for (int x = 0; x < _conversations.arraySize; x++)
{
var conversation = _conversations.GetArrayElementAtIndex(x);
var conversationName = conversation.FindPropertyRelative("name");
EditorGUI.indentLevel++;
EditorGUILayout.PropertyField(conversationName);
EditorGUI.indentLevel++;
_dialogues = conversation.FindPropertyRelative("Dialogues");
if (_dialogues.arraySize == 0)
{
save = false;
}
_dialogues.arraySize = EditorGUILayout.IntField("Dialogues size", _dialogues.arraySize);
for (int i = 0; i < _dialogues.arraySize; i++)
{
_dialogue = _dialogues.GetArrayElementAtIndex(i);
EditorGUI.indentLevel++;
EditorGUILayout.PropertyField(_dialogue, new GUIContent("Dialogue " + i), true);
name = _dialogue.FindPropertyRelative("name");
sentences = _dialogue.FindPropertyRelative("sentences");
for (int y = 0; y < sentences.arraySize; y++)
{
var sentence = sentences.GetArrayElementAtIndex(y).stringValue;
if (name.stringValue != "" && sentence != "" && saved == false)
{
save = true;
}
else
{
save = false;
}
}
EditorGUI.indentLevel--;
}
EditorGUI.indentLevel--;
EditorGUI.indentLevel--;
}
serializedObject.ApplyModifiedProperties();
if (save == false)
{
GUI.enabled = false;
}
else
{
GUI.enabled = true;
}
if (GUILayout.Button("Save Conversations"))
{
//string[] test = new string[1];
//JsonHelper.ToJson(test, true);
saved = true;
}
}
}
Inside the button “Save Conversations” I want to save the conversations and dialogues name/s and sentences the same structure it is in the three loops:
for (int x = 0; x < _conversations.arraySize; x++)
And
for (int i = 0; i < _dialogues.arraySize; i++)
And
for (int y = 0; y < sentences.arraySize; y++)
The structure in the json file or could be text file should be like in the inspector in the editor :
And then to add a new button for loading and if I press the load button it will load the jkson file and assign it back to the editor to all vars same structure like in the screenshot.