- Home /
how to get Inputs from touch keyboard...
Hi...can anyone tell my how do we get input from the touch keyboard.. like enter your name in a box .. when we touch the box keyboard appear and write your name and that name show on the other scene.. like it say on ther other scene" welcome XYZ"
Answer by salvador007 · Apr 17, 2014 at 09:49 AM
Yes, u can use GUI.TextField for solution to this problem.
Using this you can even restrict maximum string length.
:)
@salvador007 your suggestion is good but i have impleted that in anotherway only thing left is when keyboard is done writing... see my above comment
Answer by OSG · Apr 15, 2014 at 01:51 PM
GUI.TextField will do all work for you. Just save string from it in some global variable and get it when you need
edit: in Update() use TouchScreenKeyboard.done to catch the moment when user entered his name. More info about TouchScreenKeyboard here.
yahhh ... can you explain me how actually we can restrict the user to write name up to 15 characters ...if they write above the limit of 15 character show notification to write in between 0-15 character ? is it possible in unity ? and i am showing the text through the GUIText...
#pragma strict
public var enterName:GUIText;
var inputText : String = "Enter name";
private var keyboard : TouchScreen$$anonymous$$eyboard;
// Updates button's text while user is typing
function OnGUI() {
if (GUI.Button(Rect(0, 10, 200, 32), inputText))
keyboard = TouchScreen$$anonymous$$eyboard.Open(inputText);
if (keyboard)
inputText = keyboard.text;
enterName.text=inputText;
}
Try this
public var enterName:GUIText;
var inputText : String = "Enter name";
private var keyboard : TouchScreen$$anonymous$$eyboard;
// Updates button's text while user is typing
function OnGUI() {
if (GUI.Button(Rect(0, 10, 200, 32), inputText))
keyboard = TouchScreen$$anonymous$$eyboard.Open(inputText);
if (keyboard) {
inputText = keyboard.text;
if(inputText.length < 15)
enterName.text = inputText;
else
GUI.Label(Rect(0, 50, 200, 32), "Name is too long. Use less than 15 chars");
}
}
@OSG now i have implemented it like this way.. with button as it was before and i give the GUIStyle to button so it doesn't show the black background and i adjusted the button position and it show on the exact writing box so when i touch the writing box it show keyboard and in the inspector,i set the GUISTyle text cliping enable so the text doesn't go out of the box and through lenght method i can measure the lenght ....now only thing left is when i press the done button it should play an iTween animation or load new level ...i tried to make it happen like below code but it showing me error on keyboad.done (NullReferenceException: Object reference not set to an instance of an object touchinput.OnGUI () (at Assets/Scripts/touchinput.js:18) )
var inputText : String = "Enter name";
var guiStyle:GUIStyle;
var timer:float;
var done: boolean;
private var keyboard : TouchScreen$$anonymous$$eyboard;
// Updates button's text while user is typing
function OnGUI()
{
timer+=Time.deltaTime;
if(timer>7)
if (GUI.Button(Rect(Screen.width/2-200,Screen.height/2-220,Screen.width/2,Screen.height/18), inputText,guiStyle))
// print("IN");
{
keyboard = TouchScreen$$anonymous$$eyboard.Open(inputText, TouchScreen$$anonymous$$eyboardType.Default);
}
if (keyboard.done)
inputText = keyboard.text;
var lenght= inputText.length;
print(lenght);
}
Funny thing .... i don't know its a bug or what... in unity console it show the error (NullReferenceException: Object reference not set to an instance of an object touchinput.OnGUI () (at Assets/Scripts/touchinput.js:18) ) .....but when i build and run on my android device it works fine lolx :D... ... now whats this ? .. :D #pragma strict
var inputText : String = "Enter name";
var guiStyle:GUIStyle;
var timer:float;
// var done: boolean;
private var keyboard : TouchScreen$$anonymous$$eyboard;
// Updates button's text while user is typing
function OnGUI()
{
if (GUI.Button(Rect(Screen.width/2-200,Screen.height/2-220,Screen.width/2,Screen.height/18), inputText,guiStyle))
// print("IN");
{
keyboard = TouchScreen$$anonymous$$eyboard.Open(inputText, TouchScreen$$anonymous$$eyboardType.Default);
}
if (keyboard)
{
inputText = keyboard.text;
var lenght= inputText.length;
}
// Application.LoadLevel(0);
if(keyboard.done)
{
Application.LoadLevel(0);
}
}
Your answer
Follow this Question
Related Questions
Checking whether string is a valid Input.Key 1 Answer
A node in a childnode? 1 Answer
How to distinguish between multiple keyboards? 1 Answer
How can i make button on iOS instead key 0 Answers
What just happened to my Unity!? 3 Answers