/// <summary> /// Creates the speech bubble. /// </summary> /// <returns>The speech bubble.</returns> /// <param name="speakerPos">Speaker position.</param> /// <param name="dialogue">Dialogue.</param> /// <param name="offset">Offset.</param> /// <param name="displayText">Display text.</param> public GameObject CreateSpeechBubble(Transform speakerPos, DialogueChain dialogue, Vector2 offset, ref GameObject displayText) { Debug.Log("CreateSpeechBubble called."); //Canvas canvas; GameObject tBox = (GameObject)GameObject.Instantiate(charTextBox); tBox.transform.SetParent(_uiContainer.transform, false); //Get reference to response box and its script then disable it for now rBox = tBox.transform.Find("ResponseBox").gameObject; currResponseScript = tBox.GetComponentInChildren <ResponseBoxScript>(); rBox.SetActive(false); //Rect transform for UI element RectTransform uiBoxRect = tBox.GetComponent <RectTransform>(); //Get RectTransform for canvas RectTransform CanvasRect = CanvasObject.GetComponent <RectTransform>(); //Calculate position for the UI Element - 0,0 for the canvas is at the cneter of the screen, whereas World to viewPortPoint //treats the lower left as 0,0. Because of this, you need to subrat the height/width of the canvas * 0.5 to get the correct pos Vector2 viewportPosition = Camera.main.WorldToViewportPoint(speakerPos.position); Vector2 speakerScreenPos = new Vector2( ((viewportPosition.x * CanvasRect.sizeDelta.x) - (CanvasRect.sizeDelta.x * 0.5f)), ((viewportPosition.y * CanvasRect.sizeDelta.y) - (CanvasRect.sizeDelta.y * 0.5f))); /*position over desired character. Y offset needs to be double as sprite's origin pos is at its center*/ Vector2 uiObjPos = new Vector2(speakerScreenPos.x, speakerScreenPos.y + (offset.y * 2f)); uiBoxRect.anchoredPosition = uiObjPos; /*Create Text*/ displayText = (GameObject)GameObject.Instantiate(uiText); displayText.transform.SetParent(_uiContainer.transform, false); /*Set text position*/ RectTransform textRect = displayText.GetComponent <RectTransform>(); /*Zero it within parent*/ textRect.anchoredPosition = uiObjPos; /*Set flag to display string arguments*/ DialogueActive = true; StartCoroutine(PopulateText(dialogue)); /*Return ui text box*/ return(tBox); }
public void CloseTextBox() { //Clean up objects from dialogue GameObject tBox = currTextBox; GameObject text = currText; //Reset indices for dialog positions CurrDialogueIndex = 0; newDialogueIndex = 0; //TextBoxScript tScript= currTextBox.GetComponent<TextBoxScript>(); //delete box currTextBox = null; rBox = null; currResponseScript = null; Destroy(tBox); //delete text currText = null; Destroy(text); //Reset speaker script reference; speaker = null; //Update player state PlayerController._instance.EndDialogue(); }