Today I want to show you a small Dialog System for Unity. You can find it on my GitHub and use it freely. Here’s the license. It will enable you to trigger dialogues that will be then displayed in a Text Element. You can then skip through each line of dialog. For now, it is not possible to have a dialog tree with different answering options.
Example Use
You’re making a platformer and need some NPC to explain the character how to move? First, you need two text objects (one for the dialog text and one for the NPCs name). You probably also will need a button to skip through the dialog lines. Put a DialogController on an empty object in your scene and set it up (drop in all the text references in the inspector).
Next, add a DialogTrigger script to your NPC. In the inspector, you can add all the dialogues you want the NPC to say. Now your NPC can make use of the DialogTrigger (i.e. in your NPC script). Trigger the TriggerAllDialogues() function to put the dialogues in the dialog queue. Nothing is displaying yet, because you have to invoke the Continue() function first. It is used to start a dialog and also to proceed to the next dialog line.

The DialogController
This component is the core of the dialog system. You can use only one, but it is also possible to have multiple, because you can choose which DialogController is used by which DialogTrigger by choosing to reference it in the inspector of the DialogTrigger.
If you add multiple dialogues to the queue, they will be just playing after each other in the order they were added. You can also test the DialogController without a DialogTrigger by using the debugging dialogues.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class DialogController : MonoBehaviour
{
public Text titleText;
public Text dialogText;
public Text dialogButtonText;
public Text dialogCount;
public Queue<Dialog> dialogues = new Queue<Dialog>();
public Dialog[] debugDialogues;
private void Start()
{
Continue();
}
public void AddDebugDialog()
{
foreach(Dialog d in debugDialogues)
{
AddDialog(d);
}
}
public void AddDialog(Dialog d)
{
int index = 0;
foreach(string line in d.lines)
{
Dialog tempDialog = new Dialog();
tempDialog.lines = new string[1];
if (d.onlyShowTitleOnFirstLine && index > 0)
{
tempDialog.title = "";
}
else
{
tempDialog.title = d.title;
}
tempDialog.lines[0] = line;
Debug.Log("Added: " + line);
dialogues.Enqueue(tempDialog);
index++;
}
if (dialogCount)
{
dialogCount.text = "Queued Lines: " + dialogues.Count;
}
}
public void Continue()
{
if (dialogCount)
{
dialogCount.text = "Queued Lines: " + dialogues.Count;
}
if (dialogues.Count > 0)
{
Dialog d = dialogues.Dequeue();
titleText.text = d.title;
dialogText.text = d.lines[0];
}
else
{
titleText.text = "";
dialogText.text = "";
}
if (dialogText.text == "")
{
dialogButtonText.text = "Start Dialog";
}
else
{
dialogButtonText.text = "Continue";
}
}
}
The Dialog
This class houses every aspect of the dialog. You can define the dialogName which can be used to refer to specific dialogues via the DialogTrigger. The title could be the name of a NPC and will be displayed in the DialogSystem’s titleText. The dialog itself consists of lines. You can add as many as you want. They will be displayed one at a time in the dialogText of the DialogSystem.
Additionally, you have the option to onlyShowTitleOnFirstLine, which will exactly do that. If disabled, the title will be displayed with each line. If enabled, the title goes blank after the first line of the dialog.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[System.Serializable]
public class Dialog
{
public string dialogName;
public string title;
public bool onlyShowTitleOnFirstLine;
[TextArea(2,10)] public string[] lines;
}
The DialogTrigger
With the DialogTrigger you have several options for triggering dialogues. The first one would be to just trigger all the dialogues that it has attached to it with TriggerAllDialogues(). But you can also use TriggerDialog(string dialogName) to trigger all attached dialogues with a certain dialogName (i.e. a dialog for greeting the player or a dialog specific to a quest). Last but not least you can also trigger a dialog via its index with TriggerDialog(int dialogIndex).
If you have not set a DialogController in the inspector, the DialogTrigger will search for one. This can lead to problems if you have more thqn one and I would recommend you just set it up beforehand. Therefore I’ve added a little warning in case you don’t do it. But as long as at least one DialogController is in your scene it should work.
As a bonus, you can specify what should be put in the titleText of the DialogController if the title is left blank in the Dialog. So fillBlankTitlesWith is the perfect place to put the name of an NPC. This way you don’t have to write it in every separate Dialog and instead just leave it blank.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class DialogTrigger : MonoBehaviour
{
public DialogController dialogController;
[Space(10)]
public string fillBlankTitlesWith;
public Dialog[] dialogues;
private void OnEnable()
{
if (!dialogController)
{
dialogController = FindObjectOfType<DialogController>();
Debug.LogWarning("DialogController needed for DialogTrigger. It was automatically added.");
}
}
public void TriggerDialog(int dialogIndex)
{
CheckTitle();
if (dialogIndex < dialogues.Length && dialogIndex >= 0)
{
dialogController.AddDialog(dialogues[dialogIndex]);
}
else
{
Debug.LogWarning("No dialogue with index of " + dialogIndex + " found.");
}
}
public void TriggerDialog(string dialogName)
{
CheckTitle();
foreach (Dialog d in dialogues)
{
if(d.dialogName == dialogName)
{
dialogController.AddDialog(d);
}
}
}
public void TriggerAllDialogues()
{
CheckTitle();
foreach (Dialog d in dialogues)
{
dialogController.AddDialog(d);
}
}
private void CheckTitle()
{
foreach(Dialog d in dialogues)
{
if (d.title == "" && fillBlankTitlesWith != "")
{
d.title = fillBlankTitlesWith;
}
}
}
}
Creating Dialogues
I love it to create systems like that. I’ve made several dialog systems by now, but I chose a slightly different approach this time. I liked how it turned out and I think it is easy to add additional functionality to it. Maybe I get back to it and add a dialog tree and the option to show pictures of what you are talking with (i.e. an image of the face of an NPC).
Thank you for reading and have fun!
If you have any questions or suggestions, feel free to message me.