From bc5d16051f2656d0cfae738b9af5176e4754ddde Mon Sep 17 00:00:00 2001 From: Sinai <49360850+sinai-dev@users.noreply.github.com> Date: Thu, 30 Sep 2021 21:08:08 +1000 Subject: [PATCH] Fix issue with InputSystem --- src/Core/Input/CursorUnlocker.cs | 7 +------ src/Core/Input/IHandleInput.cs | 2 +- src/Core/Input/InputManager.cs | 36 +++++++++++++++++++------------- src/Core/Input/InputSystem.cs | 17 ++++++++++++--- src/Core/Input/LegacyInput.cs | 12 +++++++++-- src/Core/Input/NoInput.cs | 2 +- 6 files changed, 48 insertions(+), 28 deletions(-) diff --git a/src/Core/Input/CursorUnlocker.cs b/src/Core/Input/CursorUnlocker.cs index bb23dbc..4f7d891 100644 --- a/src/Core/Input/CursorUnlocker.cs +++ b/src/Core/Input/CursorUnlocker.cs @@ -113,9 +113,6 @@ namespace UnityExplorer.Core.Input public static void SetEventSystem() { - if (InputManager.CurrentType == InputType.InputSystem) - return; - if (EventSystem.current && EventSystem.current != UIManager.EventSys) { lastEventSystem = EventSystem.current; @@ -132,14 +129,12 @@ namespace UnityExplorer.Core.Input public static void ReleaseEventSystem() { - if (InputManager.CurrentType == InputType.InputSystem) - return; - if (lastEventSystem && lastEventSystem.gameObject.activeSelf) { lastEventSystem.enabled = true; settingEventSystem = true; + UIManager.EventSys.enabled = false; EventSystem.current = lastEventSystem; lastInputModule?.ActivateModule(); settingEventSystem = false; diff --git a/src/Core/Input/IHandleInput.cs b/src/Core/Input/IHandleInput.cs index 5326f1e..3e9b592 100644 --- a/src/Core/Input/IHandleInput.cs +++ b/src/Core/Input/IHandleInput.cs @@ -14,7 +14,7 @@ namespace UnityExplorer.Core.Input bool GetMouseButtonDown(int btn); bool GetMouseButton(int btn); - BaseInputModule UIModule { get; } + BaseInputModule UIInputModule { get; } void AddUIInputModule(); void ActivateModule(); diff --git a/src/Core/Input/InputManager.cs b/src/Core/Input/InputManager.cs index c0c4f13..c76b4bb 100644 --- a/src/Core/Input/InputManager.cs +++ b/src/Core/Input/InputManager.cs @@ -2,6 +2,7 @@ using System.Diagnostics.CodeAnalysis; using UnityEngine; using UnityEngine.EventSystems; +using UnityExplorer.UI; namespace UnityExplorer.Core.Input { @@ -16,37 +17,42 @@ namespace UnityExplorer.Core.Input { public static InputType CurrentType { get; private set; } - private static IHandleInput m_inputModule; + private static IHandleInput m_inputHandler; - public static Vector3 MousePosition => m_inputModule.MousePosition; + public static Vector3 MousePosition => m_inputHandler.MousePosition; public static bool GetKeyDown(KeyCode key) { if (key == KeyCode.None) return false; - return m_inputModule.GetKeyDown(key); + return m_inputHandler.GetKeyDown(key); } public static bool GetKey(KeyCode key) { if (key == KeyCode.None) return false; - return m_inputModule.GetKey(key); + return m_inputHandler.GetKey(key); } - public static bool GetMouseButtonDown(int btn) => m_inputModule.GetMouseButtonDown(btn); - public static bool GetMouseButton(int btn) => m_inputModule.GetMouseButton(btn); + public static bool GetMouseButtonDown(int btn) => m_inputHandler.GetMouseButtonDown(btn); + public static bool GetMouseButton(int btn) => m_inputHandler.GetMouseButton(btn); - public static BaseInputModule UIInput => m_inputModule.UIModule; + public static BaseInputModule UIInput => m_inputHandler.UIInputModule; - public static Vector2 MouseScrollDelta => m_inputModule.MouseScrollDelta; - - public static void ActivateUIModule() => m_inputModule.ActivateModule(); + public static Vector2 MouseScrollDelta => m_inputHandler.MouseScrollDelta; public static void AddUIModule() { - m_inputModule.AddUIInputModule(); - ActivateUIModule(); + m_inputHandler.AddUIInputModule(); + //ActivateUIModule(); + CursorUnlocker.SetEventSystem(); + } + + public static void ActivateUIModule() + { + UIManager.EventSys.m_CurrentInputModule = UIInput; + m_inputHandler.ActivateModule(); } public static void Init() @@ -65,7 +71,7 @@ namespace UnityExplorer.Core.Input { try { - m_inputModule = new LegacyInput(); + m_inputHandler = new LegacyInput(); CurrentType = InputType.Legacy; // make sure its working @@ -84,7 +90,7 @@ namespace UnityExplorer.Core.Input { try { - m_inputModule = new InputSystem(); + m_inputHandler = new InputSystem(); CurrentType = InputType.InputSystem; ExplorerCore.Log("Initialized new InputSystem support."); return; @@ -96,7 +102,7 @@ namespace UnityExplorer.Core.Input } ExplorerCore.LogWarning("Could not find any Input Module Type!"); - m_inputModule = new NoInput(); + m_inputHandler = new NoInput(); CurrentType = InputType.None; } } diff --git a/src/Core/Input/InputSystem.cs b/src/Core/Input/InputSystem.cs index 3240bd8..85c3cc5 100644 --- a/src/Core/Input/InputSystem.cs +++ b/src/Core/Input/InputSystem.cs @@ -201,7 +201,7 @@ namespace UnityExplorer.Core.Input ?? (m_tUIInputModule = ReflectionUtility.GetTypeByName("UnityEngine.InputSystem.UI.InputSystemUIInputModule")); internal Type m_tUIInputModule; - public BaseInputModule UIModule => m_newInputModule; + public BaseInputModule UIInputModule => m_newInputModule; internal BaseInputModule m_newInputModule; public void AddUIInputModule() @@ -239,6 +239,9 @@ namespace UnityExplorer.Core.Input private void CreateAction(object map, string actionName, string[] bindings, string propertyName) { + var disable = map.GetType().GetMethod("Disable"); + disable.Invoke(map, ArgumentUtility.EmptyArgs); + var inputActionType = ReflectionUtility.GetTypeByName("UnityEngine.InputSystem.InputAction"); var addAction = inputExtensions.GetMethod("AddAction"); var action = addAction.Invoke(null, new object[] { map, actionName, default, null, null, null, null, null }) @@ -262,8 +265,16 @@ namespace UnityExplorer.Core.Input public void ActivateModule() { - m_newInputModule.ActivateModule(); - UI_Enable.Invoke(UI_ActionMap, ArgumentUtility.EmptyArgs); + try + { + m_newInputModule.m_EventSystem = UIManager.EventSys; + m_newInputModule.ActivateModule(); + UI_Enable.Invoke(UI_ActionMap, ArgumentUtility.EmptyArgs); + } + catch (Exception ex) + { + ExplorerCore.LogWarning("Exception enabling InputSystem UI Input Module: " + ex); + } } } } \ No newline at end of file diff --git a/src/Core/Input/LegacyInput.cs b/src/Core/Input/LegacyInput.cs index c662fa2..909e802 100644 --- a/src/Core/Input/LegacyInput.cs +++ b/src/Core/Input/LegacyInput.cs @@ -42,17 +42,25 @@ namespace UnityExplorer.Core.Input // UI Input module - public BaseInputModule UIModule => m_inputModule; + public BaseInputModule UIInputModule => m_inputModule; internal StandaloneInputModule m_inputModule; public void AddUIInputModule() { m_inputModule = UIManager.CanvasRoot.gameObject.AddComponent(); + m_inputModule.m_EventSystem = UIManager.EventSys; } public void ActivateModule() { - m_inputModule.ActivateModule(); + try + { + m_inputModule.ActivateModule(); + } + catch (Exception ex) + { + ExplorerCore.LogWarning($"Exception enabling StandaloneInputModule: {ex}"); + } } } } \ No newline at end of file diff --git a/src/Core/Input/NoInput.cs b/src/Core/Input/NoInput.cs index f56140f..c156cc3 100644 --- a/src/Core/Input/NoInput.cs +++ b/src/Core/Input/NoInput.cs @@ -16,7 +16,7 @@ namespace UnityExplorer.Core.Input public bool GetMouseButton(int btn) => false; public bool GetMouseButtonDown(int btn) => false; - public BaseInputModule UIModule => null; + public BaseInputModule UIInputModule => null; public void ActivateModule() { } public void AddUIInputModule() { } }