Advertisement
Guest User

Untitled

a guest
Dec 14th, 2018
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.68 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class textEffect : MonoBehaviour {
  6.  
  7. [Tooltip("Text to show")]
  8. public string text;
  9. [Tooltip("Space between characters. Default: 0.11")]
  10. public float characterSpacing;
  11. [Tooltip("Size of the characters")]
  12. public int fontSize;
  13. [Space]
  14.  
  15. [Tooltip("Distance between the spawn position and the final position")]
  16. public float zOrigin;
  17. [Space]
  18.  
  19. [Tooltip("Time between spawn and the final position")]
  20. public float animationDuration;
  21. [Tooltip("0 means infinite")]
  22. public float despawnTime;
  23. [Tooltip("Duration of the despawn effect")]
  24. public float fadeOutDuration;
  25. [Tooltip("Time between every character spawn")]
  26. public float characterDelay;
  27. [Tooltip("Delay applied to all characters")]
  28. public float characterOffsetDelay = 0;
  29. [Space]
  30.  
  31. [Range(0,1)]
  32. [Tooltip("Random variation applied to every char")]
  33. public float errorRange;
  34.  
  35. private float velocityIncrement;
  36.  
  37. private Vector3[] localInitPosition;
  38. private Vector3[] localFinalPosition;
  39. private GameObject[] charactersGO;
  40. private float[] characterTime;
  41. private TextMesh[] characterTextMesh;
  42. private float time = 0;
  43.  
  44. private float generalScale = 0.15f;
  45.  
  46. private bool showScreens = true;
  47. private ScreenAnchor screenAnchor;
  48. private float videoDisplacement = 10;
  49. private Vector3 anchorFinalPosition;
  50. private Vector3 anchorInitPosition;
  51.  
  52. private void OnEnable() {
  53. Setup();
  54. }
  55.  
  56. void Setup() {
  57. //Moving the screen anchor
  58. showScreens = false;
  59. screenAnchor = transform.parent.GetComponent<Room>().screenAnchor.GetComponent<ScreenAnchor>();
  60.  
  61.  
  62.  
  63. velocityIncrement = 1 / animationDuration;
  64.  
  65. float spacingTotal = 0;
  66.  
  67. localFinalPosition = new Vector3[text.Length];
  68. localInitPosition = new Vector3[text.Length];
  69. charactersGO = new GameObject[text.Length];
  70. characterTime = new float[text.Length];
  71. characterTextMesh = new TextMesh[text.Length];
  72.  
  73. DestroyAll(true);
  74.  
  75. for (int i = 0; i < text.Length; i++) {
  76. GameObject character = new GameObject("Character " + i);
  77. character.transform.parent = transform;
  78.  
  79. TextMesh char3DText = character.AddComponent<TextMesh>();
  80. char3DText.fontSize = fontSize;
  81. char3DText.text = text.Substring(i, 1);
  82.  
  83. character.transform.localScale = new Vector3(generalScale, generalScale, generalScale);
  84.  
  85. CharacterInfo info;
  86. char3DText.font.GetCharacterInfo(char3DText.text[0], out info, char3DText.fontSize, char3DText.fontStyle);
  87.  
  88. localInitPosition[i] = new Vector3(spacingTotal, 0, zOrigin + spacingTotal * Random.Range(-errorRange/2, errorRange/2));
  89. localInitPosition[i] += transform.position;
  90. localFinalPosition[i] = new Vector3(spacingTotal, 0, 0);
  91. localFinalPosition[i] += transform.position;
  92. character.transform.position = localInitPosition[i];
  93.  
  94. charactersGO[i] = character;
  95. characterTime[i] = -characterDelay * i;
  96. characterTime[i] -= characterOffsetDelay;
  97. characterTextMesh[i] = char3DText;
  98. characterTextMesh[i].color = new Color(1, 1, 1, 0);
  99. spacingTotal += info.advance * characterSpacing * generalScale;
  100. }
  101.  
  102. for (int i = 0; i < text.Length; i++) {
  103. localInitPosition[i].x -= spacingTotal / 2;
  104. localFinalPosition[i].x -= spacingTotal / 2;
  105. }
  106. }
  107.  
  108. // Update is called once per frame
  109. void Update() {
  110. bool canDestroy = true;
  111. for (int i = 0; i < text.Length; i++) {
  112. float easeLerp = 0;
  113. if (characterTime[i] < 0) {
  114. easeLerp = 0;
  115. charactersGO[i].transform.position = Vector3.Lerp(localInitPosition[i], localFinalPosition[i], easeLerp);
  116. characterTextMesh[i].color = new Color(1, 1, 1, easeLerp);
  117. }
  118. else if (characterTime[i] < animationDuration) {
  119. easeLerp = Mathf.Sin((characterTime[i] / animationDuration) * Mathf.PI / 2);
  120. charactersGO[i].transform.position = Vector3.Lerp(localInitPosition[i], localFinalPosition[i], easeLerp);
  121. characterTextMesh[i].color = new Color(1, 1, 1, easeLerp);
  122. }
  123. else {
  124. if (characterTime[i] > despawnTime && despawnTime != 0) { //if despawnTime == 0, don't despawn.
  125. characterTextMesh[i].color = new Color(1, 1, 1, 1 - (characterTime[i] - despawnTime) / fadeOutDuration);
  126. showScreens = true;
  127. }
  128. }
  129. characterTime[i] += Time.deltaTime;
  130.  
  131. if (despawnTime == 0 || characterTime[i] <= 0 + (despawnTime + fadeOutDuration)) {
  132. canDestroy = false;
  133. }
  134. }
  135.  
  136. if (canDestroy) { //Destroy all characters
  137. DestroyAll(false);
  138. }
  139.  
  140. if (showScreens) {
  141. screenAnchor.transform.position = Vector3.Lerp(screenAnchor.transform.position, screenAnchor.showingPosition, 0.05f);
  142. }
  143. else {
  144. screenAnchor.transform.position = screenAnchor.hiddenPosition;
  145. }
  146. }
  147.  
  148. void DestroyAll(bool deactivateSpawner) {
  149. int tmp = charactersGO.Length - 1;
  150. while (tmp >= 0) {
  151. Destroy(charactersGO[tmp]);
  152. tmp--;
  153. }
  154. gameObject.SetActive(deactivateSpawner);
  155. }
  156.  
  157. public void OnRoomExit() {
  158. DestroyAll(false);
  159. }
  160. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement