Advertisement
EmmyDev

Text Box

Jul 2nd, 2023
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.29 KB | Source Code | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using TMPro;
  5. using FMODUnity;
  6.  
  7. public class SecretMessage : MonoBehaviour
  8. {
  9.     public enum SpecificMode { Secret, Devmode};
  10.     public SpecificMode mode;
  11.     [SerializeField] private string message;
  12.  
  13.     [SerializeField] private BoxCollider2D collider;
  14.  
  15.     private GameObject player;
  16.  
  17.     [SerializeField] private TextMeshPro displayText;
  18.     [SerializeField] private GameObject displayBackground;
  19.  
  20.     private bool hasStarted;
  21.  
  22.     [Tooltip("Hexadecimal color code after $")]
  23.     [SerializeField] private string default_color_hex;
  24.  
  25.     [Tooltip("Hexadecimal color code after @")]
  26.     [SerializeField] private string highlight_color_hex;
  27.  
  28.     [Tooltip("Hexadecimal color code after %")]
  29.     [SerializeField] private string secondary_highlight_color_hex;
  30.  
  31.     [Tooltip("pause time when using *")]
  32.     [SerializeField] private float extra_delay_seconds;
  33.  
  34.     [Header("Audio Refs")]
  35.     [SerializeField] private EventReference _generatingTextAudio;
  36.  
  37.     [Header("devmode icon")]
  38.     [SerializeField] private Sprite devIcon;
  39.  
  40.     [SerializeField] private SpriteRenderer devIconRenderer;
  41.    
  42.     // Update is called once per frame
  43.     void Start()
  44.     {
  45.  
  46.         player = GameObject.Find("Protagonist");
  47.         //determines which mode is active.
  48.         if(mode == SpecificMode.Secret)
  49.         {
  50.             if(PlayerPrefs.GetInt("secret") == 0)
  51.             {
  52.                 this.gameObject.SetActive(false);
  53.             }
  54.         }
  55.         else if(mode == SpecificMode.Devmode)
  56.         {
  57.             if(devIconRenderer != null && devIcon != null)
  58.             {
  59.                 devIconRenderer.sprite = devIcon;
  60.             }
  61.             if(PlayerPrefs.GetInt("devmode") == 0)
  62.             {
  63.                 this.gameObject.SetActive(false);
  64.             }
  65.         }
  66.     }
  67.    
  68.    
  69.     void Update()
  70.     {
  71.         Rect rect = new Rect(this.transform.position.x + collider.offset.x - (collider.size.x / 2), this.transform.position.y + collider.offset.y - (collider.size.y / 2), collider.size.x, collider.size.y);
  72.         if (rect.Contains(player.transform.position) && !hasStarted)
  73.         {
  74.             displayText.text = "";
  75.             displayText.text += "<color=" + default_color_hex + ">";
  76.             StartCoroutine(PlayText(message));
  77.         }
  78.            
  79.     }
  80.     private void OnDrawGizmos()
  81.     {
  82.         DrawRect(new Rect(this.transform.position.x + collider.offset.x - (collider.size.x / 2), this.transform.position.y + collider.offset.y - (collider.size.y / 2), collider.size.x, collider.size.y));
  83.     }
  84.     void DrawRect(Rect rect)
  85.     {  
  86.         Gizmos.DrawWireCube(new Vector3(rect.center.x, rect.center.y, 0.01f), new Vector3(rect.size.x, rect.size.y, 0.01f));
  87.     }
  88.  
  89.     IEnumerator PlayText(string text)
  90.     {
  91.         hasStarted = true;
  92.         displayBackground.SetActive(true);
  93.  
  94.         foreach (char c in text)
  95.         {
  96.             if(c == 43)
  97.             {
  98.                 //+
  99.                 displayText.text += "<br>";
  100.             }
  101.             else if(c == 64)
  102.             {
  103.                 //@
  104.                 displayText.text += "<color=" + highlight_color_hex + ">";
  105.             }
  106.             else if(c == 37)
  107.             {
  108.                 //%
  109.                 displayText.text += "<color=" + secondary_highlight_color_hex + ">";
  110.             }
  111.             else if(c == 36)
  112.             {
  113.                 //$
  114.                 displayText.text += "<color=" + default_color_hex + ">";
  115.             }
  116.             else if(c == 42)
  117.             {
  118.                 //*
  119.                 yield return new WaitForSeconds (extra_delay_seconds);
  120.             }
  121.             else
  122.             {
  123.                 if(devIconRenderer != null && devIcon != null)
  124.                 {
  125.                     devIconRenderer.gameObject.GetComponent<Animation>().Play();
  126.                 }
  127.                 RuntimeManager.PlayOneShot(_generatingTextAudio);
  128.                 displayText.text += c;
  129.                 yield return new WaitForSeconds (0.06f);
  130.             }
  131.            
  132.         }
  133.         if(mode == SpecificMode.Devmode)
  134.         {
  135.             yield return new WaitForSeconds (8f);
  136.             displayBackground.SetActive(false);
  137.             this.gameObject.SetActive(false);
  138.         }
  139.        
  140.     }
  141.  
  142. }
  143.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement