Every now and then when I type name on the mobile keyboard and click “Done” on the native keyboard, the string remains empty. And this seems to be completely random. On some devices it never fails, and in some other devices it fails 90% of the time. Am I doing something obviously wrong?
public void summonKeyboard()
{
keyboard = TouchScreenKeyboard.Open ("", TouchScreenKeyboardType.NamePhonePad);
if (keyboard != null)
{
//firstNameString = keyboard.text;
StartCoroutine(getFirstName());
}
}
IEnumerator getFirstName()
{
while (!keyboard.done)
{
yield return null;
}
firstNameString = keyboard.text;
}
What version Unity you using, this is very important
1 Like
Version 5.0.1f1
Edit: Actually I am able to reproduce this bug with only 1 line of code. So I made bug report.
Attached repro project. For example Samsung S4 Active has the problem, where Samsung Galaxy Tab 4 works always.
2057827–133940–TouchScreenKeyboardInputField.zip (757 KB)
iivo_k
April 7, 2015, 3:44pm
4
InputField has been broken on Android since at least 4.6.3p3, seems the same way in 5.
Thanks for that! Maybe I have to build my own keyboard afterall
I just received answer to the bug report that they have been able to reproduce the problem and it has been forwarded to developers. So some hope for solution.
Meanwhile here is workaround example attached: It shows red color when ever the error was fixed with the workaround. This is compatible with touchscreen and desktop both. So just change the text colors to black for example so that user only see end result no matter what.
Unity 5.0.1f1
2059383–134092–TouchScreenKeyboardInputField.zip (758 KB)
I hope following code helps you guys before unity team fix the problem.
I successfully tested on my own probject.
public class CustomInputField : InputField
{
private TouchScreenKeyboard _openedKeyboard;
public void OnEndEdit()
{
if (TouchScreenKeyboard.isSupported == true)
{
text = _openedKeyboard.text;
}
}
public void OnValueChange()
{
if (TouchScreenKeyboard.isSupported == true)
{
_openedKeyboard = m_Keyboard;
}
}
}
This is my solution for this issue
Works perfectly for me.
public void OnChangeValue()
{
if(!string.IsNullOrEmpty(inputField.text))
backUpString = inputField.text;
lastBackupStringTime = Time.realtimeSinceStartup;
}
public void OnSubmitMessage()
{
//TODO: remove when unity bug is resolved
if(string.IsNullOrEmpty(inputField.text) && Time.realtimeSinceStartup - lastBackupStringTime < 0.05f)
{
Debug.Log("Value Restored : " + backUpString);
inputField.text = backUpString;
}
if(string.IsNullOrEmpty(inputField.text)) return;
Debug.Log(inputField.text);
}
Hello, same problem here on 5.0.1f1
My fix, inspired by others, just drop this script on your InputField
using UnityEngine;
using UnityEngine.UI;
// TODO: remove this component when unity bug will be resolved
[RequireComponent(typeof(InputField))]
public class InputFieldFix : MonoBehaviour {
private string backUpString = "";
private InputField input;
void Start (){
input = GetComponent<InputField> ();
input.onValueChange.AddListener (OnChangeValue);
}
void OnDestroy(){
input.onValueChange.RemoveAllListeners ();
}
void OnChangeValue (string value){
if (string.IsNullOrEmpty (value) && backUpString.Length > 1) {
input.text = backUpString;
return;
}
backUpString = value;
}
}
Just, not working if you just leave a single char in the input. I don’t care about this detail, but just to let you know
Spoke44:
Hello, same problem here on 5.0.1f1
My fix, inspired by others, just drop this script on your InputField
This seems to prevent the deleting of all text in the field, but when you have multiple input fields they still seem to be static. For instance I have a menu with two of them and when you change one it changes the value (not the actual value in the text field, but the variable it is sending to) for both of them.
Spoke44:
Hello, same problem here on 5.0.1f1
My fix, inspired by others, just drop this script on your InputField
Just, not working if you just leave a single char in the input. I don’t care about this detail, but just to let you know
I modified the script to allow for single characters to remain in the inputfield.
// http://forum.unity3d.com/threads/android-touchscreenkeyboard-not-always-returning-value-to-inputfield.317047/
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
// TODO: remove this component when unity bug will be resolved
[RequireComponent(typeof(InputField))]
public class InputFieldFix : MonoBehaviour
{
private string backUpString = "";
private InputField input;
private bool isFinishingEditing;
void Start()
{
isFinishingEditing = true;
OnChangeValue(" ");
input = GetComponent<InputField>();
input.onValueChange.AddListener(OnChangeValue);
input.onEndEdit.AddListener(OnEndEdit);
}
void OnDestroy()
{
input.onValueChange.RemoveAllListeners();
input.onEndEdit.RemoveAllListeners();
}
void OnChangeValue(string value)
{
if (string.IsNullOrEmpty(value) && isFinishingEditing == true)
{
input.text = backUpString;
isFinishingEditing = false;
}
backUpString = value;
}
void OnEndEdit(string value)
{
isFinishingEditing = true;
}
}
Note that it isn’t 100% stable when using a single character, but it works most of the time.