HollowOutMask.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. using UnityEngine;
  2. using UnityEngine.UI;
  3. /// <summary>
  4. /// 实现镂空效果的Mask组件
  5. /// </summary>
  6. public class HollowOutMask : MaskableGraphic, ICanvasRaycastFilter
  7. {
  8. [SerializeField]
  9. private RectTransform _target;
  10. private Vector3 _targetMin = Vector3.zero;
  11. private Vector3 _targetMax = Vector3.zero;
  12. private bool _canRefresh = true;
  13. private Transform _cacheTrans = null;
  14. [SerializeField]
  15. public bool isTargetRectCanThrough = true;
  16. /// <summary>
  17. /// 设置镂空的目标
  18. /// </summary>
  19. public void SetTarget(RectTransform target)
  20. {
  21. _canRefresh = true;
  22. _target = target;
  23. _RefreshView();
  24. }
  25. private void _SetTarget(Vector3 tarMin, Vector3 tarMax)
  26. {
  27. if (tarMin == _targetMin && tarMax == _targetMax) return;
  28. _targetMin = tarMin;
  29. _targetMax = tarMax;
  30. SetAllDirty();
  31. }
  32. private void _RefreshView()
  33. {
  34. if (!_canRefresh) return;
  35. _canRefresh = false;
  36. if (null == _target)
  37. {
  38. _SetTarget(Vector3.zero, Vector3.zero);
  39. SetAllDirty();
  40. }
  41. else
  42. {
  43. Bounds bounds = RectTransformUtility.CalculateRelativeRectTransformBounds(_cacheTrans, _target);
  44. _SetTarget(bounds.min, bounds.max);
  45. }
  46. }
  47. protected override void OnPopulateMesh(VertexHelper vh)
  48. {
  49. if (_targetMin == Vector3.zero && _targetMax == Vector3.zero)
  50. {
  51. base.OnPopulateMesh(vh);
  52. return;
  53. }
  54. vh.Clear();
  55. // 填充顶点
  56. UIVertex vert = UIVertex.simpleVert;
  57. vert.color = color;
  58. Vector2 selfPiovt = rectTransform.pivot;
  59. Rect selfRect = rectTransform.rect;
  60. float outerLx = -selfPiovt.x * selfRect.width;
  61. float outerBy = -selfPiovt.y * selfRect.height;
  62. float outerRx = (1 - selfPiovt.x) * selfRect.width;
  63. float outerTy = (1 - selfPiovt.y) * selfRect.height;
  64. // 0 - Outer:LT
  65. vert.position = new Vector3(outerLx, outerTy);
  66. vh.AddVert(vert);
  67. // 1 - Outer:RT
  68. vert.position = new Vector3(outerRx, outerTy);
  69. vh.AddVert(vert);
  70. // 2 - Outer:RB
  71. vert.position = new Vector3(outerRx, outerBy);
  72. vh.AddVert(vert);
  73. // 3 - Outer:LB
  74. vert.position = new Vector3(outerLx, outerBy);
  75. vh.AddVert(vert);
  76. // 4 - Inner:LT
  77. vert.position = new Vector3(_targetMin.x, _targetMax.y);
  78. vh.AddVert(vert);
  79. // 5 - Inner:RT
  80. vert.position = new Vector3(_targetMax.x, _targetMax.y);
  81. vh.AddVert(vert);
  82. // 6 - Inner:RB
  83. vert.position = new Vector3(_targetMax.x, _targetMin.y);
  84. vh.AddVert(vert);
  85. // 7 - Inner:LB
  86. vert.position = new Vector3(_targetMin.x, _targetMin.y);
  87. vh.AddVert(vert);
  88. // 设定三角形
  89. vh.AddTriangle(4, 0, 1);
  90. vh.AddTriangle(4, 1, 5);
  91. vh.AddTriangle(5, 1, 2);
  92. vh.AddTriangle(5, 2, 6);
  93. vh.AddTriangle(6, 2, 3);
  94. vh.AddTriangle(6, 3, 7);
  95. vh.AddTriangle(7, 3, 0);
  96. vh.AddTriangle(7, 0, 4);
  97. }
  98. bool ICanvasRaycastFilter.IsRaycastLocationValid(Vector2 screenPos, Camera eventCamera)
  99. {
  100. if (!isTargetRectCanThrough) return true;
  101. if (null == _target) return true;
  102. // 将目标对象范围内的事件镂空(使其穿过)
  103. return !RectTransformUtility.RectangleContainsScreenPoint(_target, screenPos, eventCamera);
  104. }
  105. protected override void Awake()
  106. {
  107. base.Awake();
  108. _cacheTrans = GetComponent<RectTransform>();
  109. }
  110. // #if UNITY_EDITOR
  111. // void Update()
  112. // {
  113. // _canRefresh = true;
  114. // _RefreshView();
  115. // }
  116. // #endif
  117. }