using System.Collections; using UnityEngine; using UnityEngine.UI; /// /// This Manager is singleton that handles teh display and running of conversations in the game. Other components will /// use the StartConversation method to launch a pre-defined conversation component. /// public class ConversationManager : Singleton { //Is there a converastion going on bool talking = false; //The current line of text being displayed ConversationEntry currentConversationLine; //the Canvas Group for the dialog box public CanvasGroup dialogBox; //the image holder public Image imageHolder; //the text holder public Text textHolder; //Guarantee this will always be a singleton only - can't use the constructor! protected ConversationManager() { } /// /// Starts the conversation by getting a reference to the dialog UI and then calling a /// coroutine to show the content of the conversation. /// /// Conversation. public void StartConversation(Conversation conversation) { dialogBox = GameObject.Find("Dialog Box").GetComponent(); imageHolder = GameObject.Find("Speaker Image").GetComponent(); textHolder = GameObject.Find("Dialog Text").GetComponent(); //Start displying the supplied conversation if (!talking) { StartCoroutine(DisplayConversation(conversation)); } } /// /// This coroutine simply iterates through each of the entries in the conversation and displays the content /// of the entry in the dialog UI. It waits 3 seconds between entries. This is a very simple implementation of /// a conversation that does not maintain state or allow for player input or phrase options. /// /// The conversation. /// Conversation. IEnumerator DisplayConversation(Conversation conversation) { talking = true; foreach (var conversationLine in conversation.ConversationLines) { currentConversationLine = conversationLine; textHolder.text = currentConversationLine.ConversationText; imageHolder.sprite = currentConversationLine.DisplayPic; yield return new WaitForSeconds(3); } talking = false; } /// /// This method handles showing and hiding the dialog box for the conversation. It is a /// simple alpha switch from visible to invisible. /// void OnGUI() { if (talking) { dialogBox.alpha = 1; dialogBox.blocksRaycasts = true; } else { dialogBox.alpha = 0; dialogBox.blocksRaycasts = false; } } }