Search Unity

Question How to set ContentType to TextField?

Discussion in 'UI Toolkit' started by Miha-FLG, Nov 4, 2022.

  1. Miha-FLG

    Miha-FLG

    Joined:
    Mar 17, 2022
    Posts:
    4
    Is it possible to set ContentType to a UI Toolkit TextField?

    For example, I'm making a UI for a mobile game, that has an input field, that only accepts numbers. How can I set TextField to open the "numbers keyboard" on a mobile device, rather than the default one.
    I know I could do validation on it, but opening the right keyboard is important in my case.

    I couldn't find a way to do it, I'm only able to set it as a password field. Is there a way to do it (even if it's "hacky")? The only thing I can think of is "hijacking" the click event and opening up the keyboard manually, with the right settings. Are there any better solutions for this?
     
    oscarAbraham likes this.
  2. FreddyC-Unity

    FreddyC-Unity

    Unity Technologies

    Joined:
    Jun 9, 2021
    Posts:
    36
    Hi @Miha-FLG ,

    Just wanted to quickly highlight we have other TextField types such as Int, Float, Double, Long, and Hash128 that applies soft validation on what's being typed into them. In regards to changing the keyboard type being used, unfortunately there isn't a clean way to achieve this pre Unity 2023.1. For now, the most common way to achieve this is, as you mentioned, would be to highjack the event and perform your own TouchScreenKeyboard.open(...).

    On a happier note, you will be able to change the keyboard type through the UI builder or script with the upcoming version.

    Hope this helps!
     
    oscarAbraham likes this.