Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Loading text from a file is very slow, when parsing to the GUI and visually pauses Unity

Discussion in 'Scripting' started by NerdRageStudios, Dec 10, 2017.

  1. NerdRageStudios

    NerdRageStudios

    Joined:
    Nov 1, 2013
    Posts:
    167
    Hi, I am having a problem where large text files are very slow when parsing into the GUI, and can cause visible game pauses.

    First I load in the text file:
    Code (CSharp):
    1.     public void ReadFile()
    2.     {
    3.         dialogLines = new string[0];
    4.         dialogLoaded = Resources.Load<TextAsset>("Language/" + dialogPrefix + "_" + language.ToString());
    5.         string dialogString = dialogLoaded.ToString();
    6.         dialogLines = dialogString.Split("\n"[0]);
    7.     }
    Then I just read from the line number I want:
    Code (CSharp):
    1.     public string ReadDialog(int lineNumber)
    2.     {
    3.         return dialogLines[lineNumber - 1];
    4.     }
    The thing is, I have around 8 text files containing everything from dialog, GUI, ingame tool tips etc.
    The dialog file as you can imagine is rather large as it contains all the in game dialog. I think the issue might not be the size of the file, but rather the length of the string returned, as a single line is a complete dialog entry along with formatting.

    Here is an example of one line from the dialog file.

    Code (CSharp):
    1. Message Chain<br>==============================================<br>Sender: Johan Ericson<i> Warehouse Manager</i><br>To: David Watson<i> Chief Engineering Officer</i><br>Date: "03/12/2307 08:19"<br>Subject: Ammunition Crates<br>==============================================<br>Chief, do you mind telling me why, when I was inventorying the warehouse just now, I nearly put my foot through an undocumented crate? An undocumented crate which, I may add, was brimming with live goddamn ammunition! One, in fact, of several undocumented crates strewn haphazardly all over my warehouse also filled with live ammunition! Could I, as Warehouse Manager, possibly ask, Chief, what the F*** these things are doing in my goddamn warehouse?<br><br>Reply: David Watson<i> Chief Engineering Officer</i> "03/12/2307 08:24"<br>Ah, S***. Sorry Johan. You've been on leisure time so I haven't had time to brief you. The Captain and Major Haase sent a Security guy - Henry? Harvey? - down yesterday to help the janitor with a nasty pest problem in Sanitation. They also sent down some supplies and munitions for him. I got Simon and Will to unload them; I'm guessing they rushed it a bit.<br><br>Reply: Johan Ericson<i> Warehouse Manager</i> "03/12/2307 08:26"<br>Right. Sorry Chief, I'm just a little startled. Isn't live ammunition overkill for a bunch of spiders and roaches? I know Wilco has been raving for years about his "mutant vermin" but you can't tell me Security is taking him seriously?<br><br>Reply: David Watson<i> Chief Engineering Officer</i> "03/12/2307 08:27"<br>Honestly Johan, I had severe reservations myself, and wasn't shy with sharing them with the Captain. But he and the Major insisted. Chain of command and all that. They say "jump" I say "no problem sirs, of course you can put this ammunition near our volatile chemicals!". It does seem a bit much for a roach infestation, but maybe they are just throwing old Wilco a bone. He is a superb janitor, whatever else his faults may be.<br><br>Reply: Johan Ericson<i> Warehouse Manager</i> "03/12/2307 08:49"<br>Well. Alright Chief. I can't pretend to be comfortable with the idea of jarheads running around Engineering and Sanitation with loaded guns shooting at figments of Wilco's imagination, but here we are. However, I refuse to leave dangerous cargo lying around in the warehouse where the likes of Simple Simon & Co can tread all over them. So, I've moved all of the ammo crates into the storeroom and reset the passcode. You can pass it on to whoever needs direct access
    So, I am trying to know what is causing the issue?

    Is it my approach of loading from TXT file (should I be using some other way, like XML or something, no idea)

    Or, is it the string length causing a pause in the GUI? I assumed that the string length is irrelevant as the memory footprint is tiny, but maybe the GUI needs to parse this and that causes the slowdown?

    Is there a better way to do this? The reason I use external files is for having multiple languages.

    Any help would be highly appreciated!
     
  2. mgear

    mgear

    Joined:
    Aug 3, 2010
    Posts:
    8,998
  3. NerdRageStudios

    NerdRageStudios

    Joined:
    Nov 1, 2013
    Posts:
    167
    Thanks! I'll read into those now.

    I only read the file on game start, then return the lines from the array of strings when needed