• Advertisement
Sign in to follow this  

Displaying dialogue in Unity

This topic is 694 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hey everyone!

 

I'm working on an RPG and I need to display the dialogue the NPCs speak, but I have very little experience with Unity's UI functionality. I want to display the dialogue in a style very much like Undertale's style, meaning, a box that starts blank and gets filled with text gradually, sometimes making short pauses to imitate a person's speaking rhythm. Check out some Undertale videos if you don't understand what I'm talking about.  

 

Edit: The short pauses are actually kind of optional (I would rather lose them if it's really complicated implementing them). Also, to go to the next line of dialogue the player would have to press a button. 

 

Thank you very much and have an awesome day! :)

Edited by ForkyTheEditor

Share this post


Link to post
Share on other sites
Advertisement

What UI System are you using?

 

I know, stuff like that is quite easy to do using NGUI. I did some extensive work with NGUI, involving rolling text, scrolling windows and all. You can update NGUI Widgets ingame, so set up a script that changes the text attribute of the NGUI Widget showing the text (a "label", most of the time).

 

As for the new UGUI: https://docs.unity3d.com/ScriptReference/GUI.Label.html

 

I guess you are using Labels to contain the text displayed in your UI Windows. Did you already try to update the text ingame?

If you don't know what I mean, I am trying to explain this fairly quickly.

 

- Set up your label, no text assigned at first. Add the label to the window to make sure the text is displayed at the right place (will not go into UGUI Specifics, haven't really used it myself before)

 

- Create a C# or Javascript Script, that has access to that label (either add it to the label gameobject and use this, or a public global variable that you assign the label to in the editor).

 

- Make you script react to an event (like a player entering a trigger), or just act upon time "ticks" at first to test out your script (hold a variable containing the update frequency, and a variable holding the last update timestamp, compare this to the game time (this is explained in details in some tutorials. I think you will find them with quick googling if you don't know how to do that already))

 

- Now, everytime your script reaches a "tick event" to update the text, add another letter to the text shown in the label. Of course, you might need to do some formatting magic to make the label stable at the same position, and to handle new lines at the right time (as said, don't know UGUI enough to really know what you need to do exactly. In NGUI you could define anchors, so you could anchor the top left corner of the label, and set the label width/height, without any need for additional "magic", until Text had to be "rolled up").

 

- That should take care of text appearing, until you reach the max size of the window. Now, once that is reached, you might need to get more involved. I will now describe how I would do it in NGUI (UGUI might be similar, or not, IDK):

1) There are many ways how you can limit text to only be visible inside a bounding box in NGUI.

I would now just move the label up, so the text at the top rolls up and disappears.

2) After the amount of text that should disappear is invisible, and the lower line of text (if any should still be visible) is rolled up to the same position as the line it replaces, I will reset the text so the top lines are removed, move the label down again (in a single frame, so this is invisible to the player), thus I cleaned up the label so it only renders the text that is visible, and not all the suff that is not used.

 

This might need some additional work from you, as UGUI might treat widgets "overflowing" other widgets differently than NGUI. You can go old school and use a fat border hiding the rolling up text, maybe UGUI allows you some control if text should be visible if it goes outside of the containing widget.

Maybe this video has more information:

 

https://unity3d.com/learn/tutorials/modules/beginner/ui/ui-scroll-rect

 

 

TL;DR: Keep in mind that UI Elements are highly scriptable like just about everything else in Unity. You want some fancy UI Effect? As long as you can code it, you can make it happen.

Share this post


Link to post
Share on other sites

NGUI is close enough to Unity's built Canvas / UI that that above is pretty applicable.

 

I've done similar things before with integers, to make score appear like it's rolling upwards instead of changing immediately, it's mostly about having something like a coroutine that gets called that slowly trickles in the text at a set rate.  You may have to add some sort of special character or something for the long pauses.  For example, you're not likely to use the | character much in dialog, so you could have the function not display that character when it comes time to add it, and instead pause for a longer period of time.

Share this post


Link to post
Share on other sites

My recommendation is also to use a Canvas, and nest inside it a panel with a Text element added. Also have a background image and other elements as you see fit, like a blinking image indicating more text.  

 

From there you might also want to nest the panel inside a ScrollRect and add scroll bars, or write script to replace the text if you prefer.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement