Search multiple text files for string input by user

My game has a LOT of text. Like a total of 650+ pages. The text is organized in one folder. In the folder, each paragraph is its own text file so there are literally hundreds of files. Each file is structured like this:


1.1 - “Some Header Label”

A paragraph of text that might be several sentences long. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Commoda autem et incommoda in eo genere sunt, quae praeposita et reiecta diximus; Obsecro, inquit, Torquate, haec dicit Epicurus? Ut proverbia non nulla veriora sint quam vestra dogmata. Sed in rebus apertissimis nimium longi sumus. Duo Reges: constructio interrete. Id mihi magnum videtur. Illi enim inter se dissentiunt. Ego quoque, inquit, didicerim libentius si quid attuleris, quam te reprehenderim.


In the game there are multiple “Nodes” that look like little buttons. Each node represents a particular paragraph of the text. Each node has a label something like “Chapter 1, Para. 5”.

What I want to do is have a search field where the user might enter a word or a sentence like “One Flew Over the Cookoos Nest.” Then I want to present search results in my UI like this (on left-hand side of the screen):


Search Results

1.1 Some Header

“…the sentence where the SEARCHED WORD was found…”

9.7 A Different Header

“…the sentence where the SEARCHED WORD was found…”

10.2 Another Header

“…the sentence where the SEARCHED WORD was found…”

12.4 Yet Another Header

“…the sentence where the SEARCHED WORD was found…”


Because the text is broken up into multiple files I need the search to search every file in the folder. The search should give me:

  • File Name
  • Paragraph Number
  • The Header
  • several words in front of and behind the search term for display in the UI search results area.

Lastly I want to make the nodes shown in the playspace that correspond to the search results Paragraph numbers stand out from the rest by glowing or shaking or something.

Does anyone have any suggestions how to perform this type of search within Unity? Will standard Non Unity C# (.Net) work? If so, will there be a need for inheritance from special classes that are not Monobehavior?

Any guidance or help on this will be appreciated!

BTW, right now the plan is to have the files as plain text files. But I may decide to go with XML of Json. any thoughts on what’s best? When the game is built I will need to encrypt these files (or something) to prevent nefarious users from accessing the plain text.

Hi there @jessesloan1

Firstly, I highly recommend using XML or JSON, custom formats can get messy fast and JSON and XML are already established conventions that everyone can read and understand.

You do not need a UnityEngine.MonoBehaviour type to read external files - instead you would be better off creating a new static System.Object class. Let’s call this class TextUtilities.

Inside our new class, you can define some static methods you can call from anywhere - to keep it basic as an example, lets just target the header of the file for now, else I’ll be writing this all night.

Assuming you are using XML and if I’ve understood the example correctly, you can do the following:

public static class TextUtilities 
{
    public static List<XDocument> FindDocumentsByHeader(string comparisonText)
    {
        List<XDocument> docsToReturn = new List<XDocument>();
        List<string> filePaths = Directory.GetFiles(pathWhereFilesAreStored).ToList();
        foreach(string path in filePaths)
        {
            XDocument docToCheck = XDocument.Load(path);
            if (docToCheck.root.Element("header").Value.ToUpper().Contains(comparisonText.ToUpper()))
            {
                docsToReturn.Add(docToCheck);
            }
        }
        return docsToReturn;
    }
}

This should return all documents that contain the search text in the header, regardless of case sensitivity.

Please note this snippet uses both System.Linq and System.Xml.Linq.

Hopefully you can adapt this to whatever else you need. You could also adapt this to use JSON instead, but since I know XML manipulation in C# better I just did the example in it.

Hope this helps getting you started!