Runtime Search Bar for all UI text fields on a canvas?

I’m creating an educational book in Unity. I would like to be able to search keywords from all of my UI text. I would like to generate a list of the text fields that contain the word and be able to navigate to each one. Anyone know of an existing asset or how to go about this? Thanks!

Finding the list of text fields containing that word is pretty straightforward. You can do something like:

public void SetSearchString(string search) {
   search = search.ToLower(); // this way search can be case insensitive
   Text[] allTexts = thePanelContainingYourContent.GetComponentsInChildren<Text>();
   foreach (var thisText in allTexts) {
      if (thisText.text.ToLower().Contains(search)) {
         //do something with this text field
      }
   }
}

If you want to highlight the actual word, you might be able to use .Substring to inject some rich text tags around the found search term (as long as you remember to store the original value of the text and restore it before you do anything else).

1 Like

Thank you.