Game Development Reference
In-Depth Information
//How high does the dialog window need to be
int dialogHeight = 70;
Each property explains its use, but everything will become clear as we add the rest of the
functionality. Next, we'll add a Coroutine that will take a
Conversation
object and
loop through all the lines to be displayed. Add the following function to the
Conversa-
tionManager
script:
IEnumerator DisplayConversation(Conversation conversation)
{
talking = true;
foreach (var conversationLine in
conversation.ConversationLines)
{
currentConversationLine = conversationLine;
conversationTextWidth =
currentConversationLine.ConversationText.Length *
fontSpacing;
yield return new WaitForSeconds(3);
}
talking = false;
}
This simple Coroutine takes the conversation passed to it and loops through each of the
individual lines of the conversation's text. Before we start, we set the
talking
flag to
denote that a conversation is in progress; then, for each conversation line, we perform the
following tasks:
• Set a pointer to the current conversation item in the list with the
currentCon-
versationLine
property
• Figure out how long the text is to gauge how big our display area needs to be
• Wait for three seconds before moving on to the next conversation item
• When we run out of conversation lines, we set the
talking
flag to
false
to
show that we have finished
So, we have a Coroutine looping through the text. The next thing to do is to use this in-
formation to display it on the screen. For this, we need an
OnGUI
method in our script as
follows: