Advertisement
uglenXD

InputScreen.cs

Apr 22nd, 2019
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.99 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.UI;
  5. using TMPro;
  6.  
  7. public class InputScreen : MonoBehaviour
  8. {
  9.  
  10.     public static InputScreen instance;
  11.  
  12.     public TMP_InputField inputField;
  13.     /// <summary>
  14.     /// The current input delared by the player on the input screen.
  15.     /// </summary>
  16.     /// <value>The current input.</value>
  17.     public static string currentInput { get { return instance.inputField.text; } }
  18.  
  19.     public TitleHeader header;
  20.  
  21.     public GameObject root;
  22.  
  23.     void Awake()
  24.     {
  25.         instance = this;
  26.         Hide();
  27.     }
  28.  
  29.     public static void Show(string title, bool clearCurrentInput = true)
  30.     {
  31.         instance.root.SetActive(true);
  32.  
  33.         if (clearCurrentInput)
  34.             instance.inputField.text = "";
  35.  
  36.         //only show a title if one is given.
  37.         if (title != "")
  38.             instance.header.Show(title);
  39.         else
  40.             instance.header.Hide();
  41.  
  42.         if (isRevealing)
  43.             instance.StopCoroutine(revealing);
  44.  
  45.         revealing = instance.StartCoroutine(Revealing());
  46.     }
  47.  
  48.     public static void Hide()
  49.     {
  50.         instance.root.SetActive(false);
  51.         instance.header.Hide();
  52.     }
  53.  
  54.     public static bool isShowingInputField { get { return instance.inputField.gameObject.activeInHierarchy; } }
  55.     public static bool isWaitingForUserInput { get { return instance.root.activeInHierarchy; } }
  56.     public static bool isRevealing { get { return revealing != null; } }
  57.     static Coroutine revealing = null;
  58.     static IEnumerator Revealing()
  59.     {
  60.         instance.inputField.gameObject.SetActive(false);
  61.  
  62.         while (instance.header.isRevealing)
  63.             yield return new WaitForEndOfFrame();
  64.  
  65.         instance.inputField.gameObject.SetActive(true);
  66.  
  67.         revealing = null;
  68.     }
  69.  
  70.     /// <summary>
  71.     /// Accept the current input and close the screen.
  72.     /// </summary>
  73.     public void Accept()
  74.     {
  75.         Hide();
  76.     }
  77. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement