如何在对话上方的顶层添加对话?

时间:2019-03-30 03:03:03

标签: c# unity3d

在对话触发脚本顶部,我添加了一个新的List类型字符串:

using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using UnityEngine;

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

    [HideInInspector]
    public int dialogueNum = 0;

    private bool triggered = false;
    private List<Dialogue> oldDialogue;

    private void Start()
    {
        //oldDialogue = dialogue.ToList();
    }

    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 UnityEditor;
using UnityEditorInternal;
using UnityEngine;

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

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

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

        serializedObject.Update();

        _conversations.arraySize = EditorGUILayout.IntField("Conversation Size", _conversations.arraySize);

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

        for (int x = 0; x < _conversations.arraySize; x++)
        {
            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();
    }
}

我正在_对话循环中进行_conversations循环。 但这不是我想要的。

我希望在顶层的“检查器”中只有“会话大小”。

如果“对话大小”为0,则没有任何内容。 但是,例如,如果“会话大小”为5。

然后它将创建5个对话(对话1,对话2 .... Conversation5)。在每个对话下都有一个Dialouge大小,然后我可以在每个对话中进行很多对话。

这样,以后识别对话和对话将更加容易。 而是像以前那样进行了长时间的对话。

我添加了会话列表,在此之前没有会话并且可以正常工作,但是现在我想添加会话。因此,每个对话都有自己的对话。

这是根据解决方案编写的脚本:

创建了课程:

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

[System.Serializable]
public class Conversation
{
    public string Id;
    public List<Dialogue> Dialogues = new List<Dialogue>();
}

然后触发脚本:

using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using UnityEngine;

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

    [HideInInspector]
    public int dialogueNum = 0;

    private bool triggered = false;
    private List<Dialogue> oldDialogue;

    private void Start()
    {
        //oldDialogue = dialogue.ToList();
    }

    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 UnityEditor;
using UnityEditorInternal;
using UnityEngine;

[CustomEditor(typeof(DialogueTrigger))]
public class DialogueTriggerEditor : Editor
{
    private SerializedProperty _dialogues;
    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 Id = conversation.FindPropertyRelative("Id");
            EditorGUILayout.PropertyField(Id);

            _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);
                EditorGUILayout.PropertyField(dialogue, new GUIContent("Dialogue " + i), true);
            }
        }

        serializedObject.ApplyModifiedProperties();
    }
}

以及屏幕截图结果:

Conversations

我认为检查员中的所有Id应该是“对话”大小的孩子,所有“对话”大小应该是Id的孩子,然后“对话”应该是“对话大小”的孩子。

Conversation Size
   Id 1
    Dialogues Size
      Dialogue 1
        Name
        Sentences
      Dialogue 2

类似的东西。

1 个答案:

答案 0 :(得分:1)

您应该改选一个合适的班级

[Serializable]
public class Conversation
{
    public string Id;
    public List<Dialogue> Dialogues = new List<Dialogue>();
}

,而不是在其中列出该列表

public DialogueTrigget : MonoBehaviour
{
    public List<Conversation> Conversations = new List<Conversation> ();
    ...
}

比起您上次给您使用的脚本,您可以使用完全相同的脚本,

_conversations = serializedObject.FindProperty("Conversations");

,因此一个for循环与您已经尝试过的类似。.

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

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

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

        serializedObject.Update();

        _conversations.arraySize = EditorGUILayout.IntField("Conversation Size", _conversations.arraySize);

        for (int x = 0; x < _conversations.arraySize; x++)
        {
            var conversation = _conversations.GetArrayElementAtIndex(x);

            var id = conversation.FindPropertyRelative("I'd");

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

            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--;
            }

            EditorGUI.indentLevel--;
        }

        serializedObject.ApplyModifiedProperties();
    }
}

或简单地

EditorGUILayout.PropertyField(_conversations, new GUIContent ("Conversations"), true);

但这不会显示“对话1,对话2”等


再次,我强烈建议您查看nested ReorderableList,一旦您使用它,它就会很棒。


在我的智能手机上输入,因此没有保修