简体   繁体   中英

Unity InputField OnValueChanged event shows one less character for InputField.text

I have an InputField where I am using it as a search bar. I am not able to search automatically with OnValueChanged because initially, the text field will be "" now if I enter any character like a , the inputField.text is still "" instead of a because of that the search won't happen until the next character is added.

Is there any way to take the current text at the first hit of the event?

public void SearchGame()
    {
        try
        {
            if (!string.IsNullOrEmpty(SearchText.text) && SearchText.text != "")
            {                    
                var listofvalues = list1.Where(x => x.name.ToLower().StartsWith(SearchText.text.ToLower(), StringComparison.CurrentCulture)).ToList();

                if (listofvalues.Count > 0)
                {                       
                    foreach (var value in listofvalues)
                    {                           
                        //loading 
                    }
                }
                else
                {
                    //No search result
                }
            }
            else
            {
               //stuff                   
            }
        }
        catch (Exception exception)
        {
            LoggingManager.Error(exception);
        }
    }

This method is attached to the On Value Changed event of the inputfield and searchText is the Text of that input field.

Looks like you didn't use dynamic parameter allocation on this event.

public void SearchGame(string text)
{
    try
    {
        if (!string.IsNullOrEmpty(text) && text != "")
        {                    
            var listofvalues = list1.Where(x => x.name.ToLower().StartsWith(text.ToLower(), StringComparison.CurrentCulture)).ToList();

            if (listofvalues.Count > 0)
            {                       
                foreach (var value in listofvalues)
                {                           
                    //loading 
                }
            }
            else
            {
                //No search result
            }
        }
        else
        {
           //stuff                   
        }
    }
    catch (Exception exception)
    {
        LoggingManager.Error(exception);
    }
}

Note that some events have a parameter which should be included in the handler's signature. eg OnValueChanged expects a UnityAction<string> as its handler or simply a method with that signature SearchGame(string) .

So passing SearchGame() will ignore the <string> parameter.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM