【问题标题】:How can I create a dialogue system如何创建对话系统
【发布时间】:2018-05-31 09:39:28
【问题描述】:
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 与您的方法有什么关系?请给我们更多细节。
  • how can I have an array or list in which I can do use my size variable and loop it until all dialogues are said 您似乎在描述for 循环。这方式超出了范围;这可以通过阅读基本的 C# 文档来回答。 StackOverflow 不是暴力教学服务。

标签: c# arrays loops unity3d


【解决方案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;
}

最后,您需要创建 Dialogue 类,它是所有类中最简单的,只是一个 DialogueNode 列表,看起来像这样:

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

}

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

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

【讨论】:

  • 谢谢!你帮了大忙!
  • @GamingForever 如果您开始获得大量对话和内容,您可以非常轻松地保存信息并将其转换为 XML 格式。但是,如果适合您,您也可以在编辑器中完成所有操作哈哈
  • @GamingForever 请记住,如果这个答案对您有很大帮助,您可以accept它。
【解决方案2】:

嗯,如果您要求对话系统,使用 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);
    }
}

就这么简单。希望对你有帮助

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-03-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多