Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by jessesloan1 · Apr 20, 2017 at 07:38 PM · textfilessearchstrings

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.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Matthewj866 · Apr 20, 2017 at 09:33 PM

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!

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image jessesloan1 · Apr 21, 2017 at 11:01 PM 0
Share

Thanks for the answer @by $$anonymous$$atthewj866!

To be honest it may be a few days before I am actually prepared to try out your answer. At first glance, it looks like a winner. I just need to spend some time messing with my text files to get them into X$$anonymous$$L. I intend to do a few pages and then try it out.

I'll let you know my results.

avatar image Matthewj866 jessesloan1 · Apr 21, 2017 at 11:06 PM 0
Share

Good luck! This solution is based off many X$$anonymous$$L extractors ive had to build for conventional software over the years, so it should just work if you point it to the right place. :)

avatar image Matthewj866 jessesloan1 · Apr 30, 2017 at 11:07 PM 0
Share

@jessesloan1 so...what happened?

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

102 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Rubik's Cube scramble algorithm 0 Answers

Text box not displaying a character split string generated at runtime 0 Answers

TextAsset.text field is always null even though file is not empty 0 Answers

Cannot find and import heightmaps 1 Answer

Making a in editor public writing prompt 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges