Advertisement
Guest User

GuiHelper

a guest
Feb 20th, 2020
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.47 KB | None | 0 0
  1. using UnityEngine;
  2.  
  3. namespace lolxD1
  4. {
  5. public static class Utility
  6. {
  7. private static Texture2D _coloredLineTexture;
  8. private static Color _coloredLineColor;
  9.  
  10. public static void DrawLine(Vector2 lineStart, Vector2 lineEnd, Color color)
  11. {
  12. DrawLine(lineStart, lineEnd, color, 1);
  13. }
  14.  
  15. public static void DrawBox(float x, float y, float w, float h, Color color)
  16. {
  17. DrawLine(new Vector2(x, y), new Vector2(x + w, y), color);
  18. DrawLine(new Vector2(x, y), new Vector2(x, y + h), color);
  19. DrawLine(new Vector2(x + w, y), new Vector2(x + w, y + h), color);
  20. DrawLine(new Vector2(x, y + h), new Vector2(x + w, y + h), color);
  21. }
  22.  
  23. public static void DrawLine(Vector2 lineStart, Vector2 lineEnd, Color color, int thickness)
  24. {
  25. if (_coloredLineTexture == null || _coloredLineColor != color)
  26. {
  27. _coloredLineColor = color;
  28. _coloredLineTexture = new Texture2D(1, 1);
  29. _coloredLineTexture.SetPixel(0, 0, _coloredLineColor);
  30. _coloredLineTexture.wrapMode = 0;
  31. _coloredLineTexture.Apply();
  32. }
  33.  
  34. DrawLineStretched(lineStart, lineEnd, _coloredLineTexture, thickness);
  35. }
  36.  
  37. public static void DrawLineStretched(Vector2 lineStart, Vector2 lineEnd, Texture2D texture, int thickness)
  38. {
  39. var vector = lineEnd - lineStart;
  40. float pivot = 57.29578f * Mathf.Atan(vector.y / vector.x);
  41. if (vector.x < 0f)
  42. {
  43. pivot += 180f;
  44. }
  45.  
  46. if (thickness < 1)
  47. {
  48. thickness = 1;
  49. }
  50.  
  51. int yOffset = (int)Mathf.Ceil((float)(thickness / 2));
  52.  
  53. GUIUtility.RotateAroundPivot(pivot, lineStart);
  54. GUI.DrawTexture(new Rect(lineStart.x, lineStart.y - (float)yOffset, vector.magnitude, (float)thickness), texture);
  55. GUIUtility.RotateAroundPivot(-pivot, lineStart);
  56. }
  57.  
  58. public static void DrawLine(Vector2 lineStart, Vector2 lineEnd, Texture2D texture)
  59. {
  60. DrawLine(lineStart, lineEnd, texture, 1);
  61. }
  62.  
  63. public static void DrawLine(Vector2 lineStart, Vector2 lineEnd, Texture2D texture, int thickness)
  64. {
  65. var vector = lineEnd - lineStart;
  66. float pivot = 57.29578f * Mathf.Atan(vector.y / vector.x);
  67.  
  68. if (vector.x < 0f)
  69. {
  70. pivot += 180f;
  71. }
  72.  
  73. if (thickness < 1)
  74. {
  75. thickness = 1;
  76. }
  77.  
  78. int num2 = (int)Mathf.Ceil((float)(thickness / 2));
  79. var rect = new Rect(lineStart.x, lineStart.y - (float)num2, Vector2.Distance(lineStart, lineEnd), (float)thickness);
  80. GUIUtility.RotateAroundPivot(pivot, lineStart);
  81. GUI.BeginGroup(rect);
  82. int num3 = Mathf.RoundToInt(rect.width);
  83. int num4 = Mathf.RoundToInt(rect.height);
  84.  
  85. for (int i = 0; i < num4; i += texture.height)
  86. {
  87. for (int j = 0; j < num3; j += texture.width)
  88. {
  89. GUI.DrawTexture(new Rect((float)j, (float)i, (float)texture.width, (float)texture.height), texture);
  90. }
  91. }
  92.  
  93. GUI.EndGroup();
  94. GUIUtility.RotateAroundPivot(-pivot, lineStart);
  95. }
  96. }
  97. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement