Guest User

Untitled

a guest
Jan 23rd, 2018
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.73 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5.  
  6. /// <summary>DebugTextGameView class
  7. /// AUTHOR: Beard or Die
  8. /// DATE: 2018-01-12
  9. /// REQUIREMENTS: Add this script onto a GameObject that you want some debug text to
  10. /// appear over in Game View. Set up in inspector. You will also need to create
  11. /// an empty GameObject, add a TextMesh component, and drag that GameObject into
  12. /// Project folder to create a prefab from it. Use that prefab in this script's
  13. /// inspector for "Debug Text Prefab". This will allow you to change the style/size
  14. /// of your debug text just once, rather than having to set that for every usage.
  15. /// INSTRUCTIONS: If you want to specify custom text, choose Override style and
  16. /// call the public OverrideText method. Or choose GameObjectToString and override that.
  17. /// Example: public override string ToString(){ return "Some custom string"; }
  18. /// WARNING: This is for debug purposes, not for production. It's too ugly to use
  19. /// for real game usage.
  20. /// LICENSE: CC0; change and use this however you like
  21. /// </summary>
  22. public class DebugTextGameView : MonoBehaviour {
  23.  
  24. private bool isDrawn = false;
  25. private string overrideText = "OverrideText method not yet called. See instructions.";
  26. private Transform txtMeshTransform;
  27. private GameObject txtMeshObject;
  28. private TextMesh txtMesh;
  29. private float destroyTime;
  30.  
  31. // INSPECTOR SETTINGS
  32. [Tooltip("If true, set visibleOnlyWhileKeyHeld to false")] [SerializeField] private bool isVisibleOnAwake = true;
  33. [Tooltip("If true, specify heldKey and set isVisibleOnAwake to false")] [SerializeField] private bool visibleOnlyWhileKeyHeld = false;
  34.  
  35. [SerializeField] private Transform debugTextPrefab; // this prefab requires a TextMesh component attached on a child gameobject
  36. [SerializeField] private Vector3 textOffset = Vector3.up;
  37. [SerializeField] private Color color = Color.green;
  38. [Tooltip("In seconds; If 0, never auto-destroy")] [SerializeField] private float autoDestroyTime = 0f;
  39.  
  40.  
  41. [Tooltip("If visibleOnlyWhileKeyHeld is true, set this key")] [SerializeField] private KeyCode heldKey = KeyCode.LeftShift;
  42.  
  43. [HideInInspector] public enum DebugDataTypes {TransformPosition, GameObjectName, GameObjectToString, Override}
  44. [Tooltip("Choose type of data to show")] [SerializeField] private DebugDataTypes useData = DebugDataTypes.TransformPosition;
  45.  
  46.  
  47. // PRIVATE METHODS
  48. private void Awake()
  49. {
  50. Setup();
  51. }
  52.  
  53.  
  54. private void Setup()
  55. {
  56. if(txtMeshObject==null)
  57. {
  58. isDrawn=false;
  59. }
  60.  
  61. if(!isDrawn && txtMeshTransform==null)
  62. {
  63. isDrawn=true;
  64. txtMeshTransform = (Transform)Instantiate(debugTextPrefab);
  65. txtMeshTransform.position = Vector3.zero; // reset the prefab's position
  66. txtMeshObject = txtMeshTransform.gameObject;
  67. if(!isVisibleOnAwake) txtMeshObject.SetActive(false);
  68. txtMesh = txtMeshTransform.GetComponentInChildren<TextMesh>();
  69.  
  70. txtMesh.color = color;
  71.  
  72. if(autoDestroyTime>0)
  73. {
  74. destroyTime = Time.time + autoDestroyTime;
  75. StartCoroutine(AutoDestroy());
  76. }
  77.  
  78.  
  79. }
  80.  
  81. if(isDrawn)
  82. {
  83. SetPosition();
  84. UpdateDebugText();
  85. }
  86. }
  87.  
  88. private void SetPosition()
  89. {
  90. txtMeshTransform.position = gameObject.transform.position + textOffset;
  91. }
  92.  
  93. private void UpdateDebugText()
  94. {
  95.  
  96. if(txtMesh==null || txtMeshObject==null)
  97. {
  98. return; // prevent null reference exceptions
  99. }
  100.  
  101. SetPosition();
  102.  
  103. // Set text for debugging
  104. switch (useData){
  105. case DebugDataTypes.GameObjectName:
  106. txtMesh.text = gameObject.name;
  107. break;
  108. case DebugDataTypes.GameObjectToString:
  109. txtMesh.text = gameObject.ToString();
  110. break;
  111. case DebugDataTypes.TransformPosition:
  112. txtMesh.text = gameObject.transform.position.ToString();
  113. break;
  114. case DebugDataTypes.Override:
  115. if(overrideText!=null)
  116. {
  117. txtMesh.text = overrideText;
  118. } else
  119. {
  120. txtMesh.text = "OverrideText not yet called. See instructions."; // to prevent this, see instructions
  121. }
  122. break;
  123. default:
  124. txtMesh.text = gameObject.ToString();
  125. break;
  126. }
  127.  
  128. if(visibleOnlyWhileKeyHeld)
  129. {
  130. if(Input.GetKey(heldKey))
  131. {
  132. SetPosition();
  133. txtMeshObject.SetActive(true);
  134. } else {
  135. txtMeshObject.SetActive(false);
  136. }
  137. }
  138.  
  139.  
  140. if(txtMeshObject==null)
  141. {
  142. isDrawn=false;
  143. }
  144. }
  145.  
  146. private void Update()
  147. {
  148. UpdateDebugText();
  149. }
  150.  
  151. private IEnumerator AutoDestroy()
  152. {
  153. if(Time.time >= destroyTime)
  154. {
  155. Hide();
  156. Destroy(txtMeshObject,5f);
  157. yield break;
  158.  
  159. } else {
  160. yield return new WaitForSeconds(0.1f);
  161. StartCoroutine(AutoDestroy()); // recursion
  162. }
  163. }
  164.  
  165.  
  166. // PUBLIC METHODS
  167. public void OverrideText(string newText)
  168. {
  169. overrideText = newText;
  170. }
  171.  
  172. public void Show()
  173. {
  174. if(txtMeshObject!=null) txtMeshObject.SetActive(true);
  175. }
  176.  
  177. public void Hide()
  178. {
  179. if(txtMeshObject!=null) txtMeshObject.SetActive(false);
  180. }
  181.  
  182. }
Add Comment
Please, Sign In to add comment