高效的基于树的对话系统?

时间:2017-02-04 12:49:42

标签: unity3d

我正在开发一个简单的基于树的对话系统。它是一个点击推进故事的基本游戏,有不同的选择可供选择,引领玩家走向不同的故事路径。

我做的是,我创建了大量名为Convo的gameObject。每个Convo包含Text(Component)和Children以确定屏幕上的哪个角色,以及这些孩子中的标签以显示他们的变形(例如悲伤的快乐等)。 实施例

  • A-0-1(文字:"约翰:嘿,发生什么事了?")(标签"选择"如果它是'最后一次)
    • John(带标签"空闲")
    • Jane(带标签"激怒")

Story path picture

有2个变量:CurrentDialogue(string)和DialogueTracking(int)。

每次点击,我都会检查当前的convo是否有标签" Choice"。如果没有,我使用CurrentDialogue(字符串)查找具有确切名称的游戏对象并显示它的文本。然后将DialogueTracking加1,

然后我使用DialogueTracking将当前对话(字符串)从A-0-1修改为A-0-2,以及当有标记"选择" (例如A-0-4),DialogueTracking(int)重置为1,所以在玩家选择之后它是A-1-1而不是A-1-5。

在这里出现了问题,在经过大量的调情和许多故事之后,我意识到我的新手错误。代码变成了大的代码。 if / else,(比较哪个选择哪个路径)

我是编程和团结的新手,如果有人能指导我正确的方向,帮助我学习如何创建这个系统,使其干净,高效,那真的很棒!

谢谢!

1 个答案:

答案 0 :(得分:1)

我建议制作Component [Serializable] public class Dialogue { [SerializeField] Dictionary<string, Dialogue> _dialogues; [SerializeField] string _dialogueText; public Dialogue(string text) { _dialogues = new Dictionary<string, Dialogue>(); _dialogueText = text; } public string GetText() { return _dialogueText; } public bool HasManyPaths() { return _dialogues.Count > 1; } public string[] GetKeys() { return _dialogues.Keys; } public Dialogue GetDialogue(string key) { return _dialogues[key]; } } 来保存所有对话,其中每个都应该引用其他对话。然后在显示对话框后,您可以检查它是否有多个孩子或只有一个(或没有),并显示一些对话框/弹出窗口,以便从这些键中选择。

在代码示例中,它将是:

Unity

现在,您应该在DialogueManager检查员中填写此内容,并Component Dialogue来管理您的public class DialogueManager : Component { [SerializeField] Dialogue _currentDialogue; public void InitializeDialogue() { string text = _currentDialogue.GetText(); // display the text if ( _currentDialogue.HasManyPaths() ) { string[] keys = _currentDialogue.GetKeys(); // display keys; string selected_key = // user input here _currentDialogue = _currentDialogue.GetDialogue(selected_key); } else { string key = _currentDialogue.GetKeys()[0]; _currentDialogue = _currentDialogue.GetDialogue(key); } } }

MonoBehaviour

使用此方法,您只需在Unity编辑器中进行对话,并添加public void Start() { GetComponent<DialogueManager>().InitializeDialogue(); } 脚本,如下所示:

{ currentDialogue [ "hello! choose 1 or 2" ]
    1.{ dialogue [ "you've chosen 1" ]
        . { dialogue [ "this ends dialogue" ] }
    }
    2. { dialogue [ "you've chosen 2" ]
        . { dialogue [ "now choose another number between 3 and 4" ]
            3. { dialogue [ "you've chosen 3" ] }
            4. { dialogue [ "you've chosen 4" ] }
         }
     }
}

制作对话树的简单例子是:

docs/msw/install.txt