Skip to content

Commit 27d2430

Browse files
committed
keyboard position and size handled by a global proxy instead of sendmessage to a dummy gameobject
1 parent 8d97822 commit 27d2430

9 files changed

Lines changed: 71 additions & 88 deletions

Example/Assets/Plugins/NativeEditBox/Android/NativeEditBox.java

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,6 @@
2525

2626
@SuppressWarnings("unused")
2727
public class NativeEditBox {
28-
private final String GLOBAL_LISTENER_NAME = "NativeEditBoxGlobalListener_1000";
29-
private final String GLOBAL_METHOD_KEYBOARD_CHANGE = "FromNative_KeyboardChange";
30-
3128
private enum TextAnchor
3229
{
3330
UpperLeft,
@@ -74,6 +71,7 @@ private enum TouchScreenKeyboardType
7471
private static ViewTreeObserver.OnGlobalLayoutListener sGlobalListener = null;
7572
private static FrameLayout sLayout = null;
7673

74+
private static NativeEditBoxGlobalProxy globalProxy = null;
7775
private NativeEditBoxInstanceProxy instanceProxy = null;
7876

7977
private EditText mEditBox = null;
@@ -82,9 +80,10 @@ private enum TouchScreenKeyboardType
8280
private InputType currentInputType = InputType.Standard;
8381
private TouchScreenKeyboardType currentKeyboardType = TouchScreenKeyboardType.Default;
8482

85-
public NativeEditBox(NativeEditBoxInstanceProxy callback)
83+
public NativeEditBox(NativeEditBoxGlobalProxy global, NativeEditBoxInstanceProxy instance)
8684
{
87-
instanceProxy = callback;
85+
globalProxy = global;
86+
instanceProxy = instance;
8887
}
8988

9089
@SuppressWarnings("unused")
@@ -207,10 +206,12 @@ public void onGlobalLayout() {
207206
if(kbHeight > 2)
208207
{
209208
//Showing
210-
UnityPlayer.UnitySendMessage(GLOBAL_LISTENER_NAME, GLOBAL_METHOD_KEYBOARD_CHANGE, String.format(Locale.ENGLISH, "%d|%d|%d|%d", 0, screen.x, r.bottom, kbHeight));
209+
if(globalProxy != null)
210+
globalProxy.OnJavaKeyboardChange(0, screen.x, r.bottom, kbHeight);
211211
}else{
212212
//Not showing
213-
UnityPlayer.UnitySendMessage(GLOBAL_LISTENER_NAME, GLOBAL_METHOD_KEYBOARD_CHANGE, "");
213+
if(globalProxy != null)
214+
globalProxy.OnJavaKeyboardChange(0, 0, 0, 0);
214215
}
215216
}
216217
};
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package com.unityextensions.nativeeditbox;
2+
3+
public interface NativeEditBoxGlobalProxy
4+
{
5+
void OnJavaKeyboardChange(float x, float y, float width, float height);
6+
}

Example/Assets/Plugins/NativeEditBox/Android/NativeEditBoxGlobalProxy.java.meta

Lines changed: 32 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Example/Assets/Plugins/NativeEditBox/NativeEditBox.android.cs

Lines changed: 17 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,18 @@
88

99
public partial class NativeEditBox : IPointerClickHandler
1010
{
11+
class GlobalProxy : AndroidJavaProxy
12+
{
13+
public GlobalProxy() : base("com.unityextensions.nativeeditbox.NativeEditBoxGlobalProxy")
14+
{
15+
}
16+
17+
void OnJavaKeyboardChange(float x, float y, float width, float height)
18+
{
19+
NativeEditBox.keyboard = new Rect(x, y, width, height);
20+
}
21+
}
22+
1123
class InstanceProxy : AndroidJavaProxy
1224
{
1325
NativeEditBox owner = null;
@@ -60,10 +72,7 @@ void OnJavaTapOutside()
6072
}
6173
}
6274

63-
const string GlobalListenerName = "NativeEditBoxGlobalListener_1000";
64-
65-
static GameObject globalListener = null;
66-
75+
static GlobalProxy globalProxy = null;
6776
InstanceProxy instanceProxy = null;
6877

6978
AndroidJavaObject editBox = default;
@@ -118,26 +127,12 @@ public string text
118127

119128
void AwakeNative()
120129
{
121-
CreateGlobalListener();
122-
123130
inputField.interactable = false;
124131

125132
if (!switchBetweenNativeAndUnity)
126133
StartCoroutine(CreateNow(false));
127134
}
128135

129-
void CreateGlobalListener()
130-
{
131-
if (globalListener != null)
132-
return;
133-
134-
globalListener = new GameObject();
135-
globalListener.name = GlobalListenerName;
136-
GameObject.DontDestroyOnLoad(globalListener);
137-
138-
globalListener.AddComponent<NativeEditBoxGlobalListener>();
139-
}
140-
141136
bool ShowText
142137
{
143138
set
@@ -233,9 +228,12 @@ void SetupInputField()
233228
_ => TextAnchor.TextAnchorUpperLeft
234229
};
235230

231+
if (globalProxy == null)
232+
globalProxy = new GlobalProxy();
233+
236234
instanceProxy = new InstanceProxy(this);
237235

238-
editBox = new AndroidJavaObject("com.unityextensions.nativeeditbox.NativeEditBox", instanceProxy);
236+
editBox = new AndroidJavaObject("com.unityextensions.nativeeditbox.NativeEditBox", globalProxy, instanceProxy);
239237
editBox.Call("Init", inputField.lineType != TMP_InputField.LineType.SingleLine);
240238

241239
UpdatePlacementNow();
@@ -263,12 +261,6 @@ IEnumerator CoSelectAll()
263261
SelectRange(0, inputField.text.Length);
264262
SelectRange(0, inputField.text.Length);
265263
}
266-
267-
#region Public Methods
268-
269-
public static Rect KeyboardArea => NativeEditBoxGlobalListener.KeyboardArea;
270-
271-
#endregion
272264
}
273265

