如何在Inspector编辑器脚本中绘制列表及其所有项目?

时间:2019-03-28 22:16:14

标签: c# unity3d

主要脚本:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class DialogueTrigger : MonoBehaviour
{
    public List<Dialogue> dialogue = new List<Dialogue>();

    [HideInInspector]
    public int dialogueNum = 0;

    private bool triggered = false;

    public void TriggerDialogue()
    {
        if (triggered == false)
        {
            if (FindObjectOfType<DialogueManager>() != null)
            {
                FindObjectOfType<DialogueManager>().StartDialogue(dialogue[dialogueNum]);
                dialogueNum += 1;
            }
            triggered = true;
        }
    }

    private void Update()
    {
        if (DialogueManager.dialogueEnded == true)
        {
            if (dialogueNum == dialogue.Count)
            {
                return;
            }
            else
            {
                FindObjectOfType<DialogueManager>().StartDialogue(dialogue[dialogueNum]);
                DialogueManager.dialogueEnded = false;
                dialogueNum += 1;
            }
        }
    }
}

创建项目名称和句子的对话脚本:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

[System.Serializable]
public class Dialogue
{
    public string name;

    [TextArea(1, 10)]
    public string[] sentences;
}

编辑器脚本:

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

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

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

        _dialogues = serializedObject.FindProperty("dialogue");
        serializedObject.Update();

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

但是现在我在检查器中有了一个对话框变量,可以在其中设置对话框的数量以及每个对话框的名称和句子。

但是在它之下,根据我设置的对话数量创建了更多的对话。

Dialogues

我想在检查器中使用的是一种主要的对话:

然后在其中设置对话次数。例如,如果我设置5,则在“对话”下将有:对话1对话2对话3对话4对话5

然后在每个“对话”下,例如“对话1”下,将显示其名称和句子。能够更改每个对话的句子大小。

2 个答案:

答案 0 :(得分:1)

如果在对话框列表中使用SerializeField属性,则将获得“对话框”的根元素,您可以在其中指定列表中元素的数量,并且每个子级都是对话框类的实例,如果在编辑器脚本中序列化字段,如果您将其添加到脚本列表中,元素也会更新。

编辑:您还需要更新编辑器脚本,如果您想从编辑器脚本中添加元素,则可以从游戏对象中获取该类的实例,然后将元素添加到列表中(只要列表是公开的)

示例

Script.cs

[SerializeField]
public List<Dialogue> dialogue = new List<Dialogue>();

Editor.cs

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

    Script script = GameObject.Find("GameObject").GetComponent<Script>();
    script.dialogue.Add(new Dialogue());
    EditorUtility.SetDirty(script);
}

This is what it should look like in the editor

答案 1 :(得分:1)

问题是默认情况下EditorGUILayout.PropertyField不支持嵌套属性。

最简单的解决方法是使用正确的重载PropertyField(SerializedProperty property, GUIContent label, bool includeChildren, params GUILayoutOption[] options);

需要bool includeChildren

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

    private void OnEnable()
    {
        // do this only once here
        _dialogues = serializedObject.FindProperty("dialogue");
    }

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

        serializedObject.Update();

        // Ofcourse you also want to change the list size here
        _dialogues.arraySize = EditorGUILayout.IntField("Size", _dialogues.arraySize);

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

        // Note: You also forgot to add this
        serializedObject.ApplyModifiedProperties();
    }
}

enter image description here


请注意,还有其他可定制的解决方案。另一个快速的例如手动获取那些嵌套属性并定义应如何绘制它们:

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

    // store which dialogue is foldout
    private List<bool> dialogueFoldout = new List<bool>();

    private void OnEnable()
    {
        _dialogues = serializedObject.FindProperty("dialogue");

        for (var i = 0; i < _dialogues.arraySize; i++)
        {
            dialogueFoldout.Add(false);
        }
    }

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

        serializedObject.Update();

        var color = GUI.color;

        EditorGUI.BeginChangeCheck();
        _dialogues.arraySize = EditorGUILayout.IntField("Size", _dialogues.arraySize);
        if (EditorGUI.EndChangeCheck())
        {
            dialogueFoldout.Clear();

            for (var i = 0; i < _dialogues.arraySize; i++)
            {
                dialogueFoldout.Add(false);
            }

            serializedObject.ApplyModifiedProperties();
            return;
        }

        for (var i = 0; i < _dialogues.arraySize; i++)
        {
            var dialogue = _dialogues.GetArrayElementAtIndex(i);

            dialogueFoldout[i] = EditorGUILayout.Foldout(dialogueFoldout[i], "Dialogue " + i);

            // make the next fields look nested below the before one
            EditorGUI.indentLevel++;

            if (dialogueFoldout[i])
            {
                var name = dialogue.FindPropertyRelative("name");
                var sentences = dialogue.FindPropertyRelative("sentences");

                if (string.IsNullOrWhiteSpace(name.stringValue)) GUI.color = Color.yellow;
                EditorGUILayout.PropertyField(name);
                GUI.color = color;

                // if you still want to be able to controll the size
                sentences.arraySize = EditorGUILayout.IntField("Senteces size", sentences.arraySize);

                // make the next fields look nested below the before one
                EditorGUI.indentLevel++;
                for (var s = 0; s < sentences.arraySize; s++)
                {
                    var sentence = sentences.GetArrayElementAtIndex(s);
                    if (string.IsNullOrWhiteSpace(sentence.stringValue)) GUI.color = Color.yellow;
                    EditorGUILayout.PropertyField(sentence, new GUIContent("Sentece " + s));
                    GUI.color = color;
                }
                EditorGUI.indentLevel--;
            }

            EditorGUI.indentLevel--;
        }

        serializedObject.ApplyModifiedProperties();
    }
}

您可以再次迈出一步,为Dialogue类使用完整的CustomPropertyDrawer。这样做的巨大优势在于,不仅在这一类DialogueTrigger中,而且在您拥有public Dialogue字段的任何地方中,它都将使用自定义抽屉显示出来! / p>


或者,如果您真的想要精美的列表(可重新排序),可以轻松删除任何索引处的元素,等等,我强烈建议您看看ReorderableList。这是Unity使用的未记录功能,例如进入UnityEvent(例如onClick),并且有点复杂,但是一旦您掌握了它,它就会非常强大! (在my question here中,我们还解决了如何像您的情况一样将其用于嵌套列表。)