Unity对话框

时间:2018-12-05 19:51:50

标签: c# unity3d dialog

我正在尝试创建一个对话框,该对话框在给定计数时停止,然后在发生特定事件后继续。我最初将句子数设置为6,并且确实设法使对话在第三个句子之后停止

 by setting the if statement in DisplayNextSentence() to ==> [sentence.Count == 3]

然后让玩家做其他事情,但我不知道如何在完成某项活动后让NPC继续对话。

您能给我启发怎么做吗?我确实看了一些youtube视频,但是这些视频使我感到困惑,或者他们没有解释该特定部分的详细工作原理,但是我发现这种方法在我的情况下更容易理解。

容器

[System.Serializable]
public class Dialogue {

//name of npc
public string name;

//sentences of the npc
[TextArea(3,10)]
public string[] sentences;
}

另一堂课

public class DialogueTrigger : MonoBehaviour {

//calls the Dialogue class 
public Dialogue dialogue;

//triggers the dialouge
public void TriggerDialogue()
{
    FindObjectOfType<DialogueManager>().StartDialogue(dialogue);
}
}

经理

public class DialogueManager : MonoBehaviour {

private Queue<string> sentences;

public Text NPC_nameText;
public Text NPC_DialogueText;

void Start () {
    sentences = new Queue<string>(); 
}

public void StartDialogue(Dialogue dialogue)
{
    //Changes the name of the Name Text to given name
    NPC_nameText.text = dialogue.name;

    sentences.Clear();

    //goes through the whole array of sentences
    foreach(string sentence in dialogue.sentences)
    {
        sentences.Enqueue(sentence);
    }

    //displays the sentences
    DisplayNextSentence();
}

public void DisplayNextSentence()
{
    //condition if array sentence is finished looping
    if (sentences.Count == 0)
    {
        //ends dialogue and goes to the next UI
        EndDialogue();
        return;
    }   

    //Sentence appears in queue waiting for each sentence to finish
    string sentence = sentences.Dequeue();
    StopAllCoroutines();

    StartCoroutine(TypeSentence(sentence));
}

void EndDialogue()
{
    Debug.Log("End of Conversation");
}

IEnumerator TypeSentence(string sentence)
{
    //types the characters for each sentence
    NPC_DialogueText.text = "";
    foreach (char letter in sentence.ToCharArray())
    {
        NPC_DialogueText.text += letter;
        yield return null;
    }
}

}

0 个答案:

没有答案