首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >如何创建对话系统

如何创建对话系统
EN

Stack Overflow用户
提问于 2018-05-31 17:39:28
回答 2查看 946关注 0票数 -2
代码语言:javascript
复制
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);
    }
}

请告诉我如何才能有一个数组或列表,我可以在其中使用我的大小变量,并循环它,直到所有对话都说完为止。

我试过了,但我的不起作用。帮助!

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2018-05-31 23:57:38

对话系统本身就是一个非常广泛的话题。然而,我认为你不能仅仅用一个字符串列表就能完成它。如果你谈论的是给玩家提供选择,那么你需要创建一些定制的类。

首先,你需要一个类来定义角色的选项。这将是一个非常简单的类。它将包含字符串response和一个表示目的节点的整数。

代码语言:javascript
复制
public class DialogueOption {

    public string Text;
    public int DestinationNodeID;
}

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

代码语言:javascript
复制
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列表,如下所示:

代码语言:javascript
复制
public class Dialogue {

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

你可以在你的角色只有一个对话脚本的情况下使用它,但我做的不同,每个角色都有另一个名为DialogueList的类,这个类基本上是一个对话列表,它还包含了我可以显示的角色的名称,根据情况,我可以选择我想让我的角色与玩家进行哪个对话。

代码语言:javascript
复制
public class DialogueList
{
    public string name;
    public List<Dialogue> dialogues = new List<Dialogue>();

} 

这也有一个额外的好处,可以很容易地转换为字典,使用名称作为键,如果您想要在该方向上设置它,则返回对话列表。

在您的项目中的其他地方,您将需要一个控制一切的DialogueManager类。我通常将其设置为Singleton,这样我就可以从任何地方轻松地调用它。我不会全部展示给你,但是我会展示显示部分,剩下的就是设置文本和打开和关闭对象。

代码语言:javascript
复制
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);

}

我的按钮基本上只是在我在DisplayNode方法下设置的OnClick事件中附加了这个简单的方法。基本上,我把我的按钮提供给它们这个方法,它们的参数是它们的DialogueOption.DestinationId是什么

代码语言:javascript
复制
public void SetSelectedOption(int x)
{
    selected_option = x;
}
票数 2
EN

Stack Overflow用户

发布于 2018-05-31 18:56:42

嗯,如果你想要一个对话系统,使用UGUI在Unity上创建是非常容易的。

1.)创建一个包含planetext的画布

代码语言:javascript
复制
*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.)现在像这样设置您的世袭结构

代码语言:javascript
复制
Canvas
   Dialogue //Empty GameObject
      Plane
      Text

我会让它在你撞上NPC的时候弹出来。

代码语言:javascript
复制
[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);
    }
}

就这么简单。我希望它能帮上忙

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50621200

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档