RectMesh.cs 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. using UnityEngine;
  2. namespace FairyGUI
  3. {
  4. /// <summary>
  5. ///
  6. /// </summary>
  7. public class RectMesh : IMeshFactory, IHitTest
  8. {
  9. /// <summary>
  10. ///
  11. /// </summary>
  12. public Rect? drawRect;
  13. /// <summary>
  14. ///
  15. /// </summary>
  16. public float lineWidth;
  17. /// <summary>
  18. ///
  19. /// </summary>
  20. public Color32 lineColor;
  21. /// <summary>
  22. ///
  23. /// </summary>
  24. public Color32? fillColor;
  25. /// <summary>
  26. ///
  27. /// </summary>
  28. public Color32[] colors;
  29. public RectMesh()
  30. {
  31. lineColor = Color.black;
  32. }
  33. public void OnPopulateMesh(VertexBuffer vb)
  34. {
  35. Rect rect = drawRect != null ? (Rect)drawRect : vb.contentRect;
  36. Color32 color = fillColor != null ? (Color32)fillColor : vb.vertexColor;
  37. if (lineWidth == 0)
  38. {
  39. if (color.a != 0)//optimized
  40. vb.AddQuad(rect, color);
  41. }
  42. else
  43. {
  44. Rect part;
  45. //left,right
  46. part = new Rect(rect.x, rect.y, lineWidth, rect.height);
  47. vb.AddQuad(part, lineColor);
  48. part = new Rect(rect.xMax - lineWidth, rect.y, lineWidth, rect.height);
  49. vb.AddQuad(part, lineColor);
  50. //top, bottom
  51. part = new Rect(rect.x + lineWidth, rect.y, rect.width - lineWidth * 2, lineWidth);
  52. vb.AddQuad(part, lineColor);
  53. part = new Rect(rect.x + lineWidth, rect.yMax - lineWidth, rect.width - lineWidth * 2, lineWidth);
  54. vb.AddQuad(part, lineColor);
  55. //middle
  56. if (color.a != 0)//optimized
  57. {
  58. part = Rect.MinMaxRect(rect.x + lineWidth, rect.y + lineWidth, rect.xMax - lineWidth, rect.yMax - lineWidth);
  59. if (part.width > 0 && part.height > 0)
  60. vb.AddQuad(part, color);
  61. }
  62. }
  63. if (colors != null)
  64. vb.RepeatColors(colors, 0, vb.currentVertCount);
  65. vb.AddTriangles();
  66. }
  67. public bool HitTest(Rect contentRect, Vector2 point)
  68. {
  69. return contentRect.Contains(point);
  70. }
  71. }
  72. }