8
8
using UnityExplorer . Core ;
9
9
using UnityExplorer . Core . Input ;
10
10
using UnityExplorer . Core . Runtime ;
11
+ using UnityExplorer . Inspectors . MouseInspectors ;
11
12
using UnityExplorer . UI ;
12
13
using UnityExplorer . UI . Panels ;
13
14
@@ -23,17 +24,27 @@ public class InspectUnderMouse : UIPanel
23
24
{
24
25
public static InspectUnderMouse Instance { get ; private set ; }
25
26
26
- public InspectUnderMouse ( ) { Instance = this ; }
27
+ private readonly WorldInspector worldInspector ;
28
+ private readonly UiInspector uiInspector ;
27
29
28
- public static void OnDropdownSelect ( int index )
30
+ public static bool Inspecting { get ; set ; }
31
+ public static MouseInspectMode Mode { get ; set ; }
32
+
33
+ private static Vector3 lastMousePos ;
34
+
35
+ public MouseInspectorBase CurrentInspector
29
36
{
30
- switch ( index )
37
+ get
31
38
{
32
- case 0 : return ;
33
- case 1 : Instance . StartInspect ( MouseInspectMode . World ) ; break ;
34
- case 2 : Instance . StartInspect ( MouseInspectMode . UI ) ; break ;
39
+ switch ( Mode )
40
+ {
41
+ case MouseInspectMode . UI :
42
+ return uiInspector ;
43
+ case MouseInspectMode . World :
44
+ return worldInspector ;
45
+ }
46
+ return null ;
35
47
}
36
- UIManager . MouseInspectDropdown . value = 0 ;
37
48
}
38
49
39
50
// UIPanel
@@ -46,56 +57,54 @@ public static void OnDropdownSelect(int index)
46
57
public override bool ShouldSaveActiveState => false ;
47
58
public override bool ShowByDefault => false ;
48
59
49
- private static Text objNameLabel ;
50
- private static Text objPathLabel ;
51
- private static Text mousePosLabel ;
60
+ internal Text objNameLabel ;
61
+ internal Text objPathLabel ;
62
+ internal Text mousePosLabel ;
52
63
53
- // Mouse Inspector
54
- public static bool Inspecting { get ; set ; }
55
- public static MouseInspectMode Mode { get ; set ; }
56
-
57
- private static Camera MainCamera ;
58
- private static GraphicRaycaster [ ] graphicRaycasters ;
59
-
60
- private static GameObject lastHitObject ;
61
- private static Vector3 lastMousePos ;
62
-
63
- private static readonly List < Graphic > wasDisabledGraphics = new List < Graphic > ( ) ;
64
- private static readonly List < CanvasGroup > wasDisabledCanvasGroups = new List < CanvasGroup > ( ) ;
65
- private static readonly List < GameObject > objectsAddedCastersTo = new List < GameObject > ( ) ;
64
+ public InspectUnderMouse ( )
65
+ {
66
+ Instance = this ;
67
+ worldInspector = new WorldInspector ( ) ;
68
+ uiInspector = new UiInspector ( ) ;
69
+ }
66
70
67
- public void StartInspect ( MouseInspectMode mode )
71
+ public static void OnDropdownSelect ( int index )
68
72
{
69
- MainCamera = Camera . main ;
70
-
71
- if ( ! MainCamera && mode == MouseInspectMode . World )
73
+ switch ( index )
72
74
{
73
- ExplorerCore . LogWarning ( "No MainCamera found! Cannot inspect world!" ) ;
74
- return ;
75
+ case 0 : return ;
76
+ case 1 : Instance . StartInspect ( MouseInspectMode . World ) ; break ;
77
+ case 2 : Instance . StartInspect ( MouseInspectMode . UI ) ; break ;
75
78
}
79
+ UIManager . MouseInspectDropdown . value = 0 ;
80
+ }
76
81
77
- PanelDragger . ForceEnd ( ) ;
78
-
82
+ public void StartInspect ( MouseInspectMode mode )
83
+ {
79
84
Mode = mode ;
80
85
Inspecting = true ;
86
+
87
+ CurrentInspector . OnBeginMouseInspect ( ) ;
88
+
89
+ PanelDragger . ForceEnd ( ) ;
81
90
UIManager . NavBarRect . gameObject . SetActive ( false ) ;
82
91
UIManager . PanelHolder . SetActive ( false ) ;
83
92
84
93
UIRoot . SetActive ( true ) ;
85
-
86
- if ( mode == MouseInspectMode . UI )
87
- SetupUIRaycast ( ) ;
88
94
}
89
95
90
96
internal void ClearHitData ( )
91
97
{
92
- lastHitObject = null ;
98
+ CurrentInspector . ClearHitData ( ) ;
99
+
93
100
objNameLabel . text = "No hits..." ;
94
101
objPathLabel . text = "" ;
95
102
}
96
103
97
104
public void StopInspect ( )
98
105
{
106
+ CurrentInspector . OnEndInspect ( ) ;
107
+ ClearHitData ( ) ;
99
108
Inspecting = false ;
100
109
101
110
UIManager . NavBarRect . gameObject . SetActive ( true ) ;
@@ -106,11 +115,6 @@ public void StopInspect()
106
115
drop . DestroyDropdownList ( list . gameObject ) ;
107
116
108
117
UIRoot . SetActive ( false ) ;
109
-
110
- if ( Mode == MouseInspectMode . UI )
111
- StopUIInspect ( ) ;
112
-
113
- ClearHitData ( ) ;
114
118
}
115
119
116
120
private static float timeOfLastRaycast ;
@@ -123,33 +127,22 @@ public void UpdateInspect()
123
127
return ;
124
128
}
125
129
126
- if ( lastHitObject && InputManager . GetMouseButtonDown ( 0 ) )
130
+ if ( InputManager . GetMouseButtonDown ( 0 ) )
127
131
{
128
- var target = lastHitObject ;
132
+ CurrentInspector . OnSelectMouseInspect ( ) ;
129
133
StopInspect ( ) ;
130
- InspectorManager . Inspect ( target ) ;
131
134
return ;
132
135
}
133
136
134
137
var mousePos = InputManager . MousePosition ;
135
-
136
138
if ( mousePos != lastMousePos )
137
139
UpdatePosition ( mousePos ) ;
138
140
139
141
if ( ! timeOfLastRaycast . OccuredEarlierThan ( 0.1f ) )
140
142
return ;
141
-
142
143
timeOfLastRaycast = Time . realtimeSinceStartup ;
143
144
144
- // actual inspect raycast
145
-
146
- switch ( Mode )
147
- {
148
- case MouseInspectMode . UI :
149
- RaycastUI ( mousePos ) ; break ;
150
- case MouseInspectMode . World :
151
- RaycastWorld ( mousePos ) ; break ;
152
- }
145
+ CurrentInspector . UpdateMouseInspect ( mousePos ) ;
153
146
}
154
147
155
148
internal void UpdatePosition ( Vector2 mousePos )
@@ -171,181 +164,9 @@ internal void UpdatePosition(Vector2 mousePos)
171
164
172
165
// calculate and set our UI position
173
166
var inversePos = UIManager . CanvasRoot . transform . InverseTransformPoint ( mousePos ) ;
174
-
175
167
UIRoot . transform . localPosition = new Vector3 ( inversePos . x , inversePos . y , 0 ) ;
176
168
}
177
169
178
- internal void OnHitGameObject ( GameObject obj )
179
- {
180
- if ( obj != lastHitObject )
181
- {
182
- lastHitObject = obj ;
183
- objNameLabel . text = $ "<b>Click to Inspect:</b> <color=cyan>{ obj . name } </color>";
184
- objPathLabel . text = $ "Path: { obj . transform . GetTransformPath ( true ) } ";
185
- }
186
- }
187
-
188
- // Collider raycasting
189
-
190
- internal void RaycastWorld ( Vector2 mousePos )
191
- {
192
- var ray = MainCamera . ScreenPointToRay ( mousePos ) ;
193
- Physics . Raycast ( ray , out RaycastHit hit , 1000f ) ;
194
-
195
- if ( hit . transform )
196
- {
197
- var obj = hit . transform . gameObject ;
198
- OnHitGameObject ( obj ) ;
199
- }
200
- else
201
- {
202
- if ( lastHitObject )
203
- ClearHitData ( ) ;
204
- }
205
- }
206
-
207
- // UI Graphic raycasting
208
-
209
- private static void SetupUIRaycast ( )
210
- {
211
- foreach ( var obj in RuntimeProvider . Instance . FindObjectsOfTypeAll ( typeof ( Canvas ) ) )
212
- {
213
- var canvas = obj . TryCast < Canvas > ( ) ;
214
- if ( ! canvas || ! canvas . enabled || ! canvas . gameObject . activeInHierarchy )
215
- continue ;
216
- if ( ! canvas . GetComponent < GraphicRaycaster > ( ) )
217
- {
218
- canvas . gameObject . AddComponent < GraphicRaycaster > ( ) ;
219
- //ExplorerCore.Log("Added raycaster to " + canvas.name);
220
- objectsAddedCastersTo . Add ( canvas . gameObject ) ;
221
- }
222
- }
223
-
224
- // recache Graphic Raycasters each time we start
225
- var casters = RuntimeProvider . Instance . FindObjectsOfTypeAll ( typeof ( GraphicRaycaster ) ) ;
226
- graphicRaycasters = new GraphicRaycaster [ casters . Length ] ;
227
- for ( int i = 0 ; i < casters . Length ; i ++ )
228
- {
229
- graphicRaycasters [ i ] = casters [ i ] . TryCast < GraphicRaycaster > ( ) ;
230
- }
231
-
232
- // enable raycastTarget on Graphics
233
- foreach ( var obj in RuntimeProvider . Instance . FindObjectsOfTypeAll ( typeof ( Graphic ) ) )
234
- {
235
- var graphic = obj . TryCast < Graphic > ( ) ;
236
- if ( ! graphic || ! graphic . enabled || graphic . raycastTarget || ! graphic . gameObject . activeInHierarchy )
237
- continue ;
238
- graphic . raycastTarget = true ;
239
- //ExplorerCore.Log("Enabled raycastTarget on " + graphic.name);
240
- wasDisabledGraphics . Add ( graphic ) ;
241
- }
242
-
243
- // enable blocksRaycasts on CanvasGroups
244
- foreach ( var obj in RuntimeProvider . Instance . FindObjectsOfTypeAll ( typeof ( CanvasGroup ) ) )
245
- {
246
- var canvas = obj . TryCast < CanvasGroup > ( ) ;
247
- if ( ! canvas || ! canvas . gameObject . activeInHierarchy || canvas . blocksRaycasts )
248
- continue ;
249
- canvas . blocksRaycasts = true ;
250
- //ExplorerCore.Log("Enabled raycasts on " + canvas.name);
251
- wasDisabledCanvasGroups . Add ( canvas ) ;
252
- }
253
- }
254
-
255
- internal void RaycastUI ( Vector2 mousePos )
256
- {
257
- var ped = new PointerEventData ( null )
258
- {
259
- position = mousePos
260
- } ;
261
-
262
- //ExplorerCore.Log("~~~~~~~~~ begin raycast ~~~~~~~~");
263
- GameObject hitObject = null ;
264
- int highestLayer = int . MinValue ;
265
- int highestOrder = int . MinValue ;
266
- int highestDepth = int . MinValue ;
267
- foreach ( var gr in graphicRaycasters )
268
- {
269
- if ( ! gr || ! gr . canvas )
270
- continue ;
271
-
272
- var list = new List < RaycastResult > ( ) ;
273
- RuntimeProvider . Instance . GraphicRaycast ( gr , ped , list ) ;
274
-
275
- if ( list . Count > 0 )
276
- {
277
- foreach ( var hit in list )
278
- {
279
- // Manualy trying to determine which object is "on top".
280
- // Could be improved, but seems to work pretty well and isn't as laggy as you would expect.
281
-
282
- if ( ! hit . gameObject )
283
- continue ;
284
-
285
- if ( hit . gameObject . GetComponent < CanvasGroup > ( ) is CanvasGroup group && group . alpha == 0 )
286
- continue ;
287
-
288
- if ( hit . gameObject . GetComponent < Graphic > ( ) is Graphic graphic && graphic . color . a == 0f )
289
- continue ;
290
-
291
- if ( hit . sortingLayer < highestLayer )
292
- continue ;
293
-
294
- if ( hit . sortingLayer > highestLayer )
295
- {
296
- highestLayer = hit . sortingLayer ;
297
- highestDepth = int . MinValue ;
298
- }
299
-
300
- if ( hit . depth < highestDepth )
301
- continue ;
302
-
303
- if ( hit . depth > highestDepth )
304
- {
305
- highestDepth = hit . depth ;
306
- highestOrder = int . MinValue ;
307
- }
308
-
309
- if ( hit . sortingOrder <= highestOrder )
310
- continue ;
311
-
312
- highestOrder = hit . sortingOrder ;
313
- hitObject = hit . gameObject ;
314
- }
315
- }
316
- else
317
- {
318
- if ( lastHitObject )
319
- ClearHitData ( ) ;
320
- }
321
- }
322
-
323
- if ( hitObject )
324
- OnHitGameObject ( hitObject ) ;
325
-
326
- //ExplorerCore.Log("~~~~~~~~~ end raycast ~~~~~~~~");
327
- }
328
-
329
- private static void StopUIInspect ( )
330
- {
331
- foreach ( var obj in objectsAddedCastersTo )
332
- {
333
- if ( obj . GetComponent < GraphicRaycaster > ( ) is GraphicRaycaster raycaster )
334
- GameObject . Destroy ( raycaster ) ;
335
- }
336
-
337
- foreach ( var graphic in wasDisabledGraphics )
338
- graphic . raycastTarget = false ;
339
-
340
- foreach ( var canvas in wasDisabledCanvasGroups )
341
- canvas . blocksRaycasts = false ;
342
-
343
- objectsAddedCastersTo . Clear ( ) ;
344
- wasDisabledCanvasGroups . Clear ( ) ;
345
- wasDisabledGraphics . Clear ( ) ;
346
- }
347
-
348
-
349
170
// UI Construction
350
171
351
172
protected internal override void DoSetDefaultPosAndAnchors ( )
@@ -367,7 +188,10 @@ public override void ConstructPanelContent()
367
188
368
189
// Title text
369
190
370
- var title = UIFactory . CreateLabel ( inspectContent , "InspectLabel" , "<b>Mouse Inspector</b> (press <b>ESC</b> to cancel)" , TextAnchor . MiddleCenter ) ;
191
+ var title = UIFactory . CreateLabel ( inspectContent ,
192
+ "InspectLabel" ,
193
+ "<b>Mouse Inspector</b> (press <b>ESC</b> to cancel)" ,
194
+ TextAnchor . MiddleCenter ) ;
371
195
UIFactory . SetLayoutElement ( title . gameObject , flexibleWidth : 9999 ) ;
372
196
373
197
mousePosLabel = UIFactory . CreateLabel ( inspectContent , "MousePosLabel" , "Mouse Position:" , TextAnchor . MiddleCenter ) ;
0 commit comments