Stop the long pause before a unity game is started in the unity editor

by Dan   Last Updated January 13, 2018 11:13 AM

I am writing a Unity project to parse a large amount of data from a text file and as far as I can tell it works correctly for what I want to use it for.

However, there is a problem. When there is a lot of data I am trying to parse and I start the unity project in the editor, there is a long pause. Also, the data is parsed almost instantly.

Is there a way to start the editor, then parse the data?

The code I am using to parse the text file is the following. Based off the Coroutine logic in this answer. To use it just attach it to an empty GameObject and then pass in a TextAsset.

using UnityEngine;

public class DataManager : MonoBehaviour
{
    [SerializeField]
    private TextAsset textFile;
    [SerializeField]
    private System.Collections.Generic.List<DataEntry> data;
    private string textData;
    private string[] splitText;
    private int entryCount = -1;

    private void Start()
    {
        StartCoroutine(HandleData());
    }

    private System.Collections.IEnumerator HandleData()
    {
        bool workDone = false;
        while (!workDone)
        {
            yield return null;

            Debug.Log("Loading File");
            textData = textFile.ToString();
            Debug.Log("File Loaded");

            Debug.Log("Finding Entries");
            splitText = textData.Split(new char[] { '\n' }, System.StringSplitOptions.RemoveEmptyEntries);
            entryCount = splitText.Length;
            Debug.Log("Found " + entryCount + " entries");

            Debug.Log("Adding Data Entries");
            for (int i = 0; i < entryCount; i++)
            {
                data.Add(new DataEntry(splitText[i].Split(new char[] { ' ' }, System.StringSplitOptions.RemoveEmptyEntries)));
            }
            Debug.Log("All Done");

            workDone = true;
        }
    }

    [System.Serializable]
    private class DataEntry
    {
        public string time;
        public string extraVar1;
        public string extraVar2;
        public string extraVar3;
        public string extraVar4;
        public string extraVar5;
        public string extraVar6;
        public string extraVar7;
        public string extraVar8;

        public DataEntry(string[] info)
        {
            time = info[0];
            extraVar1 = info[1];
            extraVar2 = info[2];
            extraVar3 = info[3];
            extraVar4 = info[4];
            extraVar5 = info[5];
            extraVar6 = info[6];
            extraVar7 = info[7];
            extraVar8 = info[8];
        }
    }
}

In order to not crash Unity and see the size of the array holding the data entries I made a small custom editor class.

using UnityEditor;

[CustomEditor(typeof(DataManager))]
public class DataManagerEditor : Editor
{
    DataManager dM;

    private void OnEnable()
    {
        dM = (DataManager)target;
    }

    public override void OnInspectorGUI()
    {
        EditorGUILayout.LabelField("Data Count: " + serializedObject.FindProperty("data").arraySize);
        base.OnInspectorGUI();
    }
}

A sample text file can be downloaded from this Dropbox Link. Or if you are apprehensive of downloading random files from the internet, you can generate a sample data text file in the correct format using this Java code.

Note: You need to make the empty text file first and then change the FILE_PATH variable in the code to the text file file path for the code to run

import java.awt.EventQueue;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ThreadLocalRandom;

public class TxtGen {
    private static String FILE_PATH = "Wherever you want to save the file\\Data.txt";
    private List<DataEntry> dataToWrite = new ArrayList<DataEntry>();

    private TxtGen() {
        genData();
        writeTextFile();
    }

