5 Minute DIALOGUE SYSTEM in UNITY Tutorial

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
all right so in five minutes i'm going to show you how to build a dialog system like this it's definitely pretty shallow and not pretty but it's functional there's no gourmet meal happening here this is fast food but the clock's ticking so let's get into it so i have a brand new empty unity project here and the first thing we want to do is right click in our hierarchy and go down to ui and we want to add in a panel this panel is going to be our dialog box so we can go ahead and name it such and then before we do anything let's click on our canvas first and configure that we'll go down to the canvas scaler and set ui scale mode to scale with screen size you can set the x and y values to 3820 by 2160 if you're targeting 4k or 1920x1088 for targeting hd which is what i'll be doing then we'll click back to our dialog box now you don't even have to use the dialog box or you can make it look however you want but what i'm going to do is go down to the color here and make the opacity 100 and then in the inspector i'm just going to fill in some rec values i was playing with before but we'll be doing 100 for the left 500 for the top 100 for the right and 100 from the bottom and it should look something like a dialog box by the time you're done filling that out we can then right click on dialog box and go down to ui and we're going to use text text mesh pro which is a much more high quality text object and it'll ask us here if we want to import the essentials which we do so we'll press import once that's done importing the essentials we can click on our text object that has been created we'll come down here to vertex color and open it up and i'm just going to make the color black we can set it to bold and i'm going to up the font size to something like 70 and then once again we're going to change the rect values in the inspector so i'm just going to set the width and height to 1500 by 300. with those values in the scene editor you'll see the text box itself fit nice and snug within the dialog box we can now come down to the projects tab and right click on assets and go to create c-sharp script and i'm going to call this dialog let's drag the dialog script onto the dialog box and then double-click the script to open it up alright so first things first in the script at the top of this page let's add a using statement so using tm pro this is to make sure we can reference this text mesh pro component and we'll go ahead and start with all the variables we're going to need for the script so the first thing like we just mentioned we need a reference to our text mesh pro component so we'll say public text mesh pro ugui and i'll call this text component we then need a collection of different sentences or lines of dialogue we want to display i'm just going to use a string array for simplicity so i'll say public string open close square brackets and i'm going to call this lines we're also going to type out each character at a specific speed so we need to capture that speed so we'll say public float text speed and then finally we just need like a private index to track where we are within the conversation so we'll say private int index so let's go ahead and do this underneath the start and update methods we'll go ahead and make a new method called void start dialog and then here we just want to set our index equal to zero for now let's just leave it at that let's go ahead and create a co routine under this the syntax for this is a little different so we'll say i enumerator as the return type and we'll call this type line and so here what we're trying to do is we're going to type out each character one by one so we can simply say for each car for character in our lines collection of the current index and then we'll say dot to car array so what this says it takes the string and then breaks that down into a character array we simply want to say text component dot text plus equals c and with msa yield return new wait for seconds and then we want to pass in the text speed back in start dialog underneath index equals zero let's say start co-routine and then for an argument we'll pass in type line which is the method we just created and then in our start method what we want to do is say text component dot text equals string dot empty and then we just want to call our start dialog method at this point if you go into unity and select your dialog box let's drag in our text object into the text component we can set the text speed to something like 0.3 and then we can open up the lines collection and i'll set the size to something like 2 so then when we start the game you can see that it's typing it out character by character but we don't have any way to move to the next line of dialogue so let's work on that now let's create a new method called void next line we can say if index is less than our lines that length minus one because it's an array we can say index plus plus and then set the text component.text equal to string.empty and we'll say start co-routine of typeline else if we don't have anything else to say in our dialogue we can just set the game object for now to inactive so we'll say gameobject.setactive false we can then go up to our update method what we're going to say is get if input.getmousebutton down and then we'll pass in zero for the left mouse button if our text component dot text is equal to our lines at the index then we just want to proceed to the next line otherwise if it's not equal we want to stop all co-routines and we want to set our text component.text equal to our lines index so this will get the current line and just instantly fill it out and so now we can see when we play it we can click and it instantly fills out the messaging we can click again to proceed to the next line and when the dialogue's finished we can click and it closes the dialog box
Info
Channel: BMo
Views: 126,306
Rating: undefined out of 5
Keywords: bmo, unity, tutorial, how to, unity tutorial, dialogue system, dialog system, conversation, talking npc, narrative, beginner, c#, game development, game dev, dialogue system in unity, dialogue tutorial, dialog tutorial, dialog system in unity, unity3d, unity2d, 5 minute tutorial, quick tutorial, unity dialogue tutorial
Id: 8oTYabhj248
Channel Id: undefined
Length: 5min 10sec (310 seconds)
Published: Fri Mar 19 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.