CMDebug.cs 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. /*
  2. ------------------- Code Monkey -------------------
  3. Thank you for downloading the Code Monkey Utilities
  4. I hope you find them useful in your projects
  5. If you have any questions use the contact form
  6. Cheers!
  7. unitycodemonkey.com
  8. --------------------------------------------------
  9. */
  10. using System;
  11. using UnityEngine;
  12. using CodeMonkey.Utils;
  13. namespace CodeMonkey {
  14. /*
  15. * Debug Class with various helper functions to quickly create buttons, text, etc
  16. * */
  17. public static class CMDebug {
  18. // Creates a Button in the World
  19. public static World_Sprite Button(Transform parent, Vector3 localPosition, string text, System.Action ClickFunc, int fontSize = 30, float paddingX = 5, float paddingY = 5) {
  20. return World_Sprite.CreateDebugButton(parent, localPosition, text, ClickFunc, fontSize, paddingX, paddingY);
  21. }
  22. // Creates a Button in the UI
  23. public static UI_Sprite ButtonUI(Vector2 anchoredPosition, string text, Action ClickFunc) {
  24. return UI_Sprite.CreateDebugButton(anchoredPosition, text, ClickFunc);
  25. }
  26. public static UI_Sprite ButtonUI(Transform parent, Vector2 anchoredPosition, string text, Action ClickFunc) {
  27. return UI_Sprite.CreateDebugButton(parent, anchoredPosition, text, ClickFunc);
  28. }
  29. // Creates a World Text object at the world position
  30. public static void Text(string text, Vector3 localPosition = default(Vector3), Transform parent = null, int fontSize = 40, Color? color = null, TextAnchor textAnchor = TextAnchor.UpperLeft, TextAlignment textAlignment = TextAlignment.Left, int sortingOrder = UtilsClass.sortingOrderDefault) {
  31. UtilsClass.CreateWorldText(text, parent, localPosition, fontSize, color, textAnchor, textAlignment, sortingOrder);
  32. }
  33. // World text pop up at mouse position
  34. public static void TextPopupMouse(string text, Vector3? offset = null) {
  35. if (offset == null) {
  36. offset = Vector3.one;
  37. }
  38. UtilsClass.CreateWorldTextPopup(text, UtilsClass.GetMouseWorldPosition() + (Vector3)offset);
  39. }
  40. // World text pop up at mouse position
  41. public static void TextPopupMouse(object obj, Vector3? offset = null) {
  42. TextPopupMouse(obj.ToString(), offset);
  43. }
  44. // Creates a Text pop up at the world position
  45. public static void TextPopup(string text, Vector3 position, float popupTime = 1f) {
  46. UtilsClass.CreateWorldTextPopup(text, position, popupTime);
  47. }
  48. // Text Updater in World, (parent == null) = world position
  49. public static FunctionUpdater TextUpdater(Func<string> GetTextFunc, Vector3 localPosition, Transform parent = null, int fontSize = 40, Color? color = null, TextAnchor textAnchor = TextAnchor.UpperLeft, TextAlignment textAlignment = TextAlignment.Left, int sortingOrder = UtilsClass.sortingOrderDefault) {
  50. return UtilsClass.CreateWorldTextUpdater(GetTextFunc, localPosition, parent, fontSize, color, textAnchor, textAlignment, sortingOrder);
  51. }
  52. // Text Updater in UI
  53. public static FunctionUpdater TextUpdaterUI(Func<string> GetTextFunc, Vector2 anchoredPosition) {
  54. return UtilsClass.CreateUITextUpdater(GetTextFunc, anchoredPosition);
  55. }
  56. // Text Updater always following mouse
  57. public static void MouseTextUpdater(Func<string> GetTextFunc, Vector3 positionOffset = default(Vector3)) {
  58. GameObject gameObject = new GameObject();
  59. FunctionUpdater.Create(() => {
  60. gameObject.transform.position = UtilsClass.GetMouseWorldPosition() + positionOffset;
  61. return false;
  62. });
  63. TextUpdater(GetTextFunc, Vector3.zero, gameObject.transform);
  64. }
  65. // Trigger Action on Key
  66. public static FunctionUpdater KeyCodeAction(KeyCode keyCode, Action onKeyDown) {
  67. return UtilsClass.CreateKeyCodeAction(keyCode, onKeyDown);
  68. }
  69. // Debug DrawLine to draw a projectile, turn Gizmos On
  70. public static void DebugProjectile(Vector3 from, Vector3 to, float speed, float projectileSize) {
  71. Vector3 dir = (to - from).normalized;
  72. Vector3 pos = from;
  73. FunctionUpdater.Create(() => {
  74. Debug.DrawLine(pos, pos + dir * projectileSize);
  75. float distanceBefore = Vector3.Distance(pos, to);
  76. pos += dir * speed * Time.deltaTime;
  77. float distanceAfter = Vector3.Distance(pos, to);
  78. if (distanceBefore < distanceAfter) {
  79. return true;
  80. }
  81. return false;
  82. });
  83. }
  84. public static void SpritePopupMouse(Sprite sprite, float scale = 1f) {
  85. SpritePopup(UtilsClass.GetMouseWorldPosition(), sprite, scale);
  86. }
  87. public static void SpritePopup(Vector3 position, Sprite sprite, float scale) {
  88. float popupTime = 1f;
  89. GameObject gameObject = DrawSpriteTimedAlpha(position, sprite, scale, popupTime);
  90. Vector3 finalPopupPosition = position + new Vector3(0, 1, 0) * 20f;
  91. Transform transform = gameObject.transform;
  92. Vector3 moveAmount = (finalPopupPosition - position) / popupTime;
  93. FunctionUpdater.Create(delegate () {
  94. if (gameObject == null) {
  95. return true;
  96. }
  97. transform.position += moveAmount * Time.unscaledDeltaTime;
  98. return false;
  99. }, "SpritePopup");
  100. }
  101. public static GameObject DrawSpriteTimed(Sprite sprite, float scale, float timer) {
  102. return DrawSpriteTimed(UtilsClass.GetMouseWorldPosition(), sprite, scale, timer);
  103. }
  104. public static GameObject DrawSpriteTimed(Vector3 position, Sprite sprite, float scale, float timer) {
  105. GameObject gameObject = new GameObject("SpriteTimed", typeof(SpriteRenderer));
  106. gameObject.transform.position = position;
  107. gameObject.transform.localScale = Vector3.one * scale;
  108. gameObject.GetComponent<SpriteRenderer>().sprite = sprite;
  109. GameObject.Destroy(gameObject, timer);
  110. return gameObject;
  111. }
  112. public static GameObject DrawSpriteTimedAlpha(Sprite sprite, float scale, float timer, float startDecayTimeNormalized = .8f) {
  113. return DrawSpriteTimedAlpha(UtilsClass.GetMouseWorldPosition(), sprite, scale, timer, startDecayTimeNormalized);
  114. }
  115. public static GameObject DrawSpriteTimedAlpha(Vector3 position, Sprite sprite, float scale, float timer, float startDecayTimeNormalized = .8f) {
  116. GameObject gameObject = DrawSpriteTimed(position, sprite, scale, timer);
  117. SpriteRenderer spriteRenderer = gameObject.GetComponent<SpriteRenderer>();
  118. float startAlphaDecayTime = timer * startDecayTimeNormalized;
  119. float totalAlphaDecayTime = timer - startAlphaDecayTime;
  120. float currentTime = 0f;
  121. FunctionUpdater.Create(() => {
  122. if (gameObject == null) {
  123. return true;
  124. }
  125. currentTime += Time.unscaledDeltaTime;
  126. if (currentTime >= startAlphaDecayTime) {
  127. spriteRenderer.color = new Color(1, 1, 1, Mathf.Lerp(1f, 0f, 1 - ((timer - currentTime) / totalAlphaDecayTime)));
  128. }
  129. return false;
  130. });
  131. return gameObject;
  132. }
  133. }
  134. }