Search Unity

Loading files at runtime without freezing UI

Discussion in 'Editor & General Support' started by bsizzle, Mar 23, 2020.

  1. bsizzle

    bsizzle

    Joined:
    May 25, 2013
    Posts:
    11
    I know there are a lot of posts talking about how to load a lot of images/files at runtime but none seem to clear up what I want to do. My project loads a few xml files at runtime that will be stored on the user's local storage. The files will take about 1-2 minutes to read. The load time is not the problem, but I can't update the UI at all while it loads.

    What I would like to do is have some sort of progress bar or loading % update in the UI so the user can see the progress. It seems that the UI just freezes while I load and read the files. Even just adding a spinner so the user knows the application is working would be great.

    I am reading the XML files like this:
    Code (CSharp):
    1. private void addActiveItems()
    2.     {
    3.         XmlDocument xmlDoc = new XmlDocument(); // xmlDoc is the new xml document.
    4.         xmlDoc.Load(SD.UserDataPath + "frames.xml"); // load the file.
    5.         XmlNodeList framesList = xmlDoc.GetElementsByTagName("frames"); // array of the level nodes.
    6.         int totalFramesInList = framesList.Count;
    7.         foreach (XmlNode frames in framesList)
    8.         {
    9.             count++
    10.             //Here I do the work of adding items to lists.
    11.             //Would like to update the UI to show progress i.e. "Loading progress: count/totalFramesInList"
    12.         }
    13.     }
    Is there any way to do this either on a background thread or some way to update the UI while loading?
     
  2. bsizzle

    bsizzle

    Joined:
    May 25, 2013
    Posts:
    11
    Honestly the more I think about this it seems impossible on a background thread. With each item in the list I am creating a UI element to interact with, meaning I need to use the main UI thread.

    I don't think I can delete this thread, I'd be ok with moderators deleting it if needed.