Creating a search function using an input field

Updated on November 15, 2018 in [A] GUI
Share on Facebook0Tweet about this on TwitterShare on Google+0Share on Reddit0
1 on November 15, 2018

Hi all,

I’m trying to create a kind of ‘search function’ by having multiple buttons and an input field. For arguments sake we can call the buttons “A, B, C, D, E, F”, when I type ” f ” into the input field, I would like all buttons but ” F ” to be switched off. I can’t find much on this other than I may need to create a list to achieve this.

Any advice would be a big help. thank you

-M

  • Liked by
Reply
0 on November 15, 2018

Input fields have an action called OnValueChanged

If in you write a function that takes in a string, and could check it against a predefined list of game objects, you would just call that and pass in the input field text from the OnValueChanged event.

 

Something like this:

foreach(GameObject go in listOfSearchableGameobjects)
{
  go.SetActive(string.Compare([InputFieldTextGoesHere], go.name) <= 0);
}

 

string.Compare is something you could use to sort a list of strings alphabetically. If it returns less tan 0, the first string would come before the second one, if it’s 0, they’re the same, greater than 1, the first one would come after the second one.

So here, we’re checking if what you typed in comes alphabetically before the objects name. If you type in “F” this will disable everything that comes before “F”

This way doesn’t disable ones that come after it though, but you could add that with a few extra lines.

  • Liked by
Reply
Cancel