0

What I am trying to do is to read the xml file from an url which is my github repo via raw file in Unity and add it to my arrayList with class as the datatype. However, everything works well on Unity but when it's on the browser, the game will freeze when loading the scene that the script is in. On my Chrome inspects, it showed an error called, 2 FS.syncfs operations in flight at once, probably just doing extra work. I also noticed that my browser CPU usage is at maximum when the game is trying to load the scene.

Here is my code for reading the xml file and adding it to my arrayList. On the void Start() method, I will StartCoroutine(ReadXML("xxxxxxx"));.

IEnumerator ReadXML(string url)
{
    try
    {
        xmldoc = XDocument.Load(@url);

        var query = from p in xmldoc.Elements("popupNotify").Elements("popNoti")
                    select p;
        foreach (XElement record in query)
        {
            PopNotifi popNoti = new(record.Element("text").Value, 
                record.Element("colliderName").Value, record.Element("type").Value, record.Element("duration").Value);
            popNotifiList.Add(popNoti);
        }
        
    } catch (Exception e)
    {
        Debug.LogError(e.Message);
    }
    yield return url;
}
dbc
  • 104,963
  • 20
  • 228
  • 340
  • 1
    File may be huge and will take a long time to read to the end. Also updating view after each record is added will slow down application. Wait if possible until all record are found and then use AddRange(). – jdweng Aug 17 '23 at 04:30
  • Does Unity support async programming? If so, consider rewriting your code to be `async` and use [`XDocument.LoadAsync()`](https://learn.microsoft.com/en-us/dotnet/api/system.xml.linq.xdocument.loadasync) to load the file... Oh, you're already using a [coroutine](https://docs.unity3d.com/Manual/Coroutines.html). Could you use async instead? You only yield at the end so I ***believe*** (but am not sure) that implies that the entire load will be done in a single frame, according to the [docs](https://docs.unity3d.com/Manual/Coroutines.html). – dbc Aug 17 '23 at 06:38
  • Take a look at [Reading large JSON file without hanging in Unity](https://stackoverflow.com/q/66390894), maybe the same idea would apply here. – dbc Aug 17 '23 at 06:45
  • Thank you guys for your help. I manage to solve it by not using URL as a way to read the xml file. I solve it by using TextAsset. The link is here for if anyone encounter the same issue. https://youtu.be/rTgjFnTvDUc. – Vernwalker Aug 17 '23 at 08:27

0 Answers0