From 09dae6f1d3bf5f386ebdf56a1663950ee0df8e34 Mon Sep 17 00:00:00 2001 From: Sinai Date: Mon, 5 Apr 2021 16:28:30 +1000 Subject: [PATCH] Add proper support for InputSystem --- src/Core/Input/IHandleInput.cs | 2 - src/Core/Input/InputManager.cs | 3 +- src/Core/Input/InputSystem.cs | 91 ++++++++++++++------ src/Core/Input/LegacyInput.cs | 7 -- src/Core/Input/NoInput.cs | 1 - src/ExplorerCore.cs | 2 +- src/UI/InteractiveValues/InteractiveValue.cs | 4 +- src/UI/UIManager.cs | 8 +- 8 files changed, 74 insertions(+), 44 deletions(-) diff --git a/src/Core/Input/IHandleInput.cs b/src/Core/Input/IHandleInput.cs index f51eb26..279e929 100644 --- a/src/Core/Input/IHandleInput.cs +++ b/src/Core/Input/IHandleInput.cs @@ -15,8 +15,6 @@ namespace UnityExplorer.Core.Input BaseInputModule UIModule { get; } - PointerEventData InputPointerEvent { get; } - void AddUIInputModule(); void ActivateModule(); } diff --git a/src/Core/Input/InputManager.cs b/src/Core/Input/InputManager.cs index fcec938..e905312 100644 --- a/src/Core/Input/InputManager.cs +++ b/src/Core/Input/InputManager.cs @@ -27,7 +27,6 @@ namespace UnityExplorer.Core.Input public static bool GetMouseButton(int btn) => m_inputModule.GetMouseButton(btn); public static BaseInputModule UIInput => m_inputModule.UIModule; - public static PointerEventData InputPointerEvent => m_inputModule.InputPointerEvent; public static void ActivateUIModule() => m_inputModule.ActivateModule(); @@ -52,7 +51,7 @@ namespace UnityExplorer.Core.Input if (m_inputModule == null) { - ExplorerCore.LogWarning("Could not find any Input module!"); + ExplorerCore.LogWarning("Could not find any Input Module Type!"); m_inputModule = new NoInput(); CurrentType = InputType.None; } diff --git a/src/Core/Input/InputSystem.cs b/src/Core/Input/InputSystem.cs index 27711ec..51c1e31 100644 --- a/src/Core/Input/InputSystem.cs +++ b/src/Core/Input/InputSystem.cs @@ -5,6 +5,10 @@ using UnityEngine; using UnityEngine.EventSystems; using UnityExplorer.UI; using System.Collections.Generic; +using UnityExplorer.UI.Inspectors; +#if CPP +using UnhollowerRuntimeLib; +#endif namespace UnityExplorer.Core.Input { @@ -131,41 +135,74 @@ namespace UnityExplorer.Core.Input // UI Input - //public Type TInputSystemUIInputModule - // => m_tUIInputModule - // ?? (m_tUIInputModule = ReflectionHelpers.GetTypeByName("UnityEngine.InputSystem.UI.InputSystemUIInputModule")); - //internal Type m_tUIInputModule; + public Type TInputSystemUIInputModule + => m_tUIInputModule + ?? (m_tUIInputModule = ReflectionUtility.GetTypeByName("UnityEngine.InputSystem.UI.InputSystemUIInputModule")); + internal Type m_tUIInputModule; - public BaseInputModule UIModule => null; // m_newInputModule; - //internal BaseInputModule m_newInputModule; - - public PointerEventData InputPointerEvent => null; + public BaseInputModule UIModule => m_newInputModule; + internal BaseInputModule m_newInputModule; public void AddUIInputModule() { -// if (TInputSystemUIInputModule != null) -// { -//#if CPP -// // m_newInputModule = UIManager.CanvasRoot.AddComponent(Il2CppType.From(TInputSystemUIInputModule)).TryCast(); -//#else -// m_newInputModule = (BaseInputModule)UIManager.CanvasRoot.AddComponent(TInputSystemUIInputModule); -//#endif -// } -// else -// { -// ExplorerCore.LogWarning("New input system: Could not find type by name 'UnityEngine.InputSystem.UI.InputSystemUIInputModule'"); -// } + if (TInputSystemUIInputModule == null) + { + ExplorerCore.LogWarning("Unable to find UI Input Module Type, Input will not work!"); + return; + } + + var assetType = ReflectionUtility.GetTypeByName("UnityEngine.InputSystem.InputActionAsset"); +#if CPP + m_newInputModule = UIManager.CanvasRoot.AddComponent(Il2CppType.From(TInputSystemUIInputModule)).TryCast(); + var asset = ScriptableObject.CreateInstance(Il2CppType.From(assetType)); +#else + m_newInputModule = (BaseInputModule)UIManager.CanvasRoot.AddComponent(TInputSystemUIInputModule); + var asset = ScriptableObject.CreateInstance(assetType); +#endif + inputExtensions = ReflectionUtility.GetTypeByName("UnityEngine.InputSystem.InputActionSetupExtensions"); + + var addMap = inputExtensions.GetMethod("AddActionMap", new Type[] { assetType, typeof(string) }); + var map = addMap.Invoke(null, new object[] { asset, "UI" }); + + CreateAction(map, "point", new[] { "/position" }, "point"); + CreateAction(map, "click", new[] { "/leftButton" }, "leftClick"); + CreateAction(map, "rightClick", new[] { "/rightButton" }, "rightClick"); + CreateAction(map, "scrollWheel", new[] { "/scroll" }, "scrollWheel"); + + UI_Enable = map.GetType().GetMethod("Enable"); + UI_Enable.Invoke(map, new object[0]); + UI_ActionMap = map; + } + + private Type inputExtensions; + private object UI_ActionMap; + private MethodInfo UI_Enable; + + private void CreateAction(object map, string actionName, string[] bindings, string propertyName) + { + var addAction = inputExtensions.GetMethod("AddAction"); + var pointAction = addAction.Invoke(null, new object[] { map, actionName, default, null, null, null, null, null }); + + var inputActionType = pointAction.GetType(); + var addBinding = inputExtensions.GetMethod("AddBinding", + new Type[] { inputActionType, typeof(string), typeof(string), typeof(string), typeof(string) }); + + foreach (string binding in bindings) + addBinding.Invoke(null, new object[] { pointAction, binding, null, null, null }); + + var inputRef = ReflectionUtility.GetTypeByName("UnityEngine.InputSystem.InputActionReference") + .GetMethod("Create") + .Invoke(null, new object[] { pointAction }); + + TInputSystemUIInputModule + .GetProperty(propertyName) + .SetValue(m_newInputModule, inputRef, null); } public void ActivateModule() { -//#if CPP -// // m_newInputModule.ActivateModule(); -//#else -// m_newInputModule.ActivateModule(); -//#endif - - + m_newInputModule.ActivateModule(); + UI_Enable.Invoke(UI_ActionMap, new object[0]); } } } diff --git a/src/Core/Input/LegacyInput.cs b/src/Core/Input/LegacyInput.cs index a3c45eb..bdcebd4 100644 --- a/src/Core/Input/LegacyInput.cs +++ b/src/Core/Input/LegacyInput.cs @@ -44,13 +44,6 @@ namespace UnityExplorer.Core.Input public BaseInputModule UIModule => m_inputModule; internal StandaloneInputModule m_inputModule; - public PointerEventData InputPointerEvent => -#if CPP - m_inputModule.m_InputPointerEvent; -#else - null; -#endif - public void AddUIInputModule() { m_inputModule = UIManager.CanvasRoot.gameObject.AddComponent(); diff --git a/src/Core/Input/NoInput.cs b/src/Core/Input/NoInput.cs index 0ce659c..3fcf8ca 100644 --- a/src/Core/Input/NoInput.cs +++ b/src/Core/Input/NoInput.cs @@ -16,7 +16,6 @@ namespace UnityExplorer.Core.Input public bool GetMouseButtonDown(int btn) => false; public BaseInputModule UIModule => null; - public PointerEventData InputPointerEvent => null; public void ActivateModule() { } public void AddUIInputModule() { } } diff --git a/src/ExplorerCore.cs b/src/ExplorerCore.cs index 3f050dc..9d769b9 100644 --- a/src/ExplorerCore.cs +++ b/src/ExplorerCore.cs @@ -13,7 +13,7 @@ namespace UnityExplorer public class ExplorerCore { public const string NAME = "UnityExplorer"; - public const string VERSION = "3.3.6"; + public const string VERSION = "3.3.7"; public const string AUTHOR = "Sinai"; public const string GUID = "com.sinai.unityexplorer"; diff --git a/src/UI/InteractiveValues/InteractiveValue.cs b/src/UI/InteractiveValues/InteractiveValue.cs index a54fb70..660ee0d 100644 --- a/src/UI/InteractiveValues/InteractiveValue.cs +++ b/src/UI/InteractiveValues/InteractiveValue.cs @@ -31,8 +31,8 @@ namespace UnityExplorer.UI.InteractiveValues // arbitrarily check some types, fastest methods first. if (type == typeof(bool)) return typeof(InteractiveBool); - // if type is primitive then it must be a number if its not a bool - else if (type.IsPrimitive) + // if type is primitive then it must be a number if its not a bool. Also check for decimal. + else if (type.IsPrimitive || type == typeof(decimal)) return typeof(InteractiveNumber); // check for strings else if (type == typeof(string)) diff --git a/src/UI/UIManager.cs b/src/UI/UIManager.cs index 0f76eed..48ccb37 100644 --- a/src/UI/UIManager.cs +++ b/src/UI/UIManager.cs @@ -140,6 +140,8 @@ namespace UnityExplorer.UI ExplorerCore.Log("This game does not ship with the 'UI/Default' shader, using manual Default Shader..."); Graphic.defaultGraphicMaterial.shader = BackupShader; } + else + BackupShader = Graphic.defaultGraphicMaterial.shader; ConsoleFont = bundle.LoadAsset("CONSOLA"); @@ -148,9 +150,11 @@ namespace UnityExplorer.UI private static AssetBundle LoadExplorerUi(string id) { - var data = ReadFully(typeof(ExplorerCore) + var stream = typeof(ExplorerCore) .Assembly - .GetManifestResourceStream($"UnityExplorer.Resources.explorerui.{id}.bundle")); + .GetManifestResourceStream($"UnityExplorer.Resources.explorerui.{id}.bundle"); + + var data = ReadFully(stream); return AssetBundle.LoadFromMemory(data); }