3
3
using System . Linq ;
4
4
using System . Text ;
5
5
using UnityEngine ;
6
+ using UnityEngine . EventSystems ;
6
7
using UnityEngine . UI ;
7
8
using UnityExplorer . Helpers ;
8
9
using UnityExplorer . Input ;
9
10
using UnityExplorer . UI ;
11
+ using UnityExplorer . Unstrip ;
10
12
11
13
namespace UnityExplorer . Inspectors
12
14
{
13
15
public class MouseInspector
14
16
{
17
+ public enum MouseInspectMode
18
+ {
19
+ World ,
20
+ UI
21
+ }
22
+
15
23
public static bool Enabled { get ; set ; }
16
24
17
- //internal static Text s_objUnderMouseName;
25
+ public static MouseInspectMode Mode { get ; set ; }
26
+
18
27
internal static Text s_objNameLabel ;
19
28
internal static Text s_objPathLabel ;
20
29
internal static Text s_mousePosLabel ;
@@ -29,6 +38,18 @@ public static void StartInspect()
29
38
Enabled = true ;
30
39
MainMenu . Instance . MainPanel . SetActive ( false ) ;
31
40
s_UIContent . SetActive ( true ) ;
41
+
42
+ // recache Graphic Raycasters each time we start
43
+ var casters = ResourcesUnstrip . FindObjectsOfTypeAll ( typeof ( GraphicRaycaster ) ) ;
44
+ m_gCasters = new GraphicRaycaster [ casters . Length ] ;
45
+ for ( int i = 0 ; i < casters . Length ; i ++ )
46
+ {
47
+ #if CPP
48
+ m_gCasters [ i ] = casters [ i ] . TryCast < GraphicRaycaster > ( ) ;
49
+ #else
50
+ m_gCasters [ i ] = casters [ i ] as GraphicRaycaster ;
51
+ #endif
52
+ }
32
53
}
33
54
34
55
public static void StopInspect ( )
@@ -40,49 +61,68 @@ public static void StopInspect()
40
61
ClearHitData ( ) ;
41
62
}
42
63
64
+ internal static GraphicRaycaster [ ] m_gCasters ;
65
+
43
66
public static void UpdateInspect ( )
44
67
{
45
68
if ( InputManager . GetKeyDown ( KeyCode . Escape ) )
46
69
{
47
70
StopInspect ( ) ;
71
+ return ;
48
72
}
49
73
50
74
var mousePos = InputManager . MousePosition ;
51
75
52
76
if ( mousePos != s_lastMousePos )
53
- {
54
- s_lastMousePos = mousePos ;
77
+ UpdatePosition ( mousePos ) ;
55
78
56
- var inversePos = UIManager . CanvasRoot . transform . InverseTransformPoint ( mousePos ) ;
79
+ if ( ! UnityHelpers . MainCamera )
80
+ return ;
57
81
58
- s_mousePosLabel . text = $ "<color=grey>Mouse Position:</color> { ( ( Vector2 ) InputManager . MousePosition ) . ToString ( ) } " ;
82
+ // actual inspect raycast
59
83
60
- float yFix = mousePos . y < 120 ? 80 : - 80 ;
84
+ switch ( Mode )
85
+ {
86
+ case MouseInspectMode . UI :
87
+ RaycastUI ( mousePos ) ; break ;
88
+ case MouseInspectMode . World :
89
+ RaycastWorld ( mousePos ) ; break ;
90
+ }
91
+ }
61
92
62
- s_UIContent . transform . localPosition = new Vector3 ( inversePos . x , inversePos . y + yFix , 0 ) ;
93
+ internal static void OnHitGameObject ( GameObject obj )
94
+ {
95
+ if ( obj != s_lastHit )
96
+ {
97
+ s_lastHit = obj ;
98
+ s_objNameLabel . text = $ "<b>Click to Inspect:</b> <color=cyan>{ obj . name } </color>";
99
+ s_objPathLabel . text = $ "Path: { obj . transform . GetTransformPath ( true ) } ";
63
100
}
64
101
65
- if ( ! UnityHelpers . MainCamera )
66
- return ;
102
+ if ( InputManager . GetMouseButtonDown ( 0 ) )
103
+ {
104
+ StopInspect ( ) ;
105
+ InspectorManager . Instance . Inspect ( obj ) ;
106
+ }
107
+ }
67
108
68
- // actual inspect raycast
109
+ internal static void RaycastWorld ( Vector2 mousePos )
110
+ {
69
111
var ray = UnityHelpers . MainCamera . ScreenPointToRay ( mousePos ) ;
112
+ var casts = Physics . RaycastAll ( ray , 1000f ) ;
70
113
71
- if ( Physics . Raycast ( ray , out RaycastHit hit , 1000f ) )
114
+ if ( casts . Length > 0 )
72
115
{
73
- var obj = hit . transform . gameObject ;
74
-
75
- if ( obj != s_lastHit )
116
+ foreach ( var cast in casts )
76
117
{
77
- s_lastHit = obj ;
78
- s_objNameLabel . text = $ "<b>Click to Inspect:</b> <color=cyan>{ obj . name } </color>";
79
- s_objPathLabel . text = $ "Path: { obj . transform . GetTransformPath ( true ) } ";
80
- }
118
+ if ( cast . transform )
119
+ {
120
+ var obj = cast . transform . gameObject ;
81
121
82
- if ( InputManager . GetMouseButtonDown ( 0 ) )
83
- {
84
- StopInspect ( ) ;
85
- InspectorManager . Instance . Inspect ( obj ) ;
122
+ OnHitGameObject ( obj ) ;
123
+
124
+ break ;
125
+ }
86
126
}
87
127
}
88
128
else
@@ -92,14 +132,64 @@ public static void UpdateInspect()
92
132
}
93
133
}
94
134
135
+ internal static void RaycastUI ( Vector2 mousePos )
136
+ {
137
+ var ped = new PointerEventData ( null )
138
+ {
139
+ position = mousePos
140
+ } ;
141
+
142
+ #if MONO
143
+ var list = new List < RaycastResult > ( ) ;
144
+ #else
145
+ var list = new Il2CppSystem . Collections . Generic . List < RaycastResult > ( ) ;
146
+ #endif
147
+ foreach ( var gr in m_gCasters )
148
+ {
149
+ gr . Raycast ( ped , list ) ;
150
+
151
+ if ( list . Count > 0 )
152
+ {
153
+ foreach ( var hit in list )
154
+ {
155
+ if ( hit . gameObject )
156
+ {
157
+ var obj = hit . gameObject ;
158
+
159
+ OnHitGameObject ( obj ) ;
160
+
161
+ break ;
162
+ }
163
+ }
164
+ }
165
+ else
166
+ {
167
+ if ( s_lastHit )
168
+ ClearHitData ( ) ;
169
+ }
170
+ }
171
+ }
172
+
173
+ internal static void UpdatePosition ( Vector2 mousePos )
174
+ {
175
+ s_lastMousePos = mousePos ;
176
+
177
+ var inversePos = UIManager . CanvasRoot . transform . InverseTransformPoint ( mousePos ) ;
178
+
179
+ s_mousePosLabel . text = $ "<color=grey>Mouse Position:</color> { mousePos . ToString ( ) } ";
180
+
181
+ float yFix = mousePos . y < 120 ? 80 : - 80 ;
182
+ s_UIContent . transform . localPosition = new Vector3 ( inversePos . x , inversePos . y + yFix , 0 ) ;
183
+ }
184
+
95
185
internal static void ClearHitData ( )
96
186
{
97
187
s_lastHit = null ;
98
188
s_objNameLabel . text = "No hits..." ;
99
189
s_objPathLabel . text = "" ;
100
190
}
101
191
102
- #region UI Construction
192
+ #region UI Construction
103
193
104
194
internal static void ConstructUI ( )
105
195
{
@@ -112,7 +202,10 @@ internal static void ConstructUI()
112
202
baseRect . anchorMin = half ;
113
203
baseRect . anchorMax = half ;
114
204
baseRect . pivot = half ;
115
- baseRect . sizeDelta = new Vector2 ( 700 , 100 ) ;
205
+ baseRect . sizeDelta = new Vector2 ( 700 , 150 ) ;
206
+
207
+ var group = content . GetComponent < VerticalLayoutGroup > ( ) ;
208
+ group . childForceExpandHeight = true ;
116
209
117
210
// Title text
118
211
@@ -131,13 +224,16 @@ internal static void ConstructUI()
131
224
132
225
var pathLabelObj = UIFactory . CreateLabel ( content , TextAnchor . MiddleLeft ) ;
133
226
s_objPathLabel = pathLabelObj . GetComponent < Text > ( ) ;
134
- s_objPathLabel . color = Color . grey ;
135
227
s_objPathLabel . fontStyle = FontStyle . Italic ;
136
- s_objPathLabel . horizontalOverflow = HorizontalWrapMode . Overflow ;
228
+ s_objPathLabel . horizontalOverflow = HorizontalWrapMode . Wrap ;
229
+
230
+ var pathLayout = pathLabelObj . AddComponent < LayoutElement > ( ) ;
231
+ pathLayout . minHeight = 75 ;
232
+ pathLayout . flexibleHeight = 0 ;
137
233
138
234
s_UIContent . SetActive ( false ) ;
139
235
}
140
236
141
- #endregion
237
+ #endregion
142
238
}
143
239
}
0 commit comments