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. Join us on Thursday, June 8, for a Q&A with Unity's Content Pipeline group here on the forum, and on the Unity Discord, and discuss topics around Content Build, Import Workflows, Asset Database, and Addressables!
    Dismiss Notice

TeshMeshPro InputFields and onValueChanged event

Discussion in 'UGUI & TextMesh Pro' started by doingTheCodes, Apr 8, 2018.

  1. doingTheCodes

    doingTheCodes

    Joined:
    Apr 27, 2016
    Posts:
    19
    I'm not extremely well versed with delegates but I feel like I should be able to do something like

    textbox.onValueChanged += UpdateText;
    OR
    textbox.onValueChanged = UpdateText;


    private void UpdateText(object sender, EventArgs e)
    {
    // do things here
    }

    This doesn't work. What I want to do is have a textbox that can only have one letter in it at a time. so when the user clicks on it and types another key, the current value is replaced with the incoming value. I tried to lookup any documentation on onValueChanged and I've more or less got nothing
     
  2. doingTheCodes

    doingTheCodes

    Joined:
    Apr 27, 2016
    Posts:
    19
    This is what I ended up doing
    Code (CSharp):
    1.     public void Start()
    2.     {
    3.  
    4.         foreach (var textbox in Inputs)
    5.         {
    6.             textbox.text = "";
    7.             textbox.onValueChanged.AddListener(delegate { UpdateTextBox(textbox); });
    8.         }
    9.     }
    10.  
    11.     public void UpdateTextBox(TMP_InputField textbox)
    12.     {
    13.         if (textbox.text.Length > 1)
    14.         {
    15.             textbox.text = textbox.text[1].ToString();
    16.         }
    17.     }
    Then in the inspector, I had all of the TextMeshPro input fields add a reference to the UpdateTextBox method and pass themselves as arguments. I also set the character limit to 2. It seems extremely hacky, but it works.
     
    unity_lMC_sAI-WQ3yHw likes this.
  3. unity_lMC_sAI-WQ3yHw

    unity_lMC_sAI-WQ3yHw

    Joined:
    Sep 21, 2021
    Posts:
    2
    Thank you!
     
  4. leezak5555

    leezak5555

    Joined:
    Jan 16, 2020
    Posts:
    5
    That seems to be working as well:

    inputField.onValueChanged.AddListener((x) => SomeFunctionName(inputField.text));
    and the function looks like this :

    public void SomeFunctionName(string newInputFieldText)