Skip to content
This repository was archived by the owner on May 9, 2023. It is now read-only.

Commit 418ece5

Browse files
committed
Improve UI inspect-under-mouse
1 parent bf45589 commit 418ece5

File tree

2 files changed

+134
-25
lines changed

2 files changed

+134
-25
lines changed

src/Core/Inspectors/InspectUnderMouse.cs

Lines changed: 133 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -37,28 +37,40 @@ static InspectUnderMouse()
3737
UI = new MouseInspectorUI();
3838
}
3939

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)
4145
{
46+
Mode = mode;
4247
Enabled = true;
4348
MainMenu.Instance.MainPanel.SetActive(false);
4449

4550
UI.s_UIContent.SetActive(true);
4651

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)
5153
{
52-
m_gCasters[i] = casters[i].Cast(typeof(GraphicRaycaster)) as GraphicRaycaster;
54+
SetupUIRaycast();
5355
}
5456
}
5557

58+
internal static void ClearHitData()
59+
{
60+
s_lastHit = null;
61+
UI.s_objNameLabel.text = "No hits...";
62+
UI.s_objPathLabel.text = "";
63+
}
64+
5665
public static void StopInspect()
5766
{
5867
Enabled = false;
5968
MainMenu.Instance.MainPanel.SetActive(true);
6069
UI.s_UIContent.SetActive(false);
6170

71+
if (Mode == MouseInspectMode.UI)
72+
StopUIInspect();
73+
6274
ClearHitData();
6375
}
6476

@@ -91,6 +103,18 @@ public static void UpdateInspect()
91103
}
92104
}
93105

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+
94118
internal static void OnHitGameObject(GameObject obj)
95119
{
96120
if (obj != s_lastHit)
@@ -107,6 +131,8 @@ internal static void OnHitGameObject(GameObject obj)
107131
}
108132
}
109133

134+
// Collider raycasting
135+
110136
internal static void RaycastWorld(Vector2 mousePos)
111137
{
112138
var ray = UnityHelper.MainCamera.ScreenPointToRay(mousePos);
@@ -124,6 +150,54 @@ internal static void RaycastWorld(Vector2 mousePos)
124150
}
125151
}
126152

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+
127201
internal static void RaycastUI(Vector2 mousePos)
128202
{
129203
var ped = new PointerEventData(null)
@@ -136,6 +210,11 @@ internal static void RaycastUI(Vector2 mousePos)
136210
#else
137211
var list = new Il2CppSystem.Collections.Generic.List<RaycastResult>();
138212
#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;
139218
foreach (var gr in m_gCasters)
140219
{
141220
gr.Raycast(ped, list);
@@ -144,14 +223,40 @@ internal static void RaycastUI(Vector2 mousePos)
144223
{
145224
foreach (var hit in list)
146225
{
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)
148241
{
149-
var obj = hit.gameObject;
242+
highestLayer = hit.sortingLayer;
243+
highestOrder = int.MinValue;
244+
}
150245

151-
OnHitGameObject(obj);
246+
if (hit.depth < highestDepth)
247+
continue;
152248

153-
break;
249+
if (hit.depth > highestDepth)
250+
{
251+
highestDepth = hit.depth;
252+
highestOrder = int.MinValue;
154253
}
254+
255+
if (hit.sortingOrder <= highestOrder)
256+
continue;
257+
258+
highestOrder = hit.sortingOrder;
259+
hitObject = hit.gameObject;
155260
}
156261
}
157262
else
@@ -160,25 +265,30 @@ internal static void RaycastUI(Vector2 mousePos)
160265
ClearHitData();
161266
}
162267
}
268+
269+
if (hitObject)
270+
OnHitGameObject(hitObject);
271+
272+
//ExplorerCore.Log("~~~~~~~~~ end raycast ~~~~~~~~");
163273
}
164274

165-
internal static void UpdatePosition(Vector2 mousePos)
275+
private static void StopUIInspect()
166276
{
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+
}
170282

171-
UI.s_mousePosLabel.text = $"<color=grey>Mouse Position:</color> {mousePos.ToString()}";
283+
foreach (var graphic in _wasDisabledGraphics)
284+
graphic.raycastTarget = false;
172285

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;
176288

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();
182292
}
183293
}
184294
}

src/UI/Main/Home/InspectorManagerUI.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -150,8 +150,7 @@ private static void AddMouseInspectButton(GameObject topRowObj, InspectUnderMous
150150

151151
void OnInspectMouseClicked()
152152
{
153-
InspectUnderMouse.Mode = mode;
154-
InspectUnderMouse.StartInspect();
153+
InspectUnderMouse.StartInspect(mode);
155154
}
156155
}
157156

0 commit comments

Comments
 (0)