@@ -37,28 +37,40 @@ static InspectUnderMouse()
37
37
UI = new MouseInspectorUI ( ) ;
38
38
}
39
39
40
- public static void StartInspect ( )
40
+ private static readonly List < Graphic > _wasDisabledGraphics = new List < Graphic > ( ) ;
41
+ private static readonly List < CanvasGroup > _wasDisabledCanvasGroups = new List < CanvasGroup > ( ) ;
42
+ private static readonly List < GameObject > _objectsAddedCastersTo = new List < GameObject > ( ) ;
43
+
44
+ public static void StartInspect ( MouseInspectMode mode )
41
45
{
46
+ Mode = mode ;
42
47
Enabled = true ;
43
48
MainMenu . Instance . MainPanel . SetActive ( false ) ;
44
49
45
50
UI . s_UIContent . SetActive ( true ) ;
46
51
47
- // recache Graphic Raycasters each time we start
48
- var casters = RuntimeProvider . Instance . FindObjectsOfTypeAll ( typeof ( GraphicRaycaster ) ) ;
49
- m_gCasters = new GraphicRaycaster [ casters . Length ] ;
50
- for ( int i = 0 ; i < casters . Length ; i ++ )
52
+ if ( mode == MouseInspectMode . UI )
51
53
{
52
- m_gCasters [ i ] = casters [ i ] . Cast ( typeof ( GraphicRaycaster ) ) as GraphicRaycaster ;
54
+ SetupUIRaycast ( ) ;
53
55
}
54
56
}
55
57
58
+ internal static void ClearHitData ( )
59
+ {
60
+ s_lastHit = null ;
61
+ UI . s_objNameLabel . text = "No hits..." ;
62
+ UI . s_objPathLabel . text = "" ;
63
+ }
64
+
56
65
public static void StopInspect ( )
57
66
{
58
67
Enabled = false ;
59
68
MainMenu . Instance . MainPanel . SetActive ( true ) ;
60
69
UI . s_UIContent . SetActive ( false ) ;
61
70
71
+ if ( Mode == MouseInspectMode . UI )
72
+ StopUIInspect ( ) ;
73
+
62
74
ClearHitData ( ) ;
63
75
}
64
76
@@ -91,6 +103,18 @@ public static void UpdateInspect()
91
103
}
92
104
}
93
105
106
+ internal static void UpdatePosition ( Vector2 mousePos )
107
+ {
108
+ s_lastMousePos = mousePos ;
109
+
110
+ var inversePos = UIManager . CanvasRoot . transform . InverseTransformPoint ( mousePos ) ;
111
+
112
+ UI . s_mousePosLabel . text = $ "<color=grey>Mouse Position:</color> { mousePos . ToString ( ) } ";
113
+
114
+ float yFix = mousePos . y < 120 ? 80 : - 80 ;
115
+ UI . s_UIContent . transform . localPosition = new Vector3 ( inversePos . x , inversePos . y + yFix , 0 ) ;
116
+ }
117
+
94
118
internal static void OnHitGameObject ( GameObject obj )
95
119
{
96
120
if ( obj != s_lastHit )
@@ -107,6 +131,8 @@ internal static void OnHitGameObject(GameObject obj)
107
131
}
108
132
}
109
133
134
+ // Collider raycasting
135
+
110
136
internal static void RaycastWorld ( Vector2 mousePos )
111
137
{
112
138
var ray = UnityHelper . MainCamera . ScreenPointToRay ( mousePos ) ;
@@ -124,6 +150,54 @@ internal static void RaycastWorld(Vector2 mousePos)
124
150
}
125
151
}
126
152
153
+ // UI Graphic raycasting
154
+
155
+ private static void SetupUIRaycast ( )
156
+ {
157
+ foreach ( var obj in RuntimeProvider . Instance . FindObjectsOfTypeAll ( typeof ( Canvas ) ) )
158
+ {
159
+ var canvas = obj . Cast ( typeof ( Canvas ) ) as Canvas ;
160
+ if ( ! canvas || ! canvas . enabled || ! canvas . gameObject . activeInHierarchy )
161
+ continue ;
162
+ if ( ! canvas . GetComponent < GraphicRaycaster > ( ) )
163
+ {
164
+ canvas . gameObject . AddComponent < GraphicRaycaster > ( ) ;
165
+ //ExplorerCore.Log("Added raycaster to " + canvas.name);
166
+ _objectsAddedCastersTo . Add ( canvas . gameObject ) ;
167
+ }
168
+ }
169
+
170
+ // recache Graphic Raycasters each time we start
171
+ var casters = RuntimeProvider . Instance . FindObjectsOfTypeAll ( typeof ( GraphicRaycaster ) ) ;
172
+ m_gCasters = new GraphicRaycaster [ casters . Length ] ;
173
+ for ( int i = 0 ; i < casters . Length ; i ++ )
174
+ {
175
+ m_gCasters [ i ] = casters [ i ] . Cast ( typeof ( GraphicRaycaster ) ) as GraphicRaycaster ;
176
+ }
177
+
178
+ // enable raycastTarget on Graphics
179
+ foreach ( var obj in RuntimeProvider . Instance . FindObjectsOfTypeAll ( typeof ( Graphic ) ) )
180
+ {
181
+ var graphic = obj . Cast ( typeof ( Graphic ) ) as Graphic ;
182
+ if ( ! graphic || ! graphic . enabled || graphic . raycastTarget || ! graphic . gameObject . activeInHierarchy )
183
+ continue ;
184
+ graphic . raycastTarget = true ;
185
+ //ExplorerCore.Log("Enabled raycastTarget on " + graphic.name);
186
+ _wasDisabledGraphics . Add ( graphic ) ;
187
+ }
188
+
189
+ // enable blocksRaycasts on CanvasGroups
190
+ foreach ( var obj in RuntimeProvider . Instance . FindObjectsOfTypeAll ( typeof ( CanvasGroup ) ) )
191
+ {
192
+ var canvas = obj . Cast ( typeof ( CanvasGroup ) ) as CanvasGroup ;
193
+ if ( ! canvas || ! canvas . gameObject . activeInHierarchy || canvas . blocksRaycasts )
194
+ continue ;
195
+ canvas . blocksRaycasts = true ;
196
+ //ExplorerCore.Log("Enabled raycasts on " + canvas.name);
197
+ _wasDisabledCanvasGroups . Add ( canvas ) ;
198
+ }
199
+ }
200
+
127
201
internal static void RaycastUI ( Vector2 mousePos )
128
202
{
129
203
var ped = new PointerEventData ( null )
@@ -136,6 +210,11 @@ internal static void RaycastUI(Vector2 mousePos)
136
210
#else
137
211
var list = new Il2CppSystem . Collections . Generic . List < RaycastResult > ( ) ;
138
212
#endif
213
+ //ExplorerCore.Log("~~~~~~~~~ begin raycast ~~~~~~~~");
214
+ GameObject hitObject = null ;
215
+ int highestLayer = int . MinValue ;
216
+ int highestOrder = int . MinValue ;
217
+ int highestDepth = int . MinValue ;
139
218
foreach ( var gr in m_gCasters )
140
219
{
141
220
gr . Raycast ( ped , list ) ;
@@ -144,14 +223,40 @@ internal static void RaycastUI(Vector2 mousePos)
144
223
{
145
224
foreach ( var hit in list )
146
225
{
147
- if ( hit . gameObject )
226
+ if ( ! hit . gameObject )
227
+ continue ;
228
+
229
+ if ( hit . gameObject . GetComponent < CanvasGroup > ( ) is CanvasGroup group && group . alpha == 0 )
230
+ continue ;
231
+
232
+ if ( hit . gameObject . GetComponent < Graphic > ( ) is Graphic graphic && graphic . color . a == 0f )
233
+ continue ;
234
+
235
+ //ExplorerCore.Log("Hit: " + hit.gameObject.name + ", depth: " + hit.depth + ", layer: " + hit.sortingLayer + ", order: " + hit.sortingOrder);
236
+
237
+ if ( hit . sortingLayer < highestLayer )
238
+ continue ;
239
+
240
+ if ( hit . sortingLayer > highestLayer )
148
241
{
149
- var obj = hit . gameObject ;
242
+ highestLayer = hit . sortingLayer ;
243
+ highestOrder = int . MinValue ;
244
+ }
150
245
151
- OnHitGameObject ( obj ) ;
246
+ if ( hit . depth < highestDepth )
247
+ continue ;
152
248
153
- break ;
249
+ if ( hit . depth > highestDepth )
250
+ {
251
+ highestDepth = hit . depth ;
252
+ highestOrder = int . MinValue ;
154
253
}
254
+
255
+ if ( hit . sortingOrder <= highestOrder )
256
+ continue ;
257
+
258
+ highestOrder = hit . sortingOrder ;
259
+ hitObject = hit . gameObject ;
155
260
}
156
261
}
157
262
else
@@ -160,25 +265,30 @@ internal static void RaycastUI(Vector2 mousePos)
160
265
ClearHitData ( ) ;
161
266
}
162
267
}
268
+
269
+ if ( hitObject )
270
+ OnHitGameObject ( hitObject ) ;
271
+
272
+ //ExplorerCore.Log("~~~~~~~~~ end raycast ~~~~~~~~");
163
273
}
164
274
165
- internal static void UpdatePosition ( Vector2 mousePos )
275
+ private static void StopUIInspect ( )
166
276
{
167
- s_lastMousePos = mousePos ;
168
-
169
- var inversePos = UIManager . CanvasRoot . transform . InverseTransformPoint ( mousePos ) ;
277
+ foreach ( var obj in _objectsAddedCastersTo )
278
+ {
279
+ if ( obj . GetComponent < GraphicRaycaster > ( ) is GraphicRaycaster raycaster )
280
+ GameObject . Destroy ( raycaster ) ;
281
+ }
170
282
171
- UI . s_mousePosLabel . text = $ "<color=grey>Mouse Position:</color> { mousePos . ToString ( ) } ";
283
+ foreach ( var graphic in _wasDisabledGraphics )
284
+ graphic . raycastTarget = false ;
172
285
173
- float yFix = mousePos . y < 120 ? 80 : - 80 ;
174
- UI . s_UIContent . transform . localPosition = new Vector3 ( inversePos . x , inversePos . y + yFix , 0 ) ;
175
- }
286
+ foreach ( var canvas in _wasDisabledCanvasGroups )
287
+ canvas . blocksRaycasts = false ;
176
288
177
- internal static void ClearHitData ( )
178
- {
179
- s_lastHit = null ;
180
- UI . s_objNameLabel . text = "No hits..." ;
181
- UI . s_objPathLabel . text = "" ;
289
+ _objectsAddedCastersTo . Clear ( ) ;
290
+ _wasDisabledCanvasGroups . Clear ( ) ;
291
+ _wasDisabledGraphics . Clear ( ) ;
182
292
}
183
293
}
184
294
}
0 commit comments