How To Create A Dialogue System In Unity (BASIC)

Notes:

  • 0:58 Create a C# script called DialougeManager
  • 1:06 Create an empty gameobject to hold the DialougeManager script
  • 1:23 Open the script in Visual Studio and delete the Update() method
  • 1:54 Use a queue for the sentences rather than a string array
  • 1:56 Use the System.Collections namespace to use a queue
  • 2:00 How does a queue work?
  • 2:26 Define a type for the queue. This will be of type string.
  • 2:29 Initialize the queue in the Start() method
  • 2:48 Create another C# script called Dialogue
  • 2:51 What is the new Dialogue class for?
  • 3:07 Ensure the Dialogue class does not derive from MonoBehavior
  • 3:13 Delete the Start() and Update() methods in Dialogue
  • 3:20 In the Dialogue class, create a string array of the sentences that will be loaded into the conversation
  • 3:30 Create a string variable for the name of the NPC we’re talking to
  • 3:33 Use System.Serializable above the definition of the class to make the class editable in the inspector
  • 3:57 Create a third C# script called DialogueTrigger
  • 4:30 In the DialogueTrigger class, delete the Start() and Update() methods, and create a variable of type Dialogue referencing the dialogue class
  • 4:35 We now have a dialogue variable where we can change the name and edit sentences from the inspector
  • 4:41 Edit sentence size to create more sentences
  • 5:02 In the Dialogue class, specify the text area. Inside the parentheses of the function, specify the minimum amount of lines used by a sentence and the maximum amount of lines used by a sentence.
  • 5:55 In the DialogueTrigger class, create a TriggerDialogue() function
  • 6:15 Find the DialogueManager using FindObjectOfType<>()
  • 6:24 Call the StartDialogue function on the DialogueManager object. Pass in our dialogue variable as an argument.
  • 6:39 Create the StartDialogue() function. Have it take in a Dialogue object.
  • 7:51 In the StartDialogue() function, call the Clear() method on the sentences array
  • 7:56 Go through all of the strings in the sentences array with a foreach loop
  • 8:00 foreach(string sentence in dialogue.sentences)
  • 8:06 Queue up a sentence in the foreach loop
  • 8:20 Call the DisplayNextSentence() function
  • 8:25 Create the DisplayNextSentence() function
  • 8:37 In the DisplayNextSentence() function, check if there are any more sentences in the queue
  • 8:48 If there are no more sentences in the queue, call the EndDialogue() function and return
  • 8:55 Create the EndDialogue() function
  • 9:09 In the DisplayNextSentence function, if we still have sentences left in the queue, load the next sentence in the queue. Store it in a string variable called sentence.
  • 10:16 Ensure the script is using the UnityEngine.UI namespace
  • 10:24 Create name and dialogue text variables
  • 10:35 In the DisplayNextSentence() function, set the dialogue text variable’s text component equal to the current sentence being loaded
  • 10:47 Set the name variable’s text component equal to the name in the Dialogue class

Leave a Reply

Your email address will not be published. Required fields are marked *