Search code examples
c#xmlunity-game-engineunity-webgl

XML file not included in WebGL Build


I'm trying to build my game for the WebGL platform. My game is getting data from an XML file located in the project's root folder. It works fine in the editor and also if building for Windows. But when I build for WebGL the game doesn't work whatsoever and the console shows this: IsolatedStorageException: Could not find file "/Words.xml".

This is the code I'm using to read the file:

public List<string> Words = new List<string>();

public void SetWords ()
    {
        using (XmlReader reader = XmlReader.Create("Words.xml"))
        {
            while (reader.Read())
            {
                if (reader.Name == "word") Words.Add(reader.ReadString());
            }
        }
    }

Solution

  • You should put the file in Assets folder.

    Define a TextAsset field in your script then drag the xml file onto it.

    public TextAsset xmlfile;
    
    void SomeMethod()
    {
        var reader = XmlReader.Create(new MemoryStream(xmlfile.bytes));
    }