- Home /
 
Making a UI.Button put text into a InputField
Hi, I am trying to make a UI.Button input a letter into the InputField when pressed. I have tried many different methods but none of them seems to work.
Secondly I also have a backspace button which I want to remove the last letter in the InputField which I also can't get to work.
Answer by Mmmpies · Mar 02, 2015 at 06:11 PM
You don't really need an input field just a text field will do it if you want to make a custom keyboard, this is in C#...
 using UnityEngine;
 using UnityEngine.UI;        // add at the top
 using System.Collections;
 
 public class MyKeyboard : MonoBehaviour {
 
     public Text myText;        // drag your text object on here
     private bool shiftOn = false;
     
     public void ClickLetter(string letterClicked)
     {
         string tempCurString = myText.text;
         
         if(shiftOn)
             letterClicked = letterClicked.ToUpper();
                 
         string tempNewString = tempCurString + letterClicked;
         myText.text = tempNewString;
     }
     
     public void ClickBackspace()
     {
         string tempGetString = myText.text;
         if(tempGetString.Length > 0)
         {
             string tempString = tempGetString.Substring(0, tempGetString.Length -1);
             myText.text = tempString;
         }
     }
     
     public void PressShift()
     {
         shiftOn = !shiftOn;
     }
 }
 
               Drag that onto the panel your custom keyboard is on and add all your buttons, including Shift and Backspace. In the OnClick for each button click + and drag the panel with the script on it onto the slot that appears.
In the OnClick drop down for the letter/number buttons select MyKeyboard -> ClickedLetter and set the string field to be the lowercase letter for that button.
On the Shift/Backspace do the same but select PressShift/ClickBackspace.
Don't forget to drag the Text object onto the Panels Text field in that script.
Should work fine.
$$anonymous$$mmpies your a genius!!!, I wish I come across this answer sooner than I did.
Your answer