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

Commit 9f0f7f9

Browse files
committed
Prevent some niche exceptions with EventSystem
1 parent 383c6f1 commit 9f0f7f9

File tree

2 files changed

+20
-3
lines changed

2 files changed

+20
-3
lines changed

src/UI/CSConsole/ConsoleController.cs

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
using UnityExplorer.Core.Input;
1212
using UnityExplorer.UI.Panels;
1313
using UnityExplorer.UI.Widgets.AutoComplete;
14+
using System.Reflection;
1415

1516
namespace UnityExplorer.UI.CSConsole
1617
{
@@ -328,15 +329,28 @@ private static void SetCaretPosition(int caretPosition)
328329
RuntimeProvider.Instance.StartCoroutine(SetAutocompleteCaretCoro(caretPosition));
329330
}
330331

332+
internal static PropertyInfo SelectionGuardProperty => selectionGuardPropInfo ?? GetSelectionGuardPropInfo();
333+
334+
private static PropertyInfo GetSelectionGuardPropInfo()
335+
{
336+
selectionGuardPropInfo = typeof(EventSystem).GetProperty("m_SelectionGuard");
337+
if (selectionGuardPropInfo == null)
338+
selectionGuardPropInfo = typeof(EventSystem).GetProperty("m_selectionGuard");
339+
return selectionGuardPropInfo;
340+
}
341+
342+
private static PropertyInfo selectionGuardPropInfo;
343+
331344
private static IEnumerator SetAutocompleteCaretCoro(int caretPosition)
332345
{
333346
var color = Input.Component.selectionColor;
334347
color.a = 0f;
335348
Input.Component.selectionColor = color;
336-
EventSystem.current.SetSelectedGameObject(null, null);
349+
try { EventSystem.current.SetSelectedGameObject(null, null); } catch { }
337350
yield return null;
338351

339-
EventSystem.current.SetSelectedGameObject(Input.UIRoot, null);
352+
try { SelectionGuardProperty.SetValue(EventSystem.current, false, null); } catch { }
353+
try { EventSystem.current.SetSelectedGameObject(Input.UIRoot, null); } catch { }
340354
Input.Component.Select();
341355
yield return null;
342356

src/UI/Models/InputFieldRef.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,11 @@ public static void UpdateInstances()
1616
{
1717
if (inputsPendingUpdate.Any())
1818
{
19-
foreach (var entry in inputsPendingUpdate)
19+
var array = inputsPendingUpdate.ToArray();
20+
21+
for (int i = array.Length - 1; i >= 0; i--)
2022
{
23+
var entry = array[i];
2124
LayoutRebuilder.MarkLayoutForRebuild(entry.Rect);
2225
entry.OnValueChanged?.Invoke(entry.Component.text);
2326
}

0 commit comments

Comments
 (0)