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 PrisVas · Oct 21, 2015 at 01:30 PM · loadingwhilewhile-loopdialogwhile loop

Display a loading while processing a while loop

Hello, In one moment of my game, the player is able to import a file to the game. That's the function that read each line of the file:

void readTextFile(string file_path) { StreamReader inp_stm = new StreamReader(file_path);

     while (!inp_stm.EndOfStream)
     {
         string inp_ln = inp_stm.ReadLine();
         print(inp_ln);
         // Do Something with the input. 
     }

     inp_stm.Close();
 }

The problem is that when import a huge file, it takes almost 3 minutes to read it, so it looks like it stoped responing, but is acctually processing the file. So, I would like a loading dialog to show the user that the application did not stop. But how I do that if the application is tied in the "while"? Thanks.

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
Best Answer

Answer by fafase · Oct 21, 2015 at 01:39 PM

StreamReader is working in one run so you cannot cut it in sub section.

You'd be better off trying with WWW class using a local url. Then you can yield and animate something in the meanwhile.

Comment
Add comment · Show 4 · 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 PrisVas · Oct 21, 2015 at 04:22 PM 0
Share

I'm not familiar with this class. Could you explain more?

avatar image PrisVas PrisVas · Oct 21, 2015 at 06:31 PM 0
Share

That's what i got so far:

 IEnumerator readTwo(string file_path)
 {
     WWW www = new WWW("file:///" + file_path);
     yield return www;
     //print(www.text);
     print(www.text);
 }

But it reads all the docmunt once, and not line by line. But it runs faster than the other method. But, it still stops responding while processing.

avatar image fafase · Oct 23, 2015 at 08:28 AM 0
Share

Yes the result is one file. If you need to cut it by lines then you need to perform that action yourself:

     string[] lines = www.text.Split(new string[] { "\r\n", "\n" }, StringSplitOptions.None);

the WWW class is meant to process HTTP requests (GET/POST). The argument is the url, that one can be a server location or a local file.

It does not read line by line, but your original code did not either. It loads the whole into a buffer and converts it into a string array. Then you are reading line by line.

  IEnumerator readTwo(string file_path)
  {
      WWW www = new WWW("file:///" + file_path);
      yield return www;
      string[] lines = www.text.Split(new string[] { "\r\n", "\n" }, StringSplitOptions.None);
      foreach(string line in lines)
      {
            Debug.Log(line);
            yield return null;
      }
  }

The code above will load the file asynchronously, that means it processes a bit (maybe on local file it does the whole thing at once, dunno) and returns control, then processes a bit more and so on until all done.

Once you get the whole file done, the Split method cuts it. The foreach loop runs through the array and returns the control to the app on each line.

As a result, you can have other scripts perfor$$anonymous$$g action in the middle.

avatar image PrisVas fafase · Oct 23, 2015 at 12:25 PM 0
Share

O$$anonymous$$G! That´s it! thank you so much! :D

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

29 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

Related Questions

Using a while loops to repeatedly execute a method? 2 Answers

Quaternion.Slerp issue 1 Answer

How to get something to be inactive while there is a certain value 2 Answers

Iterator variable not increasing. 1 Answer

While loop freezes unity 3 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