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

Commit 09dae6f

Browse files
committed
Add proper support for InputSystem
1 parent 6ca117b commit 09dae6f

File tree

8 files changed

+73
-43
lines changed

8 files changed

+73
-43
lines changed

src/Core/Input/IHandleInput.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,6 @@ public interface IHandleInput
1515

1616
BaseInputModule UIModule { get; }
1717

18-
PointerEventData InputPointerEvent { get; }
19-
2018
void AddUIInputModule();
2119
void ActivateModule();
2220
}

src/Core/Input/InputManager.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ public static class InputManager
2727
public static bool GetMouseButton(int btn) => m_inputModule.GetMouseButton(btn);
2828

2929
public static BaseInputModule UIInput => m_inputModule.UIModule;
30-
public static PointerEventData InputPointerEvent => m_inputModule.InputPointerEvent;
3130

3231
public static void ActivateUIModule() => m_inputModule.ActivateModule();
3332

@@ -52,7 +51,7 @@ public static void Init()
5251

5352
if (m_inputModule == null)
5453
{
55-
ExplorerCore.LogWarning("Could not find any Input module!");
54+
ExplorerCore.LogWarning("Could not find any Input Module Type!");
5655
m_inputModule = new NoInput();
5756
CurrentType = InputType.None;
5857
}

src/Core/Input/InputSystem.cs

Lines changed: 63 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@
55
using UnityEngine.EventSystems;
66
using UnityExplorer.UI;
77
using System.Collections.Generic;
8+
using UnityExplorer.UI.Inspectors;
9+
#if CPP
10+
using UnhollowerRuntimeLib;
11+
#endif
812

913
namespace UnityExplorer.Core.Input
1014
{
@@ -131,41 +135,74 @@ public bool GetMouseButton(int btn)
131135

132136
// UI Input
133137

134-
//public Type TInputSystemUIInputModule
135-
// => m_tUIInputModule
136-
// ?? (m_tUIInputModule = ReflectionHelpers.GetTypeByName("UnityEngine.InputSystem.UI.InputSystemUIInputModule"));
137-
//internal Type m_tUIInputModule;
138+
public Type TInputSystemUIInputModule
139+
=> m_tUIInputModule
140+
?? (m_tUIInputModule = ReflectionUtility.GetTypeByName("UnityEngine.InputSystem.UI.InputSystemUIInputModule"));
141+
internal Type m_tUIInputModule;
138142

139-
public BaseInputModule UIModule => null; // m_newInputModule;
140-
//internal BaseInputModule m_newInputModule;
141-
142-
public PointerEventData InputPointerEvent => null;
143+
public BaseInputModule UIModule => m_newInputModule;
144+
internal BaseInputModule m_newInputModule;
143145

144146
public void AddUIInputModule()
145147
{
146-
// if (TInputSystemUIInputModule != null)
147-
// {
148-
//#if CPP
149-
// // m_newInputModule = UIManager.CanvasRoot.AddComponent(Il2CppType.From(TInputSystemUIInputModule)).TryCast<BaseInputModule>();
150-
//#else
151-
// m_newInputModule = (BaseInputModule)UIManager.CanvasRoot.AddComponent(TInputSystemUIInputModule);
152-
//#endif
153-
// }
154-
// else
155-
// {
156-
// ExplorerCore.LogWarning("New input system: Could not find type by name 'UnityEngine.InputSystem.UI.InputSystemUIInputModule'");
157-
// }
148+
if (TInputSystemUIInputModule == null)
149+
{
150+
ExplorerCore.LogWarning("Unable to find UI Input Module Type, Input will not work!");
151+
return;
152+
}
153+
154+
var assetType = ReflectionUtility.GetTypeByName("UnityEngine.InputSystem.InputActionAsset");
155+
#if CPP
156+
m_newInputModule = UIManager.CanvasRoot.AddComponent(Il2CppType.From(TInputSystemUIInputModule)).TryCast<BaseInputModule>();
157+
var asset = ScriptableObject.CreateInstance(Il2CppType.From(assetType));
158+
#else
159+
m_newInputModule = (BaseInputModule)UIManager.CanvasRoot.AddComponent(TInputSystemUIInputModule);
160+
var asset = ScriptableObject.CreateInstance(assetType);
161+
#endif
162+
inputExtensions = ReflectionUtility.GetTypeByName("UnityEngine.InputSystem.InputActionSetupExtensions");
163+
164+
var addMap = inputExtensions.GetMethod("AddActionMap", new Type[] { assetType, typeof(string) });
165+
var map = addMap.Invoke(null, new object[] { asset, "UI" });
166+
167+
CreateAction(map, "point", new[] { "<Mouse>/position" }, "point");
168+
CreateAction(map, "click", new[] { "<Mouse>/leftButton" }, "leftClick");
169+
CreateAction(map, "rightClick", new[] { "<Mouse>/rightButton" }, "rightClick");
170+
CreateAction(map, "scrollWheel", new[] { "<Mouse>/scroll" }, "scrollWheel");
171+
172+
UI_Enable = map.GetType().GetMethod("Enable");
173+
UI_Enable.Invoke(map, new object[0]);
174+
UI_ActionMap = map;
158175
}
159176

160-
public void ActivateModule()
177+
private Type inputExtensions;
178+
private object UI_ActionMap;
179+
private MethodInfo UI_Enable;
180+
181+
private void CreateAction(object map, string actionName, string[] bindings, string propertyName)
161182
{
162-
//#if CPP
163-
// // m_newInputModule.ActivateModule();
164-
//#else
165-
// m_newInputModule.ActivateModule();
166-
//#endif
183+
var addAction = inputExtensions.GetMethod("AddAction");
184+
var pointAction = addAction.Invoke(null, new object[] { map, actionName, default, null, null, null, null, null });
167185

186+
var inputActionType = pointAction.GetType();
187+
var addBinding = inputExtensions.GetMethod("AddBinding",
188+
new Type[] { inputActionType, typeof(string), typeof(string), typeof(string), typeof(string) });
168189

190+
foreach (string binding in bindings)
191+
addBinding.Invoke(null, new object[] { pointAction, binding, null, null, null });
192+
193+
var inputRef = ReflectionUtility.GetTypeByName("UnityEngine.InputSystem.InputActionReference")
194+
.GetMethod("Create")
195+
.Invoke(null, new object[] { pointAction });
196+
197+
TInputSystemUIInputModule
198+
.GetProperty(propertyName)
199+
.SetValue(m_newInputModule, inputRef, null);
200+
}
201+
202+
public void ActivateModule()
203+
{
204+
m_newInputModule.ActivateModule();
205+
UI_Enable.Invoke(UI_ActionMap, new object[0]);
169206
}
170207
}
171208
}

