GuiHelper.cs 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. using System;
  2. using UnityEngine;
  3. public static class GuiHelper
  4. {
  5. public static void DrawLine(Vector2 lineStart, Vector2 lineEnd, Color color)
  6. {
  7. GuiHelper.DrawLine(lineStart, lineEnd, color, 1);
  8. }
  9. public static void DrawLine(Vector2 lineStart, Vector2 lineEnd, Color color, int thickness)
  10. {
  11. if (GuiHelper._coloredLineTexture == null || GuiHelper._coloredLineColor != color)
  12. {
  13. GuiHelper._coloredLineColor = color;
  14. GuiHelper._coloredLineTexture = new Texture2D(1, 1);
  15. GuiHelper._coloredLineTexture.SetPixel(0, 0, GuiHelper._coloredLineColor);
  16. GuiHelper._coloredLineTexture.wrapMode = TextureWrapMode.Repeat;
  17. GuiHelper._coloredLineTexture.Apply();
  18. }
  19. GuiHelper.DrawLineStretched(lineStart, lineEnd, GuiHelper._coloredLineTexture, thickness);
  20. }
  21. public static void DrawLineStretched(Vector2 lineStart, Vector2 lineEnd, Texture2D texture, int thickness)
  22. {
  23. Vector2 vector = lineEnd - lineStart;
  24. float num = 57.29578f * Mathf.Atan(vector.y / vector.x);
  25. if (vector.x < 0f)
  26. {
  27. num += 180f;
  28. }
  29. if (thickness < 1)
  30. {
  31. thickness = 1;
  32. }
  33. int num2 = (int)Mathf.Ceil((float)(thickness / 2));
  34. GUIUtility.RotateAroundPivot(num, lineStart);
  35. GUI.DrawTexture(new Rect(lineStart.x, lineStart.y - (float)num2, vector.magnitude, (float)thickness), texture);
  36. GUIUtility.RotateAroundPivot(-num, lineStart);
  37. }
  38. public static void DrawLine(Vector2 lineStart, Vector2 lineEnd, Texture2D texture)
  39. {
  40. GuiHelper.DrawLine(lineStart, lineEnd, texture, 1);
  41. }
  42. public static void DrawLine(Vector2 lineStart, Vector2 lineEnd, Texture2D texture, int thickness)
  43. {
  44. Vector2 vector = lineEnd - lineStart;
  45. float num = 57.29578f * Mathf.Atan(vector.y / vector.x);
  46. if (vector.x < 0f)
  47. {
  48. num += 180f;
  49. }
  50. if (thickness < 1)
  51. {
  52. thickness = 1;
  53. }
  54. int num2 = (int)Mathf.Ceil((float)(thickness / 2));
  55. Rect position = new Rect(lineStart.x, lineStart.y - (float)num2, Vector2.Distance(lineStart, lineEnd), (float)thickness);
  56. GUIUtility.RotateAroundPivot(num, lineStart);
  57. GUI.BeginGroup(position);
  58. int num3 = Mathf.RoundToInt(position.width);
  59. int num4 = Mathf.RoundToInt(position.height);
  60. for (int i = 0; i < num4; i += texture.height)
  61. {
  62. for (int j = 0; j < num3; j += texture.width)
  63. {
  64. GUI.DrawTexture(new Rect((float)j, (float)i, (float)texture.width, (float)texture.height), texture);
  65. }
  66. }
  67. GUI.EndGroup();
  68. GUIUtility.RotateAroundPivot(-num, lineStart);
  69. }
  70. private static Texture2D _coloredLineTexture;
  71. private static Color _coloredLineColor;
  72. }