- Home /
Reading from a text in Unity is a lot slower than C# console application
I have a text file from which i read values and create meshes. However, this operation was too slow so i tried some different approaches. Then i realized actually reading from text in Unity is really slow.
I have created a script for just reading the data line by line in Unity, C# console application and C++ console application. Reading 75 million line takes about 45 - 50 sec in C#, 30 - 35 sec in C++ and around 3 minutes in Unity. I am not doing any operation i am just reading the lines using StreamReader
like this: string line; while ((line = sr.ReadLine())!=null) { }
I have tried it in a seperate thread in Unity as well and result is still 3 minutes. I am really confused about this. Can anyone explain me why this is the case?. I am reading from same disk and i can't explain why this occurs because if i do the extra operations i wanted to do it goes up to 15 to 20 minutes with split
and creating the meshes. I would also appreciate if you can suggest me something faster than StreamReader
. I tried BufferedReader
as well the result was more or less similar. Thanks in advance for any help!
Answer by alikanat · Jan 22, 2019 at 12:32 PM
Well i actually realized what the problem is. I was trying my code in Editor but when i build it for windows it read all the lines around 30 seconds.
Answer by sujitmarcus · Jan 22, 2019 at 11:49 AM
I don't know about more optimization method with current text file I/O but. You can use JSON as an alternative. Here are some Links to get started.
[1]: https://unity3d.com/learn/tutorials/topics/scripting/loading-game-data-json
[2]: https://docs.unity3d.com/Manual/JSONSerialization.html
Thanks for the suggestion but i must use a text file i do not have another option :(
Your answer
![](https://koobas.hobune.stream/wayback/20220612190227im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
How would I script text to pop up? 1 Answer
I can't get Text Mesh to disappear in the scene / How do I make the Text Mesh disappear in my scene 0 Answers
Inspector changes set TextMeshPro values on play 1 Answer
Error CS0029: Cannot implicitly convert type void to type UnityEngine.AsyncOperation 1 Answer
UI Text showing weird string value? 1 Answer