Skip to content

Commit d50244a

Browse files
committed
use android proxy instead of sendmessage
1 parent 614ada4 commit d50244a

6 files changed

Lines changed: 124 additions & 66 deletions

File tree

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

Lines changed: 13 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,6 @@
2525

2626
@SuppressWarnings("unused")
2727
public class NativeEditBox {
28-
private final String METHOD_TEXT_CHANGED = "Android_TextChanged";
29-
private final String METHOD_TAP_OUTSIDE = "Android_TapOutside";
30-
private final String METHOD_DID_END = "Android_DidEnd";
31-
private final String METHOD_SUBMIT_PRESSED = "Android_SubmitPressed";
32-
private final String METHOD_GOT_FOCUS = "Android_GotFocus";
33-
3428
private final String GLOBAL_LISTENER_NAME = "NativeEditBoxGlobalListener_1000";
3529
private final String GLOBAL_METHOD_KEYBOARD_CHANGE = "FromNative_KeyboardChange";
3630

@@ -80,17 +74,22 @@ private enum TouchScreenKeyboardType
8074
private static ViewTreeObserver.OnGlobalLayoutListener sGlobalListener = null;
8175
private static FrameLayout sLayout = null;
8276

83-
private String mGameObjectName;
77+
private NativeEditBoxInstanceProxy instanceProxy = null;
78+
8479
private EditText mEditBox = null;
8580

8681
private boolean currentMultiline = false;
8782
private InputType currentInputType = InputType.Standard;
8883
private TouchScreenKeyboardType currentKeyboardType = TouchScreenKeyboardType.Default;
8984

85+
public NativeEditBox(NativeEditBoxInstanceProxy callback)
86+
{
87+
instanceProxy = callback;
88+
}
89+
9090
@SuppressWarnings("unused")
91-
public void Init(final String gameObjectName, final boolean multiline)
91+
public void Init(final boolean multiline)
9292
{
93-
this.mGameObjectName = gameObjectName;
9493
this.currentMultiline = multiline;
9594

9695
final Activity activity = UnityPlayer.currentActivity;
@@ -149,13 +148,13 @@ public void onFocusChange(View v, boolean hasFocus) {
149148

150149
eb.showKeyboard(false);
151150

152-
eb.sendToUnity(METHOD_DID_END, txt);
153-
eb.sendToUnity(METHOD_TAP_OUTSIDE, "");
151+
instanceProxy.OnJavaDidEnd(txt);
152+
instanceProxy.OnJavaTapOutside();
154153
}else
155154
{
156155
sLayout.setClickable(true);
157156

158-
eb.sendToUnity(METHOD_GOT_FOCUS, "");
157+
instanceProxy.OnJavaGotFocus();
159158
}
160159
}
161160
});
@@ -173,7 +172,7 @@ public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
173172

174173
@Override
175174
public void afterTextChanged(Editable editable) {
176-
sendToUnity(METHOD_TEXT_CHANGED, editable.toString());
175+
instanceProxy.OnJavaTextChanged(editable.toString());
177176
}
178177
});
179178

