private void HandleDialogueOptionClick(int dialogueOption, DialogueSelectionDelegate dialogueSelectionDelegate = null) { DestroyDialogueBox(); if (dialogueSelectionDelegate != null) { dialogueSelectionDelegate(dialogueOption); } }
public void DisplayDialogueOptions(string[] dialogueOptions, string description = null, DialogueSelectionDelegate dialogueSelectionDelegate = null) { // If we are only showing a dialogue box, get rid of it before we display the next one. DestroyDialogueBox(); // Create fresh dialogue box and add to screen GameObject dialogueBox; float boxWidth = 0; float boxHeight = 0; int boxFontSize = 14; float offsetX = Screen.width - boxWidth; float offsetY = Screen.height - boxHeight; if (autoCreateDialogueMenus || dialogueBoxPrefab == null) { dialogueBox = new GameObject(); if (borderSprite != null) { dialogueBox.AddComponent <Image>().sprite = borderSprite; } // Set size of dialogue box switch (dialogueOptionMenuSize) { case DialogueOptionMenuSize.SMALL: { boxWidth = (float)Screen.width * 0.3f; boxHeight = (float)Screen.height * 0.3f; boxFontSize = 14; break; } case DialogueOptionMenuSize.MEDIUM: { boxWidth = (float)Screen.width * 0.5f; boxHeight = (float)Screen.height * 0.5f; boxFontSize = 24; break; } case DialogueOptionMenuSize.LARGE: { boxWidth = (float)Screen.width * 0.8f; boxHeight = (float)Screen.height * 0.8f; boxFontSize = 34; break; } default: { // Should never hit this, just bail and pick arbitrary size boxWidth = 100f; boxHeight = 100f; break; } } // Set placement of dialogue box switch (dialogueOptionMenuPlacement) { case DialogueOptionMenuPlacement.BOTTOM: { dialogueBox.GetComponent <RectTransform>().offsetMin = new Vector2(offsetX / 2.0f, 0); // left, bottom dialogueBox.GetComponent <RectTransform>().offsetMax = new Vector2(-1.0f * (offsetX / 2.0f), -1.0f * offsetY); // -right, -top break; } case DialogueOptionMenuPlacement.CENTER: { dialogueBox.GetComponent <RectTransform>().offsetMin = new Vector2(offsetX / 2.0f, offsetY / 2.0f); // left, bottom dialogueBox.GetComponent <RectTransform>().offsetMax = new Vector2(-1.0f * (offsetX / 2.0f), -1.0f * (offsetY / 2.0f)); // -right, -top break; } case DialogueOptionMenuPlacement.LEFT: { dialogueBox.GetComponent <RectTransform>().offsetMin = new Vector2(0, offsetY / 2.0f); // left, bottom dialogueBox.GetComponent <RectTransform>().offsetMax = new Vector2(-1.0f * offsetX, -1.0f * (offsetY / 2.0f)); // -right, -top break; } case DialogueOptionMenuPlacement.RIGHT: { dialogueBox.GetComponent <RectTransform>().offsetMin = new Vector2(offsetX, offsetY / 2.0f); // left, bottom dialogueBox.GetComponent <RectTransform>().offsetMax = new Vector2(0, -1.0f * (offsetY / 2.0f)); // -right, -top break; } case DialogueOptionMenuPlacement.TOP: { dialogueBox.GetComponent <RectTransform>().offsetMin = new Vector2(offsetX / 2.0f, offsetY); // left, bottom dialogueBox.GetComponent <RectTransform>().offsetMax = new Vector2(-1.0f * (offsetX / 2.0f), 0); // -right, -top break; } default: { break; } } } else { dialogueBox = Instantiate(dialogueBoxPrefab); dialogueBox.SetActive(true); } dialogueBox.transform.SetParent(canvas.transform, true); currentlyDisplayedDialogueBox = dialogueBox; // Set up optional title text if (description != null) { if (autoCreateDialogueMenus) { GameObject descriptionObject = new GameObject("Description"); Text descriptionText = descriptionObject.AddComponent <Text>(); descriptionText.text = description; descriptionText.font = menuFont; descriptionText.fontSize = boxFontSize; descriptionText.color = menuTextColor; descriptionObject.transform.SetParent(dialogueBox.transform, false); } else { DialogueMenu dialogueMenu = dialogueBox.GetComponentInChildren <DialogueMenu>(); dialogueMenu.AddDescription(description); } } // Set up dialogue selections for (int i = 0; i < dialogueOptions.Length; i++) { string dialogueOption = dialogueOptions[i]; int dialogueIndex = i; if (autoCreateDialogueMenus) { Button dialogueOptionButton = Instantiate(naiveActionButton); Text buttonText = dialogueOptionButton.GetComponentInChildren <Text>(); buttonText.text = dialogueOption; buttonText.font = menuFont; buttonText.fontSize = boxFontSize; dialogueOptionButton.name = dialogueOption; dialogueOptionButton.transform.SetParent(dialogueBox.transform, false); dialogueOptionButton.onClick.AddListener(delegate { HandleDialogueOptionClick(dialogueIndex, dialogueSelectionDelegate); }); } else { DialogueMenu dialogueMenu = dialogueBox.GetComponentInChildren <DialogueMenu>(); dialogueMenu.AddButton(dialogueOption, delegate { HandleDialogueOptionClick(dialogueIndex, dialogueSelectionDelegate); }); } } }