testGuied.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. using UnityEngine;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. namespace UnityEngine.UI
  5. {
  6. public class testGuied : MaskableGraphic, UnityEngine.ICanvasRaycastFilter
  7. {
  8. public RectTransform arrow;
  9. public Vector2 center = Vector2.zero;
  10. public Vector2 size = new Vector2(100, 100);
  11. public void DoUpdate()
  12. {
  13. // 当引导箭头位置或者大小改变后更新,注意:未处理拉伸模式
  14. if (arrow && center != arrow.anchoredPosition || size != arrow.sizeDelta)
  15. {
  16. this.center = arrow.anchoredPosition;
  17. this.size = arrow.sizeDelta;
  18. SetAllDirty();
  19. }
  20. }
  21. public bool IsRaycastLocationValid(Vector2 sp, Camera eventCamera)
  22. {
  23. // 点击在箭头框内部则无效,否则生效
  24. return !RectTransformUtility.RectangleContainsScreenPoint(arrow, sp, eventCamera);
  25. }
  26. protected override void OnFillVBO(List<UIVertex> vbo)
  27. {
  28. Vector4 outer = new Vector4(-rectTransform.pivot.x * rectTransform.rect.width,
  29. -rectTransform.pivot.y * rectTransform.rect.height,
  30. (1 - rectTransform.pivot.x) * rectTransform.rect.width,
  31. (1 - rectTransform.pivot.y) * rectTransform.rect.height);
  32. Vector4 inner = new Vector4(center.x - size.x / 2,
  33. center.y - size.y / 2,
  34. center.x + size.x * 0.5f,
  35. center.y + size.y * 0.5f);
  36. vbo.Clear();
  37. var vert = UIVertex.simpleVert;
  38. // left
  39. vert.position = new Vector2(outer.x, outer.y);
  40. vert.color = color;
  41. vbo.Add(vert);
  42. vert.position = new Vector2(outer.x, outer.w);
  43. vert.color = color;
  44. vbo.Add(vert);
  45. vert.position = new Vector2(inner.x, outer.w);
  46. vert.color = color;
  47. vbo.Add(vert);
  48. vert.position = new Vector2(inner.x, outer.y);
  49. vert.color = color;
  50. vbo.Add(vert);
  51. // top
  52. vert.position = new Vector2(inner.x, inner.w);
  53. vert.color = color;
  54. vbo.Add(vert);
  55. vert.position = new Vector2(inner.x, outer.w);
  56. vert.color = color;
  57. vbo.Add(vert);
  58. vert.position = new Vector2(inner.z, outer.w);
  59. vert.color = color;
  60. vbo.Add(vert);
  61. vert.position = new Vector2(inner.z, inner.w);
  62. vert.color = color;
  63. vbo.Add(vert);
  64. // right
  65. vert.position = new Vector2(inner.z, outer.y);
  66. vert.color = color;
  67. vbo.Add(vert);
  68. vert.position = new Vector2(inner.z, outer.w);
  69. vert.color = color;
  70. vbo.Add(vert);
  71. vert.position = new Vector2(outer.z, outer.w);
  72. vert.color = color;
  73. vbo.Add(vert);
  74. vert.position = new Vector2(outer.z, outer.y);
  75. vert.color = color;
  76. vbo.Add(vert);
  77. // bottom
  78. vert.position = new Vector2(inner.x, outer.y);
  79. vert.color = color;
  80. vbo.Add(vert);
  81. vert.position = new Vector2(inner.x, inner.y);
  82. vert.color = color;
  83. vbo.Add(vert);
  84. vert.position = new Vector2(inner.z, inner.y);
  85. vert.color = color;
  86. vbo.Add(vert);
  87. vert.position = new Vector2(inner.z, outer.y);
  88. vert.color = color;
  89. vbo.Add(vert);
  90. }
  91. private void Update()
  92. {
  93. DoUpdate();
  94. }
  95. }
  96. }