@@ -182,7 +181,7 @@ public void afterTextChanged(Editable editable) {
182181
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
183182
if(actionId >= EditorInfo.IME_ACTION_NONE && actionId <= EditorInfo.IME_ACTION_PREVIOUS)
184183
{
185-
sendToUnity(METHOD_SUBMIT_PRESSED, v.getText().toString());
184+
instanceProxy.OnJavaSubmitPressed(v.getText().toString());
186185
return true;
187186
}
188187
return false;
@@ -222,8 +221,6 @@ public void onGlobalLayout() {
222221
@SuppressWarnings("unused")
223222
public void Destroy()
224223
{
225-
mGameObjectName = null;
226-
227224
final Activity activity = UnityPlayer.currentActivity;
228225
activity.runOnUiThread(new Runnable() {
229226
@Override
@@ -623,14 +620,6 @@ private void updateInputValues()
623620
mEditBox.setInputType(editInputType);
624621
}
625622

626-
private void sendToUnity(String method, String param)
627-
{
628-
if(mGameObjectName == null)
629-
return;
630-
631-
UnityPlayer.UnitySendMessage(mGameObjectName, method, param);
632-
}
633-
634623
private String getText()
635624
{
636625
if(mEditBox == null)
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package com.unityextensions.nativeeditbox;
2+
3+
public interface NativeEditBoxInstanceProxy
4+
{
5+
void OnJavaTextChanged(String text);
6+
void OnJavaDidEnd(String text);
7+
void OnJavaSubmitPressed(String text);
8+
9+
void OnJavaGotFocus();
10+
void OnJavaTapOutside();
11+
}

Example/Assets/Plugins/NativeEditBox/Android/NativeEditBoxInstanceProxy.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: 60 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,64 @@
88

99
public partial class NativeEditBox : IPointerClickHandler
1010
{
11+
class InstanceProxy : AndroidJavaProxy
12+
{
13+
NativeEditBox owner = null;
14+
15+
public InstanceProxy(NativeEditBox owner) : base("com.unityextensions.nativeeditbox.NativeEditBoxInstanceProxy")
16+
{
17+
this.owner = owner;
18+
}
19+
20+
void OnJavaTextChanged(string text)
21+
{
22+
owner.inputField.text = text;
23+
24+
owner.OnTextChanged?.Invoke(text);
25+
}
26+
27+
void OnJavaDidEnd(string text)
28+
{
29+
owner.inputField.text = text;
30+
31+
if (owner.switchBetweenNativeAndUnity)
32+
owner.DestroyNow();
33+
34+
owner.OnDidEnd?.Invoke();
35+
}
36+
37+
void OnJavaSubmitPressed(string text)
38+
{
39+
owner.inputField.text = text;
40+
41+
owner.OnSubmit?.Invoke(text);
42+
43+
Debug.Log("submit pressed " + text);
44+
}
45+
46+
void OnJavaGotFocus()
47+
{
48+
owner.OnGotFocus?.Invoke();
49+
50+
if (owner.inputField.onFocusSelectAll)
51+
owner.StartCoroutine(owner.CoSelectAll());
52+
}
53+
54+
void OnJavaTapOutside()
55+
{
56+
if (owner.switchBetweenNativeAndUnity)
57+
owner.DestroyNow();
58+
59+
owner.OnTapOutside?.Invoke();
60+
}
61+
}
62+
1163
const string GlobalListenerName = "NativeEditBoxGlobalListener_1000";
1264

1365
static GameObject globalListener = null;
1466

67+
InstanceProxy instanceProxy = null;
68+
1569
AndroidJavaObject editBox = default;
1670

1771
#region Public Methods
@@ -129,6 +183,8 @@ void DestroyNow()
129183
if (editBox == null)
130184
return;
131185

186+
instanceProxy = null;
187+
132188
editBox.Call("Destroy");
133189
editBox = null;
134190

@@ -177,8 +233,10 @@ void SetupInputField()
177233
_ => TextAnchor.TextAnchorUpperLeft
178234
};
179235

180-
editBox = new AndroidJavaObject("com.unityextensions.nativeeditbox.NativeEditBox");
181-
editBox.Call("Init", name, inputField.lineType != TMP_InputField.LineType.SingleLine);
236+
instanceProxy = new InstanceProxy(this);
237+
238+
editBox = new AndroidJavaObject("com.unityextensions.nativeeditbox.NativeEditBox", instanceProxy);
239+
editBox.Call("Init", inputField.lineType != TMP_InputField.LineType.SingleLine);
182240

183241
UpdatePlacementNow();
184242

@@ -193,14 +251,6 @@ void SetupInputField()
193251
editBox.Call("SetText", inputField.text);
194252
}
195253

196-
void Android_GotFocus(string nothing)
197-
{
198-
OnGotFocus?.Invoke();
199-
200-
if (inputField.onFocusSelectAll)
201-
StartCoroutine(CoSelectAll());
202-
}
203-
204254
IEnumerator CoSelectAll()
205255
{
206256
//Looks bad, but works 98% of the times..... Sad.
@@ -214,38 +264,6 @@ IEnumerator CoSelectAll()
214264
SelectRange(0, inputField.text.Length);
215265
}
216266

217-
void Android_TextChanged(string text)
218-
{
219-
inputField.text = text;
220-
221-
OnTextChanged?.Invoke(text);
222-
}
223-
224-
void Android_TapOutside(string nothing)
225-
{
226-
if (switchBetweenNativeAndUnity)
227-
DestroyNow();
228-
229-
OnTapOutside?.Invoke();
230-
}
231-
232-
void Android_DidEnd(string text)
233-
{
234-
inputField.text = text;
235-
236-
if (switchBetweenNativeAndUnity)
237-
DestroyNow();
238-
239-
OnDidEnd?.Invoke();
240-
}
241-
242-
void Android_SubmitPressed(string text)
243-
{
244-
inputField.text = text;
245-
246-
OnSubmit?.Invoke(text);
247-
}
248-
249267
#region Public Methods
250268

251269
public static Rect KeyboardArea => NativeEditBoxGlobalListener.KeyboardArea;

Example/Packages/manifest.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
"dependencies": {
33
"com.unity.collab-proxy": "2.3.1",
44
"com.unity.feature.development": "1.0.1",
5+
"com.unity.mobile.android-logcat": "1.4.1",
56
"com.unity.textmeshpro": "3.0.8",
67
"com.unity.timeline": "1.7.6",
78
"com.unity.ugui": "1.0.0",

Example/Packages/packages-lock.json

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,13 @@
6060
"dependencies": {},
6161
"url": "https://packages.unity.com"
6262
},
63+
"com.unity.mobile.android-logcat": {
64+
"version": "1.4.1",
65+
"depth": 0,
66+
"source": "registry",
67+
"dependencies": {},
68+
"url": "https://packages.unity.com"
69+
},
6370
"com.unity.performance.profile-analyzer": {
6471
"version": "1.2.2",
6572
"depth": 1,

0 commit comments

Comments
 (0)