274266
#endif

Example/Assets/Plugins/NativeEditBox/NativeEditBox.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,4 +135,12 @@ static NativeEditBox FindNativeEditBoxBy(int instanceId)
135135
return i;
136136
return null;
137137
}
138+
139+
#region Keyboard Position and Size
140+
141+
static Rect keyboard = default(Rect);
142+
143+
public static Rect KeyboardArea => keyboard;
144+
145+
#endregion
138146
}

Example/Assets/Plugins/NativeEditBox/NativeEditBox.editor.cs

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -89,12 +89,6 @@ void UpdateNative()
8989
}
9090

9191
#endregion
92-
93-
#region Public Methods
94-
95-
public static Rect KeyboardArea => new Rect();
96-
97-
#endregion
9892
}
9993

10094
#endif

Example/Assets/Plugins/NativeEditBox/NativeEditBox.ios.cs

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -312,8 +312,6 @@ static void DelegateTapOutside(int instanceId)
312312

313313
#region GLOBAL CALLBACK
314314

315-
static Rect keyboard = default(Rect);
316-
317315
delegate void DelegateKeyboardChanged(float x, float y, float width, float height);
318316

319317
[MonoPInvokeCallback(typeof(DelegateKeyboardChanged))]
@@ -323,12 +321,6 @@ static void delegateKeyboardChanged(float x, float y, float width, float height)
323321
}
324322

325323
#endregion
326-
327-
#region Public Methods
328-
329-
public static Rect KeyboardArea => keyboard;
330-
331-
#endregion
332324
}
333325

334326
#endif

Example/Assets/Plugins/NativeEditBox/NativeEditBoxGlobalListener.cs

Lines changed: 0 additions & 31 deletions
This file was deleted.

Example/Assets/Plugins/NativeEditBox/NativeEditBoxGlobalListener.cs.meta

Lines changed: 0 additions & 11 deletions
This file was deleted.

0 commit comments

Comments
 (0)