如何获得SerializedObject和FineProperty以及对话的节点树子级?

时间:2019-05-14 19:02:54

标签: c# unity3d

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

public class ConversationsEditorWindow : EditorWindow
{
    const int TOP_PADDING = 2;
    const string HELP_TEXT = "Can not find 'Conversation' component on any GameObject in the Scene.";

    static Vector2 s_WindowsMinSize = Vector2.one * 300.0f;
    static Rect s_HelpRect = new Rect(0.0f, 0.0f, 300.0f, 100.0f);
    static Rect s_ListRect = new Rect(Vector2.zero, s_WindowsMinSize);

    [MenuItem("Conversations/Editor")]
    private static void ConversationsSystem()
    {
        const int width = 800;
        const int height = 800;

        var x = (Screen.currentResolution.width - width) / 2;
        var y = (Screen.currentResolution.height - height) / 2;

        GetWindow<ConversationsEditorWindow>().position = new Rect(x, y, width, height);
    }

    SerializedObject m_Conversations = null;
    ReorderableList m_ReorderableList = null;

    private void OnEnable()
    {
        ConversationTrigger conversationtrigger = FindObjectOfType<ConversationTrigger>();
        if (conversationtrigger)
        {
            m_Conversations = new SerializedObject(conversationtrigger);
            m_ReorderableList = new ReorderableList(m_Conversations, m_Conversations.FindProperty("conversations"), true, true, true, true);

            m_ReorderableList.drawHeaderCallback = (rect) => EditorGUI.LabelField(rect, "Conversation Names");
            m_ReorderableList.drawElementCallback = (Rect rect, int index, bool isActive, bool isFocused) =>
            {
                rect.y += TOP_PADDING;
                rect.height = EditorGUIUtility.singleLineHeight;
                EditorGUI.PropertyField(rect, m_ReorderableList.serializedProperty.GetArrayElementAtIndex(index));
            };
        }
    }

    private void OnInspectorUpdate()
    {
        Repaint();
    }

    private void OnGUI()
    {
        if (m_Conversations != null)
        {
            m_Conversations.Update();
            m_ReorderableList.DoList(s_ListRect);
            m_Conversations.ApplyModifiedProperties();
        }
        else
        {
            EditorGUI.HelpBox(s_HelpRect, HELP_TEXT, MessageType.Warning);
        }
    }
}

这是检查器在编辑器窗口中的外观:

Conversations

第一个问题是我可以在层次结构中任何GameObject的编辑器窗口中添加/删除项目。但是我希望只能在对话进行的GameObject上执行此操作。在这种情况下,应该是DialogueTrigger,但是例如,如果我选择“地形”,我也可以添加/删除项目。

第二个问题是它没有在“编辑器”窗口中显示每个对话的节点树子级。例如,“开头”具有“名称”和“对话”等,但是当我单击“编辑器”窗口中的折叠箭头时,它没有显示。

最后,当在“编辑器”窗口中添加新项目时,我希望它像在检查器中一样添加一个新的空对话,否则,如果我选择了一个现有对话,则它应该添加与所选对话相同的对话。

(每次单击编辑器窗口中的加号(+)时,现在每次仅添加最后一次对话。即使我先选择“开幕式”,也始终添加“锁定室”。

第一个解决的问题:

private void OnGUI()
    {
        var go = Selection.gameObjects[0];
        var ct = go.GetComponent<ConversationTrigger>();

        if (ct != null)
        {
            m_Conversations.Update();
            m_ReorderableList.DoList(s_ListRect);
            m_Conversations.ApplyModifiedProperties();
        }
        else
        {
            EditorGUI.HelpBox(s_HelpRect, HELP_TEXT, MessageType.Warning);
        }
    }

0 个答案:

没有答案