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 /
avatar image
0
Question by KnightRiderGuy · Jul 02, 2017 at 12:57 PM · uitextsave dataexternal files.txt

How to make a editable text list using external text file

I've been looking for a good method of writing and reading from an external .txt file to create a scavenger hunt list.

The idea is that if you say you are looking for an item: Example: "we are looking for a giant rubber spider" Then that information would somehow be saved into a .txt file to be read back in later. If you were to say: "We are looking for a witches hat" That too would be saved in a list format to the same .txt file.

Now here is where it might get tricky. If you say we have found the rubber spider then that item gets removed from the .txt file.

The list would also need to be displayed when called upon as a UI Text list that would update automatically based on what is currently in the .txt file.

Has anyone done something like this?

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
1

Answer by ShadyProductions · Jul 02, 2017 at 03:19 PM

Maybe something like this?:

 using System.Collections.Generic;
 using System.Linq;
 using System.IO;
 
 namespace Assets
 {
     public class TextHandler
     {
         // Enter the path of the file here
         private string filePath = "";
 
         private List<string> _fileLines;
         private List<string> FileLines
         {
             get
             {
                 if (_fileLines != null) return _fileLines;
                 string text = File.ReadAllText(filePath);
                 if (string.IsNullOrEmpty(text))
                 {
                     // Make empty list
                     _fileLines = new List<string>();
                 }
                 else if (!string.IsNullOrEmpty(text) && text.Any(f => f.Equals(',')))
                 {
                     // Read text and split on comma
                     _fileLines = text.Split(',').ToList();
                 }
                 else
                 {
                     // Read lines instead
                     _fileLines = File.ReadAllLines(filePath).ToList();
                 }
                 return _fileLines;
             }
             set
             {
                 _fileLines = value;
                 File.AppendAllText(filePath, string.Join(",", _fileLines.ToArray()));
             }
         }
 
         /// <summary>
         /// Retrieve all up to date lines
         /// </summary>
         /// <returns></returns>
         public List<string> GetAllLines()
         {
             return File.ReadAllText(filePath).Split(',').ToList();
         }
 
         /// <summary>
         /// Remove existing line from file if it exists
         /// </summary>
         /// <param name="line"></param>
         public void RemoveLine(string line)
         {
             // Remove line
             FileLines.Remove(line);
 
             // Save file
             File.AppendAllText(filePath, string.Join(",", _fileLines.ToArray()));
         }
 
         public void AddLine(string line)
         {
             // Add line
             FileLines.Add(line);
 
             // Save file
             File.AppendAllText(filePath, string.Join(",", _fileLines.ToArray()));
         }
     }
 }
Comment
Add comment · Show 8 · 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 KnightRiderGuy · Jul 02, 2017 at 04:18 PM 0
Share

Thanks @ShadyProductions How would I use this? Would this go on a game object or is this something that would somehow be referenced in my other scripts? Say something like:

 if (RubberSpiderIsFound == true){
 Remove from list (How would I get that to work with what you have here?
 }

Does it create a .txt document or would I need a blank one imported, something like scavHuntList.txt ??

avatar image ShadyProductions KnightRiderGuy · Jul 02, 2017 at 05:30 PM 0
Share

You would do something like this:

 private TextHandler _textHandler = new TextHandler();
 
 if (RubberSpiderIsFound == true){
 _textHandler.AddLine("Rubber spider is found");
 }
 
 if (RubberSpider$$anonymous$$ustBeRemoved == true){
 _textHandler.RemoveLine("Rubber spider is found");
 }
 
 UI.text = string.Join(",", _textHandler.GetAllLines());

It will automatically create a file on its first "addline" if none exists in the given filePath in the TextHandler class In the filepath you can use: Application.dataPath which will point to your assets folder

avatar image KnightRiderGuy ShadyProductions · Jul 02, 2017 at 11:22 PM 0
Share

O$$anonymous$$ thanks @ShadyProductions , I'll give this a shot and see if I can get it to work. :) I might need to pester you some more if I run into any snags. ;)

Show more comments

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

108 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 avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

4.6 UI Text rect does not expand automatically 2 Answers

uGUI letter spacing and kerning 1 Answer

Missing canvas elements on Build & Run. 2 Answers

How to change color of animated Text component 2 Answers

Inputfield text to String variable 1 Answer


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