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

Question How to change character type in textfield UI Toolkit?

Discussion in 'UI Toolkit' started by skelitheprogrammer, Dec 19, 2021.

  1. skelitheprogrammer

    skelitheprogrammer

    Joined:
    Sep 20, 2019
    Posts:
    14
    Hello. I want to make Textfield which will only allow type positive and negative digits to change values in script runtime. I could not find this parameter inside UI Toolkit and making my own solution but I wonder is it even possible at the moment?

    2021.2.3f1 unity
    1.0.0-preview.18 exp
     
  2. Monogenesis

    Monogenesis

    Joined:
    Dec 30, 2019
    Posts:
    56
    Hello,
    you can react on the input from the TextField. So you could register a callback like textField.RegisterCallback<FocusOutEvent>() and then parse the information inside the TextField to your liking or use any other callback you see fit.

    Hope that gets you going.
     
  3. skelitheprogrammer

    skelitheprogrammer

    Joined:
    Sep 20, 2019
    Posts:
    14
    Yes, im already made a parser which allow only positive int numbers with <InputEvent>. But when it will be implemented inside of InputField? Or Im missing something?
     
  4. Monogenesis

    Monogenesis

    Joined:
    Dec 30, 2019
    Posts:
    56
    For inspector gui's it is already there but not for the runtime UI.
    I'am aswell hope that the team is working on implementing that for the runtime UI.
     
  5. HugoBD-Unity

    HugoBD-Unity

    Unity Technologies

    Joined:
    May 14, 2018
    Posts:
    443
    Hi @skillitronic !

    Unfortunately, we currently don't offer a good solution for allowing users to add validation to TextFields... You could use a different type of field (the IntegerField, the FloatField, the DoubleField...) which have built-in validation, but those are not available at runtime before 2022.1.

    We plan to improve this in the future. I feel it is cumbersome to use a different type of field to have validation, and I think it would be more convenient to handle validation directly in the default TextField.
     
    dynamicbutter likes this.
  6. Monogenesis

    Monogenesis

    Joined:
    Dec 30, 2019
    Posts:
    56
    I'am very glad that you and your team are here and improving the system! Thanks!
     
    HugoBD-Unity likes this.
  7. skelitheprogrammer

    skelitheprogrammer

    Joined:
    Sep 20, 2019
    Posts:
    14
    Thanks for the answer.
     
  8. dynamicbutter

    dynamicbutter

    Joined:
    Jun 11, 2021
    Posts:
    62
    Thanks for the update all. I agree @hugobd it's a little inconvenient now. In case others need an immediate solution, something like this worked for me...

    Code (CSharp):
    1.  
    2. // Get a callback every time the value changes (here textField is a TextField)
    3. textField.RegisterValueChangedCallback(IntegerValidation);
    4.  
    5. // Reject changes unless they only contain integer characters
    6. void IntegerValidation(ChangeEvent<string> changeEvent)
    7. {
    8.     var textField = changeEvent.target as TextField;
    9.     if (!ValidateCharacters(changeEvent.newValue, "0123456789"))
    10.     {
    11.         textField.value = changeEvent.previousValue;
    12.     }
    13. }
    14.  
    15. bool ValidateCharacters(string value, string validCharacters)
    16. {
    17.     foreach (var c in value)
    18.     {
    19.         if (!validCharacters.Contains(c))
    20.         {
    21.             return false;
    22.         }
    23.     }
    24.     return true;
    25. }
     
    Evalie and HugoBD-Unity like this.