|
5 | 5 | using UnityEngine.EventSystems;
|
6 | 6 | using UnityExplorer.UI;
|
7 | 7 | using System.Collections.Generic;
|
| 8 | +using UnityExplorer.UI.Inspectors; |
| 9 | +#if CPP |
| 10 | +using UnhollowerRuntimeLib; |
| 11 | +#endif |
8 | 12 |
|
9 | 13 | namespace UnityExplorer.Core.Input
|
10 | 14 | {
|
@@ -131,41 +135,74 @@ public bool GetMouseButton(int btn)
|
131 | 135 |
|
132 | 136 | // UI Input
|
133 | 137 |
|
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; |
138 | 142 |
|
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; |
143 | 145 |
|
144 | 146 | public void AddUIInputModule()
|
145 | 147 | {
|
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; |
158 | 175 | }
|
159 | 176 |
|
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) |
161 | 182 | {
|
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 }); |
167 | 185 |
|
| 186 | + var inputActionType = pointAction.GetType(); |
| 187 | + var addBinding = inputExtensions.GetMethod("AddBinding", |
| 188 | + new Type[] { inputActionType, typeof(string), typeof(string), typeof(string), typeof(string) }); |
168 | 189 |
|
| 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]); |
169 | 206 | }
|
170 | 207 | }
|
171 | 208 | }
|
0 commit comments