CompositeMesh.cs 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. using System.Collections.Generic;
  2. using UnityEngine;
  3. namespace FairyGUI
  4. {
  5. /// <summary>
  6. ///
  7. /// </summary>
  8. public class CompositeMesh : IMeshFactory, IHitTest
  9. {
  10. /// <summary>
  11. ///
  12. /// </summary>
  13. public readonly List<IMeshFactory> elements;
  14. /// <summary>
  15. /// If it is -1, means all elements are active, otherwise, only the specific element is active
  16. /// </summary>
  17. public int activeIndex;
  18. public CompositeMesh()
  19. {
  20. elements = new List<IMeshFactory>();
  21. activeIndex = -1;
  22. }
  23. public void OnPopulateMesh(VertexBuffer vb)
  24. {
  25. int cnt = elements.Count;
  26. if (cnt == 1)
  27. elements[0].OnPopulateMesh(vb);
  28. else
  29. {
  30. VertexBuffer vb2 = VertexBuffer.Begin(vb);
  31. for (int i = 0; i < cnt; i++)
  32. {
  33. if (activeIndex == -1 || i == activeIndex)
  34. {
  35. vb2.Clear();
  36. elements[i].OnPopulateMesh(vb2);
  37. vb.Append(vb2);
  38. }
  39. }
  40. vb2.End();
  41. }
  42. }
  43. public bool HitTest(Rect contentRect, Vector2 point)
  44. {
  45. if (!contentRect.Contains(point))
  46. return false;
  47. bool flag = false;
  48. int cnt = elements.Count;
  49. for (int i = 0; i < cnt; i++)
  50. {
  51. if (activeIndex == -1 || i == activeIndex)
  52. {
  53. IHitTest ht = elements[i] as IHitTest;
  54. if (ht != null)
  55. {
  56. if (ht.HitTest(contentRect, point))
  57. return true;
  58. }
  59. else
  60. flag = true;
  61. }
  62. }
  63. return flag;
  64. }
  65. }
  66. }