Advertisement
Guest User

Untitled

a guest
Jul 21st, 2018
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.78 KB | None | 0 0
  1. using System.Collections;
  2. using UnityEngine;
  3. using UnityEngine.UI;
  4. using UnityEngine.SceneManagement;
  5.  
  6. public class Typerwriter : MonoBehaviour
  7. {
  8. public Text textBox;
  9. //Store all your text in this string array
  10. string[] sceneCallText = new string[] {""};
  11. string[] commandTextlvl1 = new string[] { "Welcome to the Academy Cadet", "Lets begin by going over the controls", "On your left is the Verticle Thrust Control", "On your Right is the Forward Thrust Control", "Try and avoid the Obsticles and make it to the finish platform!", "" };
  12. string[] commandTextlvl2 = new string[] { "Congratulations on making it through the first level!", "Its time for your second test!", "Good Luck Cadet!", "" };
  13. string[] commandTextlvl4 = new string[] { "Level 4", "" };
  14. string[] commandTextlvl7 = new string[] { "Level 7", "" };
  15. string[] commandTextlvl10 = new string[] { "Level 10", "" };
  16. int currentlyDisplayingText = 0;
  17. [SerializeField] float typeSpeed = .1f;
  18. public Animator commandData;
  19. public GameObject typerwriterText;
  20. public bool hasBeenPlayed;
  21. string currentSceneCall;
  22.  
  23. void Awake()
  24. {
  25. hasBeenPlayed = false;
  26. int currentScene = SceneManager.GetActiveScene().buildIndex;
  27.  
  28. if (!hasBeenPlayed)
  29. {
  30. commandData.enabled = false;
  31. commandData = typerwriterText.GetComponent<Animator>();
  32. StartCoroutine("AnimateText");
  33. if (currentScene == 2)
  34. {
  35. commandData.enabled = true;
  36. sceneCallText = commandTextlvl1;
  37. commandData.Play("CommandData");
  38. }
  39. if (currentScene == 3)
  40. {
  41. commandData.enabled = true;
  42. sceneCallText = commandTextlvl2;
  43. commandData.Play("CommandData");
  44. }
  45. if (currentScene == 5)
  46. {
  47. commandData.enabled = true;
  48. sceneCallText = commandTextlvl4;
  49. commandData.Play("CommandData");
  50. }
  51. if (currentScene == 8)
  52. {
  53. commandData.enabled = true;
  54. sceneCallText = commandTextlvl7;
  55. commandData.Play("CommandData");
  56. }
  57. if (currentScene == 11)
  58. {
  59. commandData.enabled = true;
  60. sceneCallText = commandTextlvl10;
  61. commandData.Play("CommandData");
  62. }
  63. }
  64. else return;
  65. }
  66.  
  67. //This is a function for a button you press to skip to the next text
  68. public void SkipToNextText()
  69. {
  70. StopAllCoroutines();
  71. currentlyDisplayingText++;
  72. //If we've reached the end of the array, do anything you want.
  73. if (currentlyDisplayingText >= sceneCallText.Length)
  74. {
  75. StopAllCoroutines();
  76. commandData.Play("CommandDataOut");
  77. //TODO put in check to see if all levels and coroutines have been played
  78. hasBeenPlayed = true;
  79. return;
  80. }
  81. StartCoroutine("AnimateText");
  82. }
  83.  
  84. /*Note that the speed you want the typewriter effect to be going at is the yield waitforseconds (in my case it's 1 letter for
  85. every 0.03 seconds, replace this with a public float if you want to experiment with speed in from the editor)*/
  86. IEnumerator AnimateText()
  87. {
  88.  
  89. yield return new WaitForSeconds(1.5f);
  90.  
  91. for (int i = 0; i < (sceneCallText[currentlyDisplayingText].Length + 1); i++)
  92. {
  93. textBox.text = sceneCallText[currentlyDisplayingText].Substring(0, i);
  94. yield return new WaitForSeconds(typeSpeed);
  95. }
  96. yield return new WaitForSeconds(1.5f);
  97. SkipToNextText();
  98. }
  99. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement