Advertisement
vonmunch

Typewriter Normal

Feb 7th, 2016
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.39 KB | None | 0 0
  1. using UnityEngine;
  2. using UnityEngine.UI;
  3. using System.Collections;
  4. using System.Collections.Generic;
  5.  
  6. public class TextBoxManager : MonoBehaviour {
  7.     public TextAsset textFile;
  8.     public GameObject panel;
  9.    
  10.     private List<string> textLines = new List<string>();
  11.     public Text display;
  12.     public float displaySpeed;
  13.    
  14.     private int currentLine = 0;
  15.     private int currentCharPosition = 0;
  16.    
  17.     private bool textRunning = false;
  18.     private bool endText = false;
  19.     private bool textActive = false;
  20.  
  21.     GUIStyle style = new GUIStyle();
  22.    
  23.     void Start () {
  24.         display = GetComponentInParent<Text>();
  25.         initializeStyle();
  26.  
  27.         string[] initialArray = textFile.text.Split('\n');
  28.         foreach (string s in initialArray)
  29.             textLines.AddRange(initializeText(s));
  30.     }
  31.    
  32.     void Update () {
  33.         //se texto nao estiver sendo apresentado na tela, botar seta no canto inferior direito
  34.         panel.GetComponent<Animator>().SetBool("showArrow", !textRunning);
  35.         checkPress();
  36.         checkEndText();
  37.     }
  38.    
  39.     public void initiateEvent() {
  40.         panel.GetComponent<Animator>().SetBool("eventActive", true);
  41.         StartCoroutine(displayText());
  42.     }
  43.    
  44.     private void checkPress() {
  45.         if (Input.GetKeyDown(KeyCode.Z) && textActive) {
  46.             if (textRunning)
  47.                 endLine();
  48.             else
  49.                 nextLine();
  50.         }
  51.     }
  52.    
  53.     private IEnumerator displayText() {
  54.         yield return new WaitForSeconds(1.2f);
  55.         textActive = true;
  56.         textRunning = true;
  57.        
  58.         //enquanto ainda houver texto para ser apresentado, apresente-o
  59.         while (!endText) {
  60.             checkCommand(textLines[currentLine]);
  61.             display.text = textLines[currentLine].Substring(0, currentCharPosition);
  62.            
  63.             if (currentCharPosition < textLines[currentLine].Length)
  64.                 currentCharPosition++;
  65.             else
  66.                 textRunning = false;
  67.            
  68.             yield return new WaitForSeconds(1 / displaySpeed);
  69.         }
  70.     }
  71.    
  72.     //percorre a linha ate o final e caso necessario, realiza realinhamento
  73.     private void endLine() {
  74.         currentCharPosition = textLines[currentLine].Length;
  75.     }
  76.    
  77.     private void nextLine() {
  78.         if (currentLine < textLines.Count - 1) {
  79.             currentLine++;
  80.             resetLine();
  81.             checkCommand(textLines[currentLine]);
  82.         } else {
  83.             endText = true;
  84.         }
  85.     }
  86.    
  87.     private void resetLine() {
  88.         currentCharPosition = 0;
  89.         textRunning = true;
  90.     }
  91.  
  92.     //checa se a proxima linha possui algum comando, se possuir, executa o comando
  93.     private void checkCommand(string txt) {
  94.         string idCommand = string.Empty;
  95.         string command = string.Empty;
  96.  
  97.         if (txt.StartsWith("[")) {
  98.             txt = txt.Remove(txt.Length - 1);
  99.             txt = txt.Remove(0, 1);
  100.  
  101.             for (int i = 0; i < txt.Length; i++) {
  102.                 if (txt.ToCharArray()[i] == '_') {
  103.                     idCommand = txt.Substring(0, i);
  104.                     command = txt.Remove(0, i+1);
  105.                 }
  106.             }
  107.  
  108.             if (idCommand.Equals("portrait")) {
  109.                 GameObject[] portraitArray = GameObject.FindGameObjectsWithTag(idCommand);
  110.                 GameObject portrait = GameObject.Find (command);
  111.  
  112.                 Debug.Log ("Portrait: " + portrait);
  113.  
  114.                 if (command.Equals("end"))
  115.                     foreach (GameObject p in portraitArray)
  116.                         p.GetComponent<Image>().enabled = false;
  117.                 else if (portrait == null)
  118.                     Debug.Log ("Portrait '" + txt + "' not found.");
  119.                 else
  120.                     portrait.GetComponent<Image>().enabled = true;
  121.             }
  122.  
  123.             if (currentLine < textLines.Count - 1)
  124.                 currentLine++;
  125.         }
  126.     }
  127.  
  128.     private List<string> initializeText(string s) {
  129.         List<string> output = new List<string>();
  130.         int j = 0;
  131.  
  132.         while (needRealign(s) || j > 10) {
  133.             j++;
  134.             for (int i = 0; i < s.Length; i+=10) {
  135.                 string preview = s.Substring(0, i);
  136.                
  137.                 if (needRealign(preview)) {
  138.                     for (int c = preview.Length - 1; preview.ToCharArray()[c] != ' '; c--)
  139.                         preview = preview.Remove(c);
  140.                    
  141.                     output.Add (preview);
  142.                     s = s.Remove (0, preview.Length);
  143.                     break;
  144.                 }
  145.             }
  146.         }
  147.        
  148.         output.Add (s);
  149.         return output;
  150.     }
  151.    
  152.     //checa se o texto esta pulando pra fora da text box
  153.     private bool needRealign(string text) {
  154.         GUIContent content = new GUIContent(text);
  155.         int numLines = 3;
  156.        
  157.         Vector2 textDimensions = style.CalcSize(content);
  158.         if (textDimensions.x + 100 > display.rectTransform.rect.width * numLines)
  159.             return true;
  160.        
  161.         return false;
  162.     }
  163.    
  164.     private void checkEndText() {
  165.         if (endText) {
  166.             panel.GetComponent<Animator>().SetBool("endText", true);
  167.             display.enabled = false;
  168.         }
  169.     }
  170.    
  171.     private void initializeStyle() {
  172.         style.fontStyle = display.fontStyle;
  173.         style.font = display.font;
  174.         style.fontSize = display.fontSize;
  175.     }
  176. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement