PlaneMesh.cs 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. using UnityEngine;
  2. namespace FairyGUI
  3. {
  4. /// <summary>
  5. ///
  6. /// </summary>
  7. public class PlaneMesh : IMeshFactory
  8. {
  9. public int gridSize = 30;
  10. public void OnPopulateMesh(VertexBuffer vb)
  11. {
  12. float w = vb.contentRect.width;
  13. float h = vb.contentRect.height;
  14. float xMax = vb.contentRect.xMax;
  15. float yMax = vb.contentRect.yMax;
  16. int hc = (int)Mathf.Min(Mathf.CeilToInt(w / gridSize), 9);
  17. int vc = (int)Mathf.Min(Mathf.CeilToInt(h / gridSize), 9);
  18. int eachPartX = Mathf.FloorToInt(w / hc);
  19. int eachPartY = Mathf.FloorToInt(h / vc);
  20. float x, y;
  21. for (int i = 0; i <= vc; i++)
  22. {
  23. if (i == vc)
  24. y = yMax;
  25. else
  26. y = vb.contentRect.y + i * eachPartY;
  27. for (int j = 0; j <= hc; j++)
  28. {
  29. if (j == hc)
  30. x = xMax;
  31. else
  32. x = vb.contentRect.x + j * eachPartX;
  33. vb.AddVert(new Vector3(x, y, 0));
  34. }
  35. }
  36. for (int i = 0; i < vc; i++)
  37. {
  38. int k = i * (hc + 1);
  39. for (int j = 1; j <= hc; j++)
  40. {
  41. int m = k + j;
  42. vb.AddTriangle(m - 1, m, m + hc);
  43. vb.AddTriangle(m, m + hc + 1, m + hc);
  44. }
  45. }
  46. }
  47. }
  48. }