Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

How to create your own InputField

Discussion in 'Scripting' started by phoda, Mar 16, 2020.

  1. phoda

    phoda

    Joined:
    Nov 11, 2014
    Posts:
    384
    Okay, so my game requires me to have my own text input, thing is i have been doing it this way for now
    Code (CSharp):
    1. void Update(){
    2.         //Detects inputs
    3.         if (Input.anyKeyDown){
    4.             foreach(KeyCode x in keyCodes){
    5.                 if (Input.GetKeyDown(x)){
    6.  
    7.                     //Key input
    8.                     if (keyInputs.Contains(x)) {
    9.                         RD.UIDB.t_Output.text += "" + x.ToString();
    10.                         Debug.Log("KeyCode down: " + x);
    11.                         break;
    12.                     }
    13.                 }
    14.             }
    15.         }
    16.     }
    keycodes is array of keycodes and keyInputs is list of KeyCode to see if that letter should be displayed or not. That work no problem BUT if you want to type {, in europe is alrgr + B yet in america its defined key so making it display when pressing b and altgr is not viable solution.

    I was thining about having text input and on each update it transfers text from input field to my text but seems weird workaround and i just control in game is my inputfield focused or not.

    anyone has ideas or questions?
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,797
    Have you considered using this function for your string input:

    https://docs.unity3d.com/ScriptReference/Input-inputString.html

    It will usually only return one or two characters in a given frame, and then you can add that to your string as you go.

    It also returns backspace and return/newline. There's sample code in there to do almost precisely what you're trying to do.
     
    phoda likes this.
  3. phoda

    phoda

    Joined:
    Nov 11, 2014
    Posts:
    384
    seems what i have been looking for, thanks.