src/Core/Input/LegacyInput.cs

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -44,13 +44,6 @@ public LegacyInput()
4444
public BaseInputModule UIModule => m_inputModule;
4545
internal StandaloneInputModule m_inputModule;
4646

47-
public PointerEventData InputPointerEvent =>
48-
#if CPP
49-
m_inputModule.m_InputPointerEvent;
50-
#else
51-
null;
52-
#endif
53-
5447
public void AddUIInputModule()
5548
{
5649
m_inputModule = UIManager.CanvasRoot.gameObject.AddComponent<StandaloneInputModule>();

src/Core/Input/NoInput.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ public class NoInput : IHandleInput
1616
public bool GetMouseButtonDown(int btn) => false;
1717

1818
public BaseInputModule UIModule => null;
19-
public PointerEventData InputPointerEvent => null;
2019
public void ActivateModule() { }
2120
public void AddUIInputModule() { }
2221
}

src/ExplorerCore.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ namespace UnityExplorer
1313
public class ExplorerCore
1414
{
1515
public const string NAME = "UnityExplorer";
16-
public const string VERSION = "3.3.6";
16+
public const string VERSION = "3.3.7";
1717
public const string AUTHOR = "Sinai";
1818
public const string GUID = "com.sinai.unityexplorer";
1919

src/UI/InteractiveValues/InteractiveValue.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,8 @@ public static Type GetIValueForType(Type type)
3131
// arbitrarily check some types, fastest methods first.
3232
if (type == typeof(bool))
3333
return typeof(InteractiveBool);
34-
// if type is primitive then it must be a number if its not a bool
35-
else if (type.IsPrimitive)
34+
// if type is primitive then it must be a number if its not a bool. Also check for decimal.
35+
else if (type.IsPrimitive || type == typeof(decimal))
3636
return typeof(InteractiveNumber);
3737
// check for strings
3838
else if (type == typeof(string))

src/UI/UIManager.cs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,8 @@ private static void LoadBundle()
140140
ExplorerCore.Log("This game does not ship with the 'UI/Default' shader, using manual Default Shader...");
141141
Graphic.defaultGraphicMaterial.shader = BackupShader;
142142
}
143+
else
144+
BackupShader = Graphic.defaultGraphicMaterial.shader;
143145

144146
ConsoleFont = bundle.LoadAsset<Font>("CONSOLA");
145147

@@ -148,9 +150,11 @@ private static void LoadBundle()
148150

149151
private static AssetBundle LoadExplorerUi(string id)
150152
{
151-
var data = ReadFully(typeof(ExplorerCore)
153+
var stream = typeof(ExplorerCore)
152154
.Assembly
153-
.GetManifestResourceStream($"UnityExplorer.Resources.explorerui.{id}.bundle"));
155+
.GetManifestResourceStream($"UnityExplorer.Resources.explorerui.{id}.bundle");
156+
157+
var data = ReadFully(stream);
154158

155159
return AssetBundle.LoadFromMemory(data);
156160
}

0 commit comments

Comments
 (0)