- Home /
3d text to trigger mobile keyboard
I want to use 3d text as the button to trigger open mobile keyboard. Currently what I do is having a box as the collider as the child to the 3d text and change size accordingly to the length of the 3d text. Whenever you touch the box collider, it will trigger a GUI.Textfield and when you tap on the GUI.Textfield, the mobile keyboard will be launch for you to input. here's my code:
void OnGUI () {
if(inEditMode) {
GUI.SetNextControlName ("hiddenTextField"); //Prepare a Control Name so we can focus the TextField
GUI.FocusControl ("hiddenTextField"); //Focus the TextField
guiString = GUI.TextField (new Rect (90, 100, 200, 25), guiString, 25); //Display a TextField
textComponent.text = guiString;
}
if (Input.GetMouseButtonDown(0)) {
ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if(Physics.Raycast(ray,out hit))
{
if(hit.transform== boxcollider.transform) {
print("touched!!");
inEditMode = true;
} else {
inEditMode = false;
}
}
}
}
But I want to make do without tapping on the GUI.Textfield. Is it possible?
I tried an alternative method:
if (Input.GetMouseButtonDown(0)) {
ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if(Physics.Raycast(ray,out hit))
{
if(hit.transform== boxcollider.transform) {
TouchScreenKeyboard.Open("", TouchScreenKeyboardType.Default, false, false, true, true);///<-- Added this line!!!
inEditMode = true;
} else {
inEditMode = false;
}
}
}
That TouchScreenKeyboard.Open
did trigger a keyboard but do not sync with GUI.Textfield. but if you tap on GUI.Textfield at this moment, another keyboard will open and that will sync. At this point, you have 2 layers of keyboard.
Any suggestions will be good.
Have you tried passing focus to the field before opening the keyboard
re-read this. See the example, you need to retrieve the text from the keyboard and set your guiString to it. Be sure to check to make sure the keyboard is open like the example does.
Answer by KRanges · Apr 21, 2014 at 01:15 AM
You need to re-read this, after one pass i can see your error. Read the example they give. And see what the keyboard function is actually doing. It returns a type touchscreenkeyboard, which has variables you can access such as text(being the text typed into the keyboard) as you can see in the unity example you need to update you textfields text to relect that of the keyboards text.
Answer by sooonism · Apr 21, 2014 at 05:43 AM
Thanks to KRanges' advise, Now I figure out:
///define keyboard and important variables
private TouchScreenKeyboard keyboard;//<--- add this line
private bool inEditMode = false;
private string guiString;
/// more codes here...
void OnGUI () {
if(inEditMode) {
GUI.SetNextControlName ("hiddenTextField"); //Prepare a Control Name so we can focus the TextField
GUI.FocusControl ("hiddenTextField"); //Focus the TextField
guiString = GUI.TextField (new Rect (90, 100, 200, 25), guiString, 25); //Display a TextField
guiString = keyboard.text; //<-- add this line
textComponent.text = guiString;
}
if (Input.GetMouseButtonDown(0)) {
ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if(Physics.Raycast(ray,out hit))
{
if(hit.transform== boxcollider.transform) {
print("touched!!");
keyboard = TouchScreenKeyboard.Open(guiString, TouchScreenKeyboardType.URL);//<--- add this line
inEditMode = true;
} else {
inEditMode = false;
}
}
}
}
One thing though, if you do this, the normal keyboard is not working anymore.
What do you mean normal keyboard? Ill try running your script in the morning and see what happens, never launched the keyboard manually myself.
By normal $$anonymous$$eyboard I mean keyboard on your computer. $$anonymous$$y script only works with iPad at the moment. If you test it inside Unity nothing will happen. This as far as I know.
There are a lot more things than just the script. If you are interested, P$$anonymous$$ me your email and I will send you the scenes and the scripts.
Your answer
Follow this Question
Related Questions
How to save text and display using Touch Screen Keyboard 1 Answer
how to hide ios keyboard but keep input field in focus for Voice Control 0 Answers
Touch interface with alternative keyboard in Unity3D? 1 Answer
How Can I Switch Focus Between 3D Text Buttons By Using The Keyboard? 0 Answers
How to call windows native keyboard and have it ontop of a fullscreen build 0 Answers