1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265
| using System; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI;
public class HighlightModule : Graphic, ICanvasRaycastFilter { public bool CanClick { get; set; } private readonly Vector2[] _outer = new Vector2[4]; private readonly Vector2[] _inner = new Vector2[4]; public RectTransform[] TargetUI { get; private set; } public Camera CurrentCamera { get; private set; }
public bool IsValid { get; private set; } public bool IsInValidRange { get; private set; }
private Vector2 _targetLocalPos; private Vector3 _lastTargetPos; private Vector2 _lastTargetSize; private Vector3 _curTargetPos; private Vector2 _curTargetSize; float _leftX, _rightX, _bottomY, _topY;
private Button _selfButton; public Button SelfButton { get { if (_selfButton == null) { if (!TryGetComponent(out _selfButton)) { _selfButton = gameObject.AddComponent<Button>(); } } return _selfButton; } } public void Init(RectTransform targetUI, Camera currentCamera, System.Action clickAction) { Init(new RectTransform[] {targetUI},currentCamera, clickAction); } public void Init(RectTransform[] targetUIs, Camera currentCamera, System.Action clickAction) { TargetUI = targetUIs; CurrentCamera = currentCamera; color = new Color32(0, 0, 0, 190); SelfButton.onClick.RemoveAllListeners(); AddButtonListener(clickAction); } public void AddButtonListener(System.Action onClick) { SelfButton.onClick.AddListener(() => onClick.Invoke()); }
private void Update() { if (CurrentCamera == null || !JudgeTargetValid()) { if (IsValid) { SetAllDirty(); } IsValid = false; return; }
if (!IsValid) { IsValid = true; SetAllDirty(); } GetValidRectData(); JudgeChange(); }
private bool JudgeTargetValid() { if (TargetUI == null || TargetUI.Length == 0) return false;
bool isValid = false; foreach (Transform t in TargetUI) { if (t.gameObject.activeSelf) { isValid = true; break; } } return isValid; }
private void JudgeChange() { if (_lastTargetPos != _curTargetPos || _lastTargetSize != _curTargetSize) { _lastTargetPos = _curTargetPos; _lastTargetSize = _curTargetSize;
RectTransformUtility.ScreenPointToLocalPointInRectangle( rectTransform, RectTransformUtility.WorldToScreenPoint(CurrentCamera, _lastTargetPos), CurrentCamera, out _targetLocalPos );
SetAllDirty(); } } private void GetValidRectData() { List<float> pointsX = new(); List<float> pointsY = new(); Tuple<float[], float[]> tuple; foreach (RectTransform rect in TargetUI) { tuple = GetRectCornerPoints(rect); pointsX.AddRange(tuple.Item1); pointsY.AddRange(tuple.Item2); } var arrayX = pointsX.ToArray(); var arrayY = pointsY.ToArray(); _leftX = Mathf.Min(arrayX); _rightX = Mathf.Max(arrayX); _bottomY = Mathf.Min(arrayY); _topY = Mathf.Max(arrayY);
_curTargetSize = new Vector2(_rightX - _leftX, _topY - _bottomY); _curTargetPos = new Vector3(_leftX + _curTargetSize.x * 0.5f, _bottomY + _curTargetSize.y * 0.5f); } private Tuple<float[],float[]> GetRectCornerPoints(RectTransform rect) { float[] cornerX = new float[2]; float[] cornerY = new float[2]; float width = rect.rect.width * rect.localScale.x; float height = rect.rect.height * rect.localScale.y; cornerX[0] = rect.position.x - width * 0.5f; cornerX[1] = rect.position.x + width * 0.5f; cornerY[0] = rect.position.y - height * 0.5f; cornerY[1] = rect.position.y + height * 0.5f; return new Tuple<float[], float[]>(cornerX,cornerY); } public bool IsRaycastLocationValid(Vector2 sp, Camera eventCamera) { if (!CanClick || !JudgeTargetValid()) return true;
IsInValidRange = false;
IsInValidRange = JudgePointInInnerRect(sp); return !IsInValidRange; } private bool JudgePointInInnerRect(Vector3 point) { return point.x >= _leftX && point.x <= _rightX && point.y >= _bottomY && point.y <= _topY; } protected override void OnPopulateMesh(VertexHelper vh) { vh.Clear();
if (!IsValid) { return; }
UpdateVertexData();
AddVertices(_outer, vh); AddVertices(_inner, vh);
int count = 4;
for (int i = 0; i < count; ++i) { AddTriangles(vh, i, count); } }
private void AddVertices(Vector2[] vertices, VertexHelper vh) { for (int i = 0; i < vertices.Length; i++) { UIVertex vertex = new() { position = vertices[i], color = color }; vh.AddVert(vertex); } } private void AddTriangles(VertexHelper vh, int startIndex, int count) { int outerEnd = GetValidIndex(startIndex + 1, count); int innerEnd = GetValidIndex(startIndex + count + 1, count); vh.AddTriangle(startIndex, outerEnd, startIndex + count); vh.AddTriangle(innerEnd, startIndex + count, outerEnd); } private int GetValidIndex(int current, int count) { if (current % count != 0) return current; else return (current / count - 1) * count; } private void UpdateVertexData() { float outerLeftX = -rectTransform.pivot.x * rectTransform.rect.width; float outerRightX = (1 - rectTransform.pivot.x) * rectTransform.rect.width; float outerBottomY = -rectTransform.pivot.y * rectTransform.rect.height; float outerUpperY = (1 - rectTransform.pivot.y) * rectTransform.rect.height;
_outer[0] = new Vector2(outerLeftX, outerBottomY); _outer[1] = new Vector2(outerLeftX, outerUpperY); _outer[2] = new Vector2(outerRightX, outerUpperY); _outer[3] = new Vector2(outerRightX, outerBottomY);
float width = _curTargetSize.x; float height = _curTargetSize.y;
float innerLeftX = _targetLocalPos.x - width * 0.5f; float innerRightX = _targetLocalPos.x + width * 0.5f; float innerBottomY = _targetLocalPos.y - height * 0.5f; float innerUpperY = _targetLocalPos.y + height * 0.5f;
_inner[0] = new Vector2(innerLeftX, innerBottomY); _inner[1] = new Vector2(innerLeftX, innerUpperY); _inner[2] = new Vector2(innerRightX, innerUpperY); _inner[3] = new Vector2(innerRightX, innerBottomY); } }
|