示例#1
0
        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); });
                }
            }
        }
示例#2
0
        public void DisplayCharacterDialogue(string dialogue, string characterName = null, DialogueAdvanceDelegate dialogueAdvanceDelegate = null)
        {
            Debug.Log(characterName + " says: " + dialogue);
            // Place dialogue
            Vector2 dialoguePoint = new Vector2(Screen.width * 0.5f, Screen.height * 0.5f);

            if (characterName != null)
            {
                float      height         = 0;
                Vector3    offsetPosition = Vector3.zero;
                GameObject speaker        = GameObject.Find(characterName);
                if (speaker != null)
                {
                    BoxCollider2D collider = speaker.GetComponent <BoxCollider2D>();
                    if (collider != null)
                    {
                        height = collider.size.y;
                    }
                    offsetPosition = new Vector3(speaker.transform.position.x, speaker.transform.position.y + height + kDialogueVerticalOffset, speaker.transform.position.z);
                }

                Camera cam = FindObjectsOfType <Camera>().First();
                if (cam != null)
                {
                    Vector3 screenPosition = cam.WorldToViewportPoint(offsetPosition);
                    Debug.Log(screenPosition);
                    dialoguePoint = new Vector2(screenPosition.x * canvas.GetComponent <RectTransform>().sizeDelta.x, screenPosition.y * canvas.GetComponent <RectTransform>().sizeDelta.y);
                }
            }

            GameObject dialogueObject;

            if (autoCreateSpokenText || dialogueBoxPrefab == null)
            {
                dialogueObject = new GameObject("Current Dialogue");
                Text dialogueText            = dialogueObject.AddComponent <Text>();
                ContentSizeFitter sizeFitter = dialogueObject.AddComponent <ContentSizeFitter>();
                sizeFitter.horizontalFit = ContentSizeFitter.FitMode.PreferredSize;
                sizeFitter.verticalFit   = ContentSizeFitter.FitMode.PreferredSize;
                dialogueText.text        = dialogue;
                // TODO(laurenfrazier): Will each character have their own font/size/color?
                dialogueText.font     = menuFont;
                dialogueText.fontSize = 44;
            }
            else
            {
                dialogueObject = Instantiate(dialogueBoxPrefab);
                dialogueObject.SetActive(true);
                DialogueMenu dialogueMenu = dialogueObject.GetComponentInChildren <DialogueMenu>();
                if (dialogueMenu != null)
                {
                    if (characterName != null)
                    {
                        dialogueMenu.AddTitle(characterName);
                    }
                    dialogueMenu.AddDescription(dialogue);
                }
            }
            dialogueObject.transform.SetParent(canvas.transform, true);
            dialogueObject.transform.position = dialoguePoint;
            currentlyDisplayedDialogueBox     = dialogueObject;

            // When user dismisses by touching the screen, call callback
            this.dialogueAdvanceDelegate = dialogueAdvanceDelegate;
            if (screenTouchPanel != null)
            {
                screenTouchPanel.SetActive(true);
            }
            awaitingDialogueAdvance = true;
        }