Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- using UnityEngine.UI;
- using System.Collections;
- using System.Collections.Generic;
- public class TextBoxManager : MonoBehaviour {
- public TextAsset textFile;
- public GameObject panel;
- private List<string> textLines = new List<string>();
- public Text display;
- public float displaySpeed;
- private int currentLine = 0;
- private int currentCharPosition = 0;
- private bool textRunning = false;
- private bool endText = false;
- private bool textActive = false;
- GUIStyle style = new GUIStyle();
- void Start () {
- display = GetComponentInParent<Text>();
- initializeStyle();
- string[] initialArray = textFile.text.Split('\n');
- foreach (string s in initialArray)
- textLines.AddRange(initializeText(s));
- }
- void Update () {
- //se texto nao estiver sendo apresentado na tela, botar seta no canto inferior direito
- panel.GetComponent<Animator>().SetBool("showArrow", !textRunning);
- checkPress();
- checkEndText();
- }
- public void initiateEvent() {
- panel.GetComponent<Animator>().SetBool("eventActive", true);
- StartCoroutine(displayText());
- }
- private void checkPress() {
- if (Input.GetKeyDown(KeyCode.Z) && textActive) {
- if (textRunning)
- endLine();
- else
- nextLine();
- }
- }
- private IEnumerator displayText() {
- yield return new WaitForSeconds(1.2f);
- textActive = true;
- textRunning = true;
- //enquanto ainda houver texto para ser apresentado, apresente-o
- while (!endText) {
- checkCommand(textLines[currentLine]);
- display.text = textLines[currentLine].Substring(0, currentCharPosition);
- if (currentCharPosition < textLines[currentLine].Length)
- currentCharPosition++;
- else
- textRunning = false;
- yield return new WaitForSeconds(1 / displaySpeed);
- }
- }
- //percorre a linha ate o final e caso necessario, realiza realinhamento
- private void endLine() {
- currentCharPosition = textLines[currentLine].Length;
- }
- private void nextLine() {
- if (currentLine < textLines.Count - 1) {
- currentLine++;
- resetLine();
- checkCommand(textLines[currentLine]);
- } else {
- endText = true;
- }
- }
- private void resetLine() {
- currentCharPosition = 0;
- textRunning = true;
- }
- //checa se a proxima linha possui algum comando, se possuir, executa o comando
- private void checkCommand(string txt) {
- string idCommand = string.Empty;
- string command = string.Empty;
- if (txt.StartsWith("[")) {
- txt = txt.Remove(txt.Length - 1);
- txt = txt.Remove(0, 1);
- for (int i = 0; i < txt.Length; i++) {
- if (txt.ToCharArray()[i] == '_') {
- idCommand = txt.Substring(0, i);
- command = txt.Remove(0, i+1);
- }
- }
- if (idCommand.Equals("portrait")) {
- GameObject[] portraitArray = GameObject.FindGameObjectsWithTag(idCommand);
- GameObject portrait = GameObject.Find (command);
- Debug.Log ("Portrait: " + portrait);
- if (command.Equals("end"))
- foreach (GameObject p in portraitArray)
- p.GetComponent<Image>().enabled = false;
- else if (portrait == null)
- Debug.Log ("Portrait '" + txt + "' not found.");
- else
- portrait.GetComponent<Image>().enabled = true;
- }
- if (currentLine < textLines.Count - 1)
- currentLine++;
- }
- }
- private List<string> initializeText(string s) {
- List<string> output = new List<string>();
- int j = 0;
- while (needRealign(s) || j > 10) {
- j++;
- for (int i = 0; i < s.Length; i+=10) {
- string preview = s.Substring(0, i);
- if (needRealign(preview)) {
- for (int c = preview.Length - 1; preview.ToCharArray()[c] != ' '; c--)
- preview = preview.Remove(c);
- output.Add (preview);
- s = s.Remove (0, preview.Length);
- break;
- }
- }
- }
- output.Add (s);
- return output;
- }
- //checa se o texto esta pulando pra fora da text box
- private bool needRealign(string text) {
- GUIContent content = new GUIContent(text);
- int numLines = 3;
- Vector2 textDimensions = style.CalcSize(content);
- if (textDimensions.x + 100 > display.rectTransform.rect.width * numLines)
- return true;
- return false;
- }
- private void checkEndText() {
- if (endText) {
- panel.GetComponent<Animator>().SetBool("endText", true);
- display.enabled = false;
- }
- }
- private void initializeStyle() {
- style.fontStyle = display.fontStyle;
- style.font = display.font;
- style.fontSize = display.fontSize;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement