Unity InputField OnValueChanged事件为InputField.text少显示一个字符 [英] Unity InputField OnValueChanged event shows one less character for InputField.text
问题描述
我有一个InputField
用作搜索栏.我无法使用OnValueChanged
自动搜索,因为最初,如果我输入诸如a
之类的任何字符,则文本字段现在将为""
,因此inputField.text
仍然是""
而不是a
在添加下一个字符之前,不会进行搜索.
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);
}
}
此方法附加到输入字段的On Value Changed
事件,而searchText
是该输入字段的文本".
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);
}
}
请注意,某些事件具有应包含在处理程序签名中的参数.例如OnValueChanged
期望UnityAction<string>
作为其处理程序,或者仅是具有该签名SearchGame(string)
的方法.
Note that some events have a parameter which should be included in the handler's signature. e.g. OnValueChanged
expects a UnityAction<string>
as its handler or simply a method with that signature SearchGame(string)
.
因此,传递SearchGame()
会忽略<string>
参数.
So passing SearchGame()
will ignore the <string>
parameter.
这篇关于Unity InputField OnValueChanged事件为InputField.text少显示一个字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!