- Home /
Android: TouchScreenKeyboard not functioning
The TouchScreenKeyboard just doesn't want to work. It opens up, you can put in the string but as soon as you click done, it's gone- like it was never there. You can do this everyday with any string doesn't work.
I am just using two simple lines of code
// declaration and stuff
//psuedocode
private string x1;
private TouchScreenKeyboard numpad = null;
private string x1;
public tk2dTextMesh textfield;
void Update () {
if (Input.touchCount > 0) {
Ray r0 = Camera.main.ScreenPointToRay (new Vector2(Input.GetTouch(0).position.x, Input.GetTouch(0).position.y));
RaycastHit hit;
if(Physics.Raycast(r0, out hit))
{
Debug.Log ("Enter The Number");
if(hit.collider.name == "NumInput")
{
numpad = TouchScreenKeyboard.Open (x1, TouchScreenKeyboardType.NumberPad);
numpad.active = true;
}
}
}
if (numpad != null) {
textfield.text = x1;
}
}
I have wasted 10 hours on this plus I have to make apk(s) everytime to test the code out..
Answer by Yury-Habets · Dec 18, 2014 at 07:19 AM
You may want to use numpad.text property to get the result. x1 is the input param in your code, the default string. Please refer to http://docs.unity3d.com/ScriptReference/TouchScreenKeyboard.Open.html
Answer by zharik86 · Dec 18, 2014 at 08:03 AM
In your case, when you click "Ok", your mobile keboard is gone, but variable "numpad" is not null. Maybe, check status this variable. But you can use visible/invisible keyboard method, try:
//Add variable for detection
private bool isFirst = false;
void Start() {
x1 = ""; //initialization string, whitout line your variable is null
}
void Update () {
if (Input.touchCount > 0) {
Ray r0 = Camera.main.ScreenPointToRay (new Vector2(Input.GetTouch(0).position.x, Input.GetTouch(0).position.y));
RaycastHit hit;
if(Physics.Raycast(r0, out hit)) {
Debug.Log ("Enter The Number");
if(hit.collider.name == "NumInput") {
numpad = TouchScreenKeyboard.Open (x1, TouchScreenKeyboardType.NumberPad);
numpad.active = true;
//Change variable for activation
isFirst = true;
}
}
}
//Change end condition (or, maybe, play with it parametr)
if (isFirst && !TouchScreenKeyboard.visible) {
textfield.text = x1;
isFirst = false;
}
}
I hope that it will help you.