- Home /
How to save text and display using Touch Screen Keyboard
Hey guys,
A user clicks a button and the iphone keyboard pops up. They are prompted to enter their player's name. I want to be able to save the text they enter in Player Prefs so I can grab their name throughout the game. Seemingly simple but nothing is being saved. After the user taps "return" or "done" on the keyboard there is nothing saved but an empty string in the PlayerPrefs. Here's the code:
private string playerName;
void NamePlayer() {
TouchScreenKeyboard keyboard;
keyboard = TouchScreenKeyboard.Open(playerName, TouchScreenKeyboardType.Default, false, false, false, false, "Enter Name");
if (keyboard.active)
playerName = keyboard.text;
PlayerPrefs.SetString("PlayerName",playerName);
}
Have you tried using 'keyboard.done' ins$$anonymous$$d of 'keyboard.active'? 'active' is for when the keyboard is still visible, 'done' is for when its gone away.
Hey ya I've tried using keyboard.done. That doesn't do anything for me. originally I had it set up like so:
if (keyboard.active)
playerName = keyboard.text;
if (keyboard.done)
PlayerPrefs.SetString("PlayerName", playerName);
is there any ways to implement Touch Screen $$anonymous$$eyboard for Windows PC Platform???
@girishgowdayt : Please don't post comments as answers. Post comments by clicking the [add new comment] button, a window then open for you to type in. Answer fields are for answers only, as this is a knowledge base.
Read this page : http://answers.unity3d.com/page/newuser.html
Please watch : http://video.unity3d.com/video/7720450/tutorials-using-unity-answers
I have never done anything for windows touch. I did a quick search, perhaps something here or one of the links mentioned might help :
Answer by AlucardJay · Feb 25, 2013 at 02:48 AM
This is what I wrote to use the iPhone keyboard :
#pragma strict
public var kBoardString : String = "";
private var tempString : String = "";
private var keyboard : iPhoneKeyboard;
private var iskBoardOpen : boolean = false;
function Update()
{
kBoard0();
}
function kBoard0()
{
// keyboard = iPhoneKeyboard.Open( tempName : String, iPhoneKeyboardType.Default, Auto-Correct, Multi-Line, Secure, Alert, "Text Placeholder" );
if ( !iskBoardOpen )
{
keyboard = iPhoneKeyboard.Open( tempString, iPhoneKeyboardType.Default, false, false, false, false, "Default Keyboard" );
iskBoardOpen = true;
}
if ( keyboard.done )
{
kBoardString = keyboard.text;
tempString = "";
iskBoardOpen = false;
}
else
{
kBoardString = keyboard.text;
}
}
Alucardj, Thank you for the response. I realized what the issue was with my script. I was trying to set a Player Pref during the same frame as when the keyboard was opened. So obviously it wasn't working. I noticed you placed your code to run in the Update function and that's why yours works and $$anonymous$$e doesn't. I either need to do something similar or initate a coroutine and yield while the keyboard is active. Thanks for the tip.
Yes, it was some experimenting to get this right. The kBoard function call was actually in a switch-case, but called every frame while waiting for input, hence the boolean. I was having trouble reading tempString, so it is just a null variable for the iPhone$$anonymous$$eyboard command to use. Glad this helped =]
Your answer
Follow this Question
Related Questions
iPhoneKeyboard.area works? 2 Answers
TouchScreenKeyboard not working with GUI.TextArea (tested with Android tablet) 0 Answers
How can I tell if the device I am on supports touch? 4 Answers
Remove "Preview text line" above keyboard input text on Android and iPhone 1 Answer
What is the command to check whether player is touching the screen or not?? 1 Answer