From 9f0f7f9b571ed0085464f0cd9adf0ff1435a5cc2 Mon Sep 17 00:00:00 2001 From: Sinai Date: Sun, 23 May 2021 18:33:19 +1000 Subject: [PATCH] Prevent some niche exceptions with EventSystem --- src/UI/CSConsole/ConsoleController.cs | 18 ++++++++++++++++-- src/UI/Models/InputFieldRef.cs | 5 ++++- 2 files changed, 20 insertions(+), 3 deletions(-) diff --git a/src/UI/CSConsole/ConsoleController.cs b/src/UI/CSConsole/ConsoleController.cs index 5479fc5..376d9c9 100644 --- a/src/UI/CSConsole/ConsoleController.cs +++ b/src/UI/CSConsole/ConsoleController.cs @@ -11,6 +11,7 @@ using UnityExplorer.UI.CSConsole; using UnityExplorer.Core.Input; using UnityExplorer.UI.Panels; using UnityExplorer.UI.Widgets.AutoComplete; +using System.Reflection; namespace UnityExplorer.UI.CSConsole { @@ -328,15 +329,28 @@ namespace UnityExplorer.UI.CSConsole RuntimeProvider.Instance.StartCoroutine(SetAutocompleteCaretCoro(caretPosition)); } + internal static PropertyInfo SelectionGuardProperty => selectionGuardPropInfo ?? GetSelectionGuardPropInfo(); + + private static PropertyInfo GetSelectionGuardPropInfo() + { + selectionGuardPropInfo = typeof(EventSystem).GetProperty("m_SelectionGuard"); + if (selectionGuardPropInfo == null) + selectionGuardPropInfo = typeof(EventSystem).GetProperty("m_selectionGuard"); + return selectionGuardPropInfo; + } + + private static PropertyInfo selectionGuardPropInfo; + private static IEnumerator SetAutocompleteCaretCoro(int caretPosition) { var color = Input.Component.selectionColor; color.a = 0f; Input.Component.selectionColor = color; - EventSystem.current.SetSelectedGameObject(null, null); + try { EventSystem.current.SetSelectedGameObject(null, null); } catch { } yield return null; - EventSystem.current.SetSelectedGameObject(Input.UIRoot, null); + try { SelectionGuardProperty.SetValue(EventSystem.current, false, null); } catch { } + try { EventSystem.current.SetSelectedGameObject(Input.UIRoot, null); } catch { } Input.Component.Select(); yield return null; diff --git a/src/UI/Models/InputFieldRef.cs b/src/UI/Models/InputFieldRef.cs index fca7f37..3d539b3 100644 --- a/src/UI/Models/InputFieldRef.cs +++ b/src/UI/Models/InputFieldRef.cs @@ -16,8 +16,11 @@ namespace UnityExplorer.UI { if (inputsPendingUpdate.Any()) { - foreach (var entry in inputsPendingUpdate) + var array = inputsPendingUpdate.ToArray(); + + for (int i = array.Length - 1; i >= 0; i--) { + var entry = array[i]; LayoutRebuilder.MarkLayoutForRebuild(entry.Rect); entry.OnValueChanged?.Invoke(entry.Component.text); }