0
\$\begingroup\$
using System.Collections;
using System.Collections.Generic;
using UnityEditor;
using UnityEditorInternal;
using UnityEngine;

[CustomEditor(typeof(DialogueTrigger))]
public class DialogueTriggerEditor : Editor
{
    private SerializedProperty _conversations;

    private void OnEnable()
    {
        _conversations = serializedObject.FindProperty("conversations");
    }

    public override void OnInspectorGUI()
    {
        //base.OnInspectorGUI();

        serializedObject.Update();

        _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("conversationName");

            EditorGUI.indentLevel++;
            EditorGUILayout.PropertyField(conversationName);

            EditorGUI.indentLevel++;
            var _dialogues = conversation.FindPropertyRelative("Dialogues");

            _dialogues.arraySize = EditorGUILayout.IntField("Dialogues size", _dialogues.arraySize);

            for (int i = 0; i < _dialogues.arraySize; i++)
            {
                var dialogue = _dialogues.GetArrayElementAtIndex(i);
                EditorGUI.indentLevel++;
                EditorGUILayout.PropertyField(dialogue, new GUIContent("Dialogue " + i), true);

                EditorGUI.indentLevel--;
            }

            if (_dialogues.arraySize > 0)
            {
                if (GUILayout.Button("Save Conversation"))
                {

                }
            }

            EditorGUI.indentLevel--;
        }

        serializedObject.ApplyModifiedProperties();
    }
}

I'm using in some places the EditorGUI.indentLevel++; but the result is this:

constructor

But I want it to be like this:

Conversations Size
     Conversation Name
          Dialogue size
     Conversation Name
          Dialogue size
     Conversation Name
          Dialogue size
     Conversation Name
          Dialogue size

This is how it should look like in the Inspector.

The problem is in this part:

EditorGUI.indentLevel++;
EditorGUILayout.PropertyField(conversationName);
EditorGUI.indentLevel++;
var _dialogues = conversation.FindPropertyRelative("Dialogues");
_dialogues.arraySize = EditorGUILayout.IntField("Dialogues size", _dialogues.arraySize);

I can't make the "Conversation" Name the "Dialogues" and the "Dialogues size" the same as I wanted.

The "Dialogues" is fine but the others are not.

\$\endgroup\$
1
  • 1
    \$\begingroup\$ At the outer level of your for loop, it looks like you increment the indent twice, but only decrement it once. Is that intentional? \$\endgroup\$ Commented Apr 1, 2019 at 22:30

1 Answer 1

1
\$\begingroup\$

Working solution is to add another decrement at the bottom of the outer loop:

using System.Collections;
using System.Collections.Generic;
using UnityEditor;
using UnityEditorInternal;
using UnityEngine;

[CustomEditor(typeof(DialogueTrigger))]
public class DialogueTriggerEditor : Editor
{
    private SerializedProperty _conversations;

    private void OnEnable()
    {
        _conversations = serializedObject.FindProperty("conversations");
    }

    public override void OnInspectorGUI()
    {
        //base.OnInspectorGUI();

        serializedObject.Update();

        _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("conversationName");

            EditorGUI.indentLevel++;
            EditorGUILayout.PropertyField(conversationName);

            EditorGUI.indentLevel++;
            var _dialogues = conversation.FindPropertyRelative("Dialogues");

            _dialogues.arraySize = EditorGUILayout.IntField("Dialogues size", _dialogues.arraySize);

            for (int i = 0; i < _dialogues.arraySize; i++)
            {
                var dialogue = _dialogues.GetArrayElementAtIndex(i);
                EditorGUI.indentLevel++;
                EditorGUILayout.PropertyField(dialogue, new GUIContent("Dialogue " + i), true);

                EditorGUI.indentLevel--;
            }

            if (_dialogues.arraySize > 0)
            {
                if (GUILayout.Button("Save Conversation"))
                {

                }
            }

            EditorGUI.indentLevel--;
            EditorGUI.indentLevel--;
        }

        serializedObject.ApplyModifiedProperties();
    }
}
\$\endgroup\$
1
  • \$\begingroup\$ Don't forget to click the ✅ to mark this answer as "Accepted" if it worked for you. \$\endgroup\$ Commented May 23 at 14:19

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.