    private void genData() {
        long startTime = System.currentTimeMillis();
        int max = 1324;

        for(int i = 0; i < 200000; i++) {
            long endTime   = System.currentTimeMillis();
            long totalTime = endTime - startTime;
            dataToWrite.add(new DataEntry(Long.toString(totalTime),
                    Integer.toString(ThreadLocalRandom.current().nextInt(0, max * 2 + 1)),
                    Integer.toString(ThreadLocalRandom.current().nextInt(0, max * 3 + 1)),
                    Integer.toString(ThreadLocalRandom.current().nextInt(0, max * 4 + 1)),
                    Integer.toString(ThreadLocalRandom.current().nextInt(0, max * 5 + 1)),
                    Integer.toString(ThreadLocalRandom.current().nextInt(0, max * 6 + 1)),
                    Integer.toString(ThreadLocalRandom.current().nextInt(0, max * 7 + 1)),
                    Integer.toString(ThreadLocalRandom.current().nextInt(0, max * 8 + 1)),
                    Integer.toString(ThreadLocalRandom.current().nextInt(0, max * 9 + 1)),
                    Integer.toString(ThreadLocalRandom.current().nextInt(0, max * 10 + 1)),
                    Integer.toString(ThreadLocalRandom.current().nextInt(0, max * 11 + 1)),
                    Integer.toString(ThreadLocalRandom.current().nextInt(0, max * 12 + 1)),
                    Integer.toString(ThreadLocalRandom.current().nextInt(0, max * 13 + 1)),
                    Integer.toString(ThreadLocalRandom.current().nextInt(0, max * 14 + 1)),
                    Integer.toString(ThreadLocalRandom.current().nextInt(0, max * 15 + 1)),
                    Integer.toString(ThreadLocalRandom.current().nextInt(0, max * 16 + 1)),
                    Integer.toString(ThreadLocalRandom.current().nextInt(0, max * 17 + 1)),
                    Integer.toString(ThreadLocalRandom.current().nextInt(0, max * 18 + 1)),
                    Integer.toString(ThreadLocalRandom.current().nextInt(0, max * 19 + 1)),
                    Integer.toString(ThreadLocalRandom.current().nextInt(0, max * 20 + 1)),
                    Integer.toString(ThreadLocalRandom.current().nextInt(0, max * 21 + 1)),
                    Integer.toString(ThreadLocalRandom.current().nextInt(0, max * 22 + 1)),
                    Integer.toString(ThreadLocalRandom.current().nextInt(0, max * 23 + 1)),
                    Integer.toString(ThreadLocalRandom.current().nextInt(0, max * 24 + 1)),
                    Integer.toString(ThreadLocalRandom.current().nextInt(0, max * 25 + 1)),
                    Integer.toString(ThreadLocalRandom.current().nextInt(0, max * 26 + 1)),
                    Integer.toString(ThreadLocalRandom.current().nextInt(0, max * 27 + 1)),
                    Integer.toString(ThreadLocalRandom.current().nextInt(0, max * 28 + 1)),
                    Integer.toString(ThreadLocalRandom.current().nextInt(0, max * 29 + 1)),
                    Integer.toString(ThreadLocalRandom.current().nextInt(0, max * 30 + 1)),
                    Integer.toString(ThreadLocalRandom.current().nextInt(0, max * 31 + 1)),
                    Integer.toString(ThreadLocalRandom.current().nextInt(0, max * 32 + 1)),
                    Integer.toString(ThreadLocalRandom.current().nextInt(0, max * 33 + 1)),
                    Integer.toString(ThreadLocalRandom.current().nextInt(0, max * 34 + 1)),
                    Integer.toString(ThreadLocalRandom.current().nextInt(0, max * 35 + 1)),
                    Integer.toString(ThreadLocalRandom.current().nextInt(0, max * 36 + 1)),
                    Integer.toString(ThreadLocalRandom.current().nextInt(0, max * 37 + 1)),
                    Integer.toString(ThreadLocalRandom.current().nextInt(0, max * 38 + 1)),
                    Integer.toString(ThreadLocalRandom.current().nextInt(0, max * 39 + 1)),
                    Integer.toString(ThreadLocalRandom.current().nextInt(0, max * 40 + 1)),
                    Integer.toString(ThreadLocalRandom.current().nextInt(0, max * 41 + 1)),
                    Integer.toString(ThreadLocalRandom.current().nextInt(0, max * 42 + 1)),
                    Integer.toString(ThreadLocalRandom.current().nextInt(0, max * 43 + 1)),
                    Integer.toString(ThreadLocalRandom.current().nextInt(0, max * 44 + 1)),
                    Integer.toString(ThreadLocalRandom.current().nextInt(0, max * 45 + 1)),
                    Integer.toString(ThreadLocalRandom.current().nextInt(0, max * 46 + 1)),
                    Integer.toString(ThreadLocalRandom.current().nextInt(0, max * 47 + 1)),
                    Integer.toString(ThreadLocalRandom.current().nextInt(0, max * 48 + 1)),
                    Integer.toString(ThreadLocalRandom.current().nextInt(0, max * 49 + 1)),
                    Integer.toString(ThreadLocalRandom.current().nextInt(0, max * 50 + 1)),
                    Integer.toString(ThreadLocalRandom.current().nextInt(0, max * 51 + 1)),
                    Integer.toString(ThreadLocalRandom.current().nextInt(0, max * 52 + 1)),
                    Integer.toString(ThreadLocalRandom.current().nextInt(0, max * 53 + 1)),
                    Integer.toString(ThreadLocalRandom.current().nextInt(0, max * 54 + 1)),
                    Integer.toString(ThreadLocalRandom.current().nextInt(0, max * 55 + 1)),
                    Integer.toString(ThreadLocalRandom.current().nextInt(0, max * 56 + 1)),
                    Integer.toString(ThreadLocalRandom.current().nextInt(0, max * 57 + 1)),
                    Integer.toString(ThreadLocalRandom.current().nextInt(0, max * 58 + 1)),
                    Integer.toString(ThreadLocalRandom.current().nextInt(0, max * 59 + 1)),
                    Integer.toString(ThreadLocalRandom.current().nextInt(0, max * 60 + 1)),
                    Integer.toString(ThreadLocalRandom.current().nextInt(0, max * 61 + 1)),
                    Integer.toString(ThreadLocalRandom.current().nextInt(0, max * 62 + 1)),
                    Integer.toString(ThreadLocalRandom.current().nextInt(0, max * 63 + 1)),
                    Integer.toString(ThreadLocalRandom.current().nextInt(0, max * 64 + 1)),
                    Integer.toString(ThreadLocalRandom.current().nextInt(0, max * 65 + 1)),
                    Integer.toString(ThreadLocalRandom.current().nextInt(0, max * 66 + 1)),
                    Integer.toString(ThreadLocalRandom.current().nextInt(0, max * 67 + 1)),
                    Integer.toString(ThreadLocalRandom.current().nextInt(0, max * 68 + 1)),
                    Integer.toString(ThreadLocalRandom.current().nextInt(0, max * 69 + 1)),
                    Integer.toString(ThreadLocalRandom.current().nextInt(0, max * 70 + 1)),
                    Integer.toString(ThreadLocalRandom.current().nextInt(0, max * 71 + 1)),
                    Integer.toString(ThreadLocalRandom.current().nextInt(0, max * 72 + 1)),
                    Integer.toString(ThreadLocalRandom.current().nextInt(0, max * 73 + 1)),
                    Integer.toString(ThreadLocalRandom.current().nextInt(0, max * 74 + 1)),
                    Integer.toString(ThreadLocalRandom.current().nextInt(0, max * 75 + 1)),
                    Integer.toString(ThreadLocalRandom.current().nextInt(0, max * 76 + 1)),
                    Integer.toString(ThreadLocalRandom.current().nextInt(0, max * 77 + 1))));
            System.out.println("Adding entry " + i);
            try {
                Thread.sleep(1);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

    private void writeTextFile() {
        try {
            PrintWriter writer = new PrintWriter(FILE_PATH, "UTF-8");

            int i = 0;

            for(DataEntry dE : dataToWrite) {
                writer.println(dE.toString());
                System.out.println("Writing entry " + i);
                i++;
            }
            writer.close();

        } catch (FileNotFoundException | UnsupportedEncodingException e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        EventQueue.invokeLater(() -> new TxtGen());
    }

    private class DataEntry {
        private String var1;
        private String var2;
        private String var3;
        private String var4;
        private String var5;
        private String var6;
        private String var7;
        private String var8;
        private String var9;
        private String var10;
        private String var11;
        private String var12;
        private String var13;
        private String var14;
        private String var15;
        private String var16;
        private String var17;
        private String var18;
        private String var19;
        private String var20;
        private String var21;
        private String var22;
        private String var23;
        private String var24;
        private String var25;
        private String var26;
        private String var27;
        private String var28;
        private String var29;
        private String var30;
        private String var31;
        private String var32;
        private String var33;
        private String var34;
        private String var35;
        private String var36;
        private String var37;
        private String var38;
        private String var39;
        private String var40;
        private String var41;
        private String var42;
        private String var43;
        private String var44;
        private String var45;
        private String var46;
        private String var47;
        private String var48;
        private String var49;
        private String var50;
        private String var51;
        private String var52;
        private String var53;
        private String var54;
        private String var55;
        private String var56;
        private String var57;
        private String var58;
        private String var59;
        private String var60;
        private String var61;
        private String var62;
        private String var63;
        private String var64;
        private String var65;
        private String var66;
        private String var67;
        private String var68;
        private String var69;
        private String var70;
        private String var71;
        private String var72;
        private String var73;
        private String var74;
        private String var75;
        private String var76;
        private String var77;

        public DataEntry(String var1a,
                String var2a,
                String var3a,
                String var4a,
                String var5a,
                String var6a,
                String var7a,
                String var8a,
                String var9a,
                String var10a,
                String var11a,
                String var12a,
                String var13a,
                String var14a,
                String var15a,
                String var16a,
                String var17a,
                String var18a,
                String var19a,
                String var20a,
                String var21a,
                String var22a,
                String var23a,
                String var24a,
                String var25a,
                String var26a,
                String var27a,
                String var28a,
                String var29a,
                String var30a,
                String var31a,
                String var32a,
                String var33a,
                String var34a,
                String var35a,
                String var36a,
                String var37a,
                String var38a,
                String var39a,
                String var40a,
                String var41a,
                String var42a,
                String var43a,
                String var44a,
                String var45a,
                String var46a,
                String var47a,
                String var48a,
                String var49a,
                String var50a,
                String var51a,
                String var52a,
                String var53a,
                String var54a,
                String var55a,
                String var56a,
                String var57a,
                String var58a,
                String var59a,
                String var60a,
                String var61a,
                String var62a,
                String var63a,
                String var64a,
                String var65a,
                String var66a,
                String var67a,
                String var68a,
                String var69a,
                String var70a,
                String var71a,
                String var72a,
                String var73a,
                String var74a,
                String var75a,
                String var76a,
                String var77a) {
            var1 = var1a;
            var2 = var2a;
            var3 = var3a;
            var4 = var4a;
            var5 = var5a;
            var6 = var6a;
            var7 = var7a;
            var8 = var8a;
            var9 = var9a;
            var10 = var10a;
            var11 = var11a;
            var12 = var12a;
            var13 = var13a;
            var14 = var14a;
            var15 = var15a;
            var16 = var16a;
            var17 = var17a;
            var18 = var18a;
            var19 = var19a;
            var20 = var20a;
            var21 = var21a;
            var22 = var22a;
            var23 = var23a;
            var24 = var24a;
            var25 = var25a;
            var26 = var26a;
            var27 = var27a;
            var28 = var28a;
            var29 = var29a;
            var30 = var30a;
            var31 = var31a;
            var32 = var32a;
            var33 = var33a;
            var34 = var34a;
            var35 = var35a;
            var36 = var36a;
            var37 = var37a;
            var38 = var38a;
            var39 = var39a;
            var40 = var40a;
            var41 = var41a;
            var42 = var42a;
            var43 = var43a;
            var44 = var44a;
            var45 = var45a;
            var46 = var46a;
            var47 = var47a;
            var48 = var48a;
            var49 = var49a;
            var50 = var50a;
            var51 = var51a;
            var52 = var52a;
            var53 = var53a;
            var54 = var54a;
            var55 = var55a;
            var56 = var56a;
            var57 = var57a;
            var58 = var58a;
            var59 = var59a;
            var60 = var60a;
            var61 = var61a;
            var62 = var62a;
            var63 = var63a;
            var64 = var64a;
            var65 = var65a;
            var66 = var66a;
            var67 = var67a;
            var68 = var68a;
            var69 = var69a;
            var70 = var70a;
            var71 = var71a;
            var72 = var72a;
            var73 = var73a;
            var74 = var74a;
            var75 = var75a;
            var76 = var76a;
            var77 = var77a;
        }

        public String toString() { 
            return  "     " + var1 +
                    "     " + var2 +
                    "     " + var3 +
                    "     " + var4 +
                    "     " + var5 +
                    "     " + var6 +
                    "     " + var7 +
                    "     " + var8 +
                    "     " + var9 +
                    "     " + var10 +
                    "     " + var11 +
                    "     " + var12 +
                    "     " + var13 +
                    "     " + var14 +
                    "     " + var15 +
                    "     " + var16 +
                    "     " + var17 +
                    "     " + var18 +
                    "     " + var19 +
                    "     " + var20 +
                    "     " + var21 +
                    "     " + var22 +
                    "     " + var23 +
                    "     " + var24 +
                    "     " + var25 +
                    "     " + var26 +
                    "     " + var27 +
                    "     " + var28 +
                    "     " + var29 +
                    "     " + var30 +
                    "     " + var31 +
                    "     " + var32 +
                    "     " + var33 +
                    "     " + var34 +
                    "     " + var35 +
                    "     " + var36 +
                    "     " + var37 +
                    "     " + var38 +
                    "     " + var39 +
                    "     " + var40 +
                    "     " + var41 +
                    "     " + var42 +
                    "     " + var43 +
                    "     " + var44 +
                    "     " + var45 +
                    "     " + var46 +
                    "     " + var47 +
                    "     " + var48 +
                    "     " + var49 +
                    "     " + var50 +
                    "     " + var51 +
                    "     " + var52 +
                    "     " + var53 +
                    "     " + var54 +
                    "     " + var55 +
                    "     " + var56 +
                    "     " + var57 +
                    "     " + var58 +
                    "     " + var59 +
                    "     " + var60 +
                    "     " + var61 +
                    "     " + var62 +
                    "     " + var63 +
                    "     " + var64 +
                    "     " + var65 +
                    "     " + var66 +
                    "     " + var67 +
                    "     " + var68 +
                    "     " + var69 +
                    "     " + var70 +
                    "     " + var71 +
                    "     " + var72 +
                    "     " + var73 +
                    "     " + var74 +
                    "     " + var75 +
                    "     " + var76 +
                    "     " + var77;
        }
    }
}

Cross Post



Related Questions





NullReferenceException in StartCoroutine method

Updated November 25, 2016 08:05 AM

Best way to queue animation coroutines in unity

Updated April 15, 2017 00:13 AM