forked from dantasulisses/WebMobileInputFix
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathKeyboardController.cs
More file actions
208 lines (187 loc) · 6.36 KB
/
Copy pathKeyboardController.cs
File metadata and controls
208 lines (187 loc) · 6.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
using System.Runtime.InteropServices;
namespace WebGLKeyboard
{
/// <summary>
/// Controls the flow of opening the keyboard and adding the necessary components to input fields as scenes load
/// </summary>
public class KeyboardController : MonoBehaviour
{
public bool isKeyboardOpen = false;
public UnityEngine.UI.InputField currentNativeInput;
#if TMP_PRESENT
public TMPro.TMP_InputField currentTmproInput;
#endif
[DllImport("__Internal")]
private static extern void OpenInputKeyboard(string str);
[DllImport("__Internal")]
private static extern void CloseInputKeyboard();
//Just adds these functions references to avoid stripping
[DllImport("__Internal")]
private static extern void FixInputOnBlur();
[DllImport("__Internal")]
private static extern void FixInputUpdate();
void OnEnable()
{
SceneManager.sceneLoaded += OnSceneLoaded;
}
void OnDisable()
{
SceneManager.sceneLoaded -= OnSceneLoaded;
}
private void Start()
{
PadronizeObjectName();
//Calls the scene loaded in the first scene manually because this component will initialize after the scene load
OnSceneLoaded(SceneManager.GetActiveScene(), LoadSceneMode.Single);
DontDestroyOnLoad(gameObject);
}
/// <summary>
/// Changes this object name and parent to guarantee that it will be accessible from the outside javascript functions
/// </summary>
private void PadronizeObjectName()
{
gameObject.name = "_WebGLKeyboard";
gameObject.transform.SetParent(null);
}
/// <summary>
/// Callback when scene loads to add the DetectFocus component to every input field
/// </summary>
/// <param name="scene"></param>
/// <param name="mode"></param>
void OnSceneLoaded(Scene scene, LoadSceneMode mode)
{
List<UnityEngine.UI.InputField> nativeInputs = FindObjectsOfTypeInScene<UnityEngine.UI.InputField>(scene);
for (int x = 0; x < nativeInputs.Count; x++)
{
DetectInputFocus detect = nativeInputs[x].gameObject.AddComponent<DetectInputFocus>();
detect.Initialize(this);
}
#if TMP_PRESENT
List<TMPro.TMP_InputField> tmProInputs = FindObjectsOfTypeInScene<TMPro.TMP_InputField>(scene);
for (int x = 0; x < tmProInputs.Count; x++)
{
DetectInputFocus detect = tmProInputs[x].gameObject.AddComponent<DetectInputFocus>();
detect.Initialize(this);
}
#endif
}
/// <summary>
/// Call the external javascript function to trigger the keyboard and link to the input field
/// </summary>
/// <param name="input"></param>
public void FocusInput(UnityEngine.UI.InputField input)
{
isKeyboardOpen = true;
currentNativeInput = input;
#if UNITY_EDITOR
Debug.Log("OpenInputKeyboard");
#else
OpenInputKeyboard(input.text);
#endif
#if !UNITY_EDITOR && UNITY_WEBGL
UnityEngine.WebGLInput.captureAllKeyboardInput = false;
#endif
}
#if TMP_PRESENT
/// <summary>
/// Call the external javascript function to trigger the keyboard and link to the input field
/// </summary>
/// <param name="input"></param>
public void FocusInput(TMPro.TMP_InputField input)
{
isKeyboardOpen = true;
currentTmproInput = input;
#if UNITY_EDITOR
Debug.Log("OpenInputKeyboard");
#else
OpenInputKeyboard(input.text);
#endif
#if !UNITY_EDITOR && UNITY_WEBGL
UnityEngine.WebGLInput.captureAllKeyboardInput = false;
#endif
}
#endif
/// <summary>
/// Forces the keyboard to close and unfocus
/// </summary>
public void ForceClose()
{
#if UNITY_EDITOR
Debug.Log("CloseInputKeyboard");
#else
CloseInputKeyboard();
#endif
}
/// <summary>
/// Clear the link to the open keyboard
/// </summary>
public void LoseFocus()
{
if (isKeyboardOpen == false)
return;
isKeyboardOpen = false;
if (currentNativeInput != null)
{
currentNativeInput.DeactivateInputField();
currentNativeInput = null;
}
#if TMP_PRESENT
if (currentTmproInput != null)
{
currentTmproInput.DeactivateInputField();
currentTmproInput = null;
}
#endif
#if !UNITY_EDITOR && UNITY_WEBGL
UnityEngine.WebGLInput.captureAllKeyboardInput = true;
#endif
}
/// <summary>
/// Receives the string inputed in the keyboard
/// </summary>
/// <param name="value"></param>
public void ReceiveInputChange(string value)
{
Debug.Log(value);
//This shouldn't happen
if (isKeyboardOpen == false)
return;
//Applies the new string to the input field
if (currentNativeInput != null)
{
currentNativeInput.text = value;
}
#if TMP_PRESENT
if (currentTmproInput != null)
{
currentTmproInput.text = value;
}
#endif
}
public void InputOnBlurFix() { FixInputOnBlur(); }
public void InputUpdateFix() { FixInputUpdate(); }
/// <summary>
/// Returns all objects of a type in a loaded scene
/// </summary>
/// <typeparam name="T"></typeparam>
/// <returns></returns>
public List<T> FindObjectsOfTypeInScene<T>(Scene scene)
{
List<T> results = new List<T>();
if (scene.isLoaded)
{
var allGameObjects = scene.GetRootGameObjects();
for (int x = 0; x < allGameObjects.Length; x++)
{
var go = allGameObjects[x];
results.AddRange(go.GetComponentsInChildren<T>(true));
}
}
return results;
}
}
}