如何创建对话系统

时间:2018-05-31 09:39:28

标签: c# arrays loops unity3d unityscript

using System.Collections;
using UnityEngine;
using TMPro;

public class NPCscript : Interactable {
    public GameObject dialougeObj;
    public TMP_Text dialouge;
    public int Size;
    public string dialougeSt;

    public override void Interact () {
        base.Interact ();
        dialougeObj.SetActive (true);
        NPCInteraction();
    }

    void OnTriggerExit(Collider other) {
        dialougeObj.SetActive(false);
    }
    void NPCInteraction(){
        dialouge.SetText(dialougeSt);
    }
}

请告诉我怎样才能有一个数组或列表,我可以使用我的size变量并循环它直到所有对话都被说出来。

我试过但我的不行。 帮助!

2 个答案:

答案 0 :(得分:1)

对话系统本身就是一个非常广泛的话题。但是,我不认为你只需要一个字符串列表即可完成它。如果您正在谈论给予玩家选择,那么您将需要创建一些自定义类。

首先,您需要一个班级来定义角色的选项。这将是一个非常简单的课程。它将包含字符串响应和表示目标节点的整数。

public class DialogueOption {

    public string Text;
    public int DestinationNodeID;
}

接下来,您将需要一个DialogueNode类。考虑一下NPC告诉角色的内容,然后它还包含一个列表,你猜对了,对话选项。它将拥有的另一件事是NodeID。这是一个整数,为我们提供了一个从我们的选项发送给我们的地方。看起来有点像这样:

public class DialogueNode {

    public int NodeID = -1; //I use -1 as a way to exit a conversation.
    //NodeId should be a positive number

    public string Text;
    public List<DialogueOption> Options;
}

最后,你需要创建一个对话类,这是最简单的一个,只是一个DialogueNodes列表,看起来像这样:

public class Dialogue {

    public List<DialogueNode> Nodes = new List<DialogueNode>();
}

你可以在你的角色只有一个对话脚本的地方使用它但是,我做了不同的地方,然后每个角色都有另一个名为DialogueList的类,它基本上是一个对话列表,它还包含角色的名字,我可以显示,根据情况,我可以选择我希望我的角色在当时与玩家进行哪种对话。

public class DialogueList
{
    public string name;
    public List<Dialogue> dialogues = new List<Dialogue>();

} 

这还有一个额外的好处,即如果你想在那个方向上设置它,可以使用名称作为键返回一个对话列表,轻松转换为字典。

在项目的其他地方,你需要一个能控制一切的DialogueManager类。我通常会把它变成一个Singleton,所以我可以从任何地方轻松调用它。我不会告诉你所有这些,但我会告诉你显示的部分,其余的只是设置文本,打开和关闭对象。

public void RunDialogue(string name, Dialogue dia)
{
    nameText.text = name;

    StartCoroutine(run(dia));
}

IEnumerator run(Dialogue dia)
{
    DialoguePanel.SetActive(true);

    //start the convo
    int node_id = 0;

    //if the node is equal to -1 end the conversation
    while (node_id != -1 )
    {
        //display the current node
        DisplayNode(dia.Nodes[node_id]);

        //reset the selected option
        selected_option = -2;

        //wait here until a selection is made by button click
        while (selected_option == -2)
        {
            yield return new WaitForSeconds(0.25f);
        }

        //get the new id since it has changed
        node_id = selected_option;

    }

    //the user exited the conversation
    EndDialogue(node_id);

}

我的按钮基本上只是在我在OnPoint事件中附加了这个简单的方法,我在DisplayNode方法下设置了它。基本上,我拿出我的按钮给他们这个方法,他们的参数是他们的DialogueOption.DestinationId

public void SetSelectedOption(int x)
{
    selected_option = x;
}

答案 1 :(得分:0)

如果您要求使用对话系统,可以使用UGUI在Unity上轻松创建。

1。)创建一个包含planetext的画布,我想会做

*scale the plane ofcourse to your satisfaction and put it infront of the camera 
*and about the text you must put it infront of the plane like the plane is on the back
*and the text is infront of the plane.

2。)现在设置这样的层次

Canvas
   Dialogue //Empty GameObject
      Plane
      Text

每当你碰到类似那样的NPC时,我都会弹出它

[SerializeField] GameObject dialogueObject; //here drag the canvas
[SerializeField] GameObject text; //we will get its component for changing the text later on
void Start(){
   text = GetComponent<Text>().text = "Sample Dialogue";
   dialogueObject.setActive(false);
}

void OnCollisionEnter(collision collide){
    if(collide.gameObject.name == "NPC"){
        dialogue.setActive(true);
    }
}

这很简单。我希望它有所帮助

相关问题