Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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
1
Question by JareBear12418 · Jun 05, 2020 at 01:28 PM · unity 5textresourcesresources.loadread

How to load a large text asset with minimum to no lag.

So I have these large text files that are over 1mb in size, and unity pretty much crashes when I try and load them instantly.

So I tried using IEnumerator to load it, this got rid of the lag early on, but as the Coroutine went on, it completely slows down the application and just crashes.

 IEnumerator loadInText (TextAsset textToLoad) {
         txtText.text = "";
         if (selectedType == "Stories") {
             var arrayString = textToLoad.text.Split ('\n');
             foreach (var line in arrayString) {
                 txtText.text += line.ToString () + "\n";
                 yield return new WaitForSeconds (0.0001f);
             }
         }
         yield return null;
     }
 

The 0.0001f delay is there because unity crashes without it.

This has been a huge pain to deal with, and it seems unity is having lots of trouble loading text.

Is there anyway to put this function on it's own thread of some sort so it doesn't effect the main thread? If that is possible then It would work well enough for a fix, but it would be better if the text file just loads instantly in a couple of seconds at least.

I think it maybe important to mention that I am using TextMeshPro for text, and that may take text longer to render? I am also having my TextMeshPro inside a scroll view that auto sizes depending on the TextMeshPro , I do understand those also take up processing, but they are a mandatory component and are the only ones.

I am running this application on Mobile Android and it's even slower then. I am using Unity 2018.3

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

2 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by andrew-lukasik · Jun 05, 2020 at 02:18 PM

 using System.Threading.Tasks;

 static async Task<string> ReadTextFileInBackgroundThread ( string filePath )
 {
     if( !System.IO.File.Exists(filePath) ) 
     {
             Debug.LogWarning($"This file path is invalid tho: '{filePath}'");
             return string.Empty;
     }
     // we're still on unity thread here
     return await Task.Run(
         () =>
         {
             // we're on worker thread, so exciting!
             return System.IO.File.ReadAllText(filePath);
         }
     );
     // back on unity thread
 }

 async void Start ()
 {
     txtText.text = await ReadTextFileInBackgroundThread( @"c:\hentai\wishlist.txt" );
 }
Comment
Add comment · Show 20 · 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 andrew-lukasik · Jun 05, 2020 at 02:27 PM 2
Share

Alternatively:

 IEnumerator LoadTextJustHowHomoSapiensWould ( TextAsset textToLoad )
 {
     txtText.text = textToLoad.text;
     // done!
     yield return null;
 }

Because the rest of your code has nothing to do with loading but allocating memory for no reason.

avatar image JareBear12418 andrew-lukasik · Jun 05, 2020 at 02:31 PM 0
Share

This method I tried when I first tried the solution with IEnumerators. But It didn't improve anything what so ever. I will try your block of code above when I get home, as it looks promising. Thank you.

avatar image andrew-lukasik JareBear12418 · Jun 05, 2020 at 02:50 PM 0
Share

That's right, Coroutines run on Unity's main thread so this is why there is lag when reading files. You need to know that in your original code the only part that reads a file from disk is this text property here: var arrayString = textToLoad.text.Split ('\n'); That's it. The rest of the code was just adding busy work allocating garbage memory in a loop (immediately destroying/splitting original text and recreating it again).

Show more comments
avatar image andrew-lukasik · Jun 05, 2020 at 03:25 PM 1
Share

Word of friendly warning


$$anonymous$$ulti-threading is hard^3 to both comprehend and work with. So do not go crazy with it. Keep it very local, encapsulated in dedicated methods and best to use it only where it's really needed. Otherwise your program can easily become crazy-complex (race conditions etc. can be a nightmare to fix).

Also:

  • most unity-specific code won't work outside main thread ( forget about accessing Components, GameObjects, etc )

  • System.* namespace classes works fine on other threads

  • crashes/errors on other threads will not appear in unity console (must be sent there manually in try/catch blocks or from direct Debug.Log calls)

  • Debug.Log* works fine on different threads (thanks god!)

avatar image JareBear12418 andrew-lukasik · Jun 05, 2020 at 03:28 PM 0
Share

Just a question on this. most unity-specific code won't work outside main thread (forget about accessing Components, GameObjects, etc)

Let's say I find the gameobject within the thread, using FindObjectsWithTag and get what I need from there, like change the text, or change the value in a script. All that would still work? But using global variables in the script wont?

avatar image andrew-lukasik JareBear12418 · Jun 05, 2020 at 03:42 PM 0
Share

90% of unity methods won't work inside a worker thread and will either fail totally silently or throw you exception about being on wrong thread. FindObjectsWithTag is among them. In other words - worker threads are totally useless in dealing with scene objects.

Show more comments
avatar image JareBear12418 · Jun 05, 2020 at 05:31 PM 0
Share

@andrew-lukasik

So I implemented this function, and confirmed everything is working, but when I load my 1mb text file, it still crashes and doesn't load the text file.

Is there a way to loop over each line in the text file, and return it in the separate thread?

avatar image andrew-lukasik JareBear12418 · Jun 05, 2020 at 06:43 PM 0
Share

Ohh I totally forgot that you're trying to display 1mb of text in single Text rendering component! This probably can crash unity because it's just too much text to display at once. Two triangles (or whatever number it is) per every character in that 1mb text... is a LOT of triangles for a single mesh to handle

avatar image JareBear12418 andrew-lukasik · Jun 05, 2020 at 06:46 PM 0
Share

yea :'D Would using Unity's old Text mesh work better? since it's lower quality? I mean I loaded a 400kb file fine in a couple of seconds, but scrolling around was laggy, so ye unity is struggling with rendering. I'm sure there are optimizations to be done in terms of rendering.

Show more comments
avatar image andrew-lukasik JareBear12418 · Jun 05, 2020 at 07:18 PM 0
Share

Text$$anonymous$$eshPro is best text solution so I think your task here now is to figure out how to make Text$$anonymous$$eshPro display this text that was loaded.

  • Idea #1: split the text file into 2 parts maybe?

  • Idea #2: experiment with text component display properties like text.maxVisibleLines = 10 (it may be set too high by default for this text block)

avatar image JareBear12418 andrew-lukasik · Jun 05, 2020 at 07:22 PM 0
Share

For the Idea #1 You are referring to calling the thread 2-3 times, and it sends back 1/2 or 1/3 of the text file until it's done, That would work for the loading, but then were back to the problem where Unity and Scroll view is laggy.

For idea #2 sounds promising for solving the laggy problem when I scroll the text. I will look into changing the graphics settings for text to low, maybe that will fix the issues!

avatar image
0

Answer by JareBear12418 · Jun 08, 2020 at 03:22 PM

I recommend this asset for an easier setup process, and threading for UI and threading for not UI, Super easy to use. https://assetstore.unity.com/packages/tools/integration/task-parallel-82257

Comment
Add comment · 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

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

230 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 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 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

How do I read unicode text file on IOS from resources folder? 1 Answer

Reading .txt file from Resources folder 1 Answer

upload text file from desktop to Resources Runtime 0 Answers

Resources.Load returns null in a build, but works in editor 2 Answers

Reading level specific file in build (from another level) 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