Change Resources.Load to StreamingAssets folder

I have this code to read from XML file:

     public class ItemContainer
            {
                [XmlArray("Items")]
                [XmlArrayItem("Item")]
                public List<Item> items = new List<Item>();
     
                public static ItemContainer Load(string path)
                {
                    TextAsset _xml = Resources.Load<TextAsset>(path);
     
                    XmlSerializer serializer = new XmlSerializer(typeof(ItemContainer));
     
                    StringReader reader = new StringReader(_xml.text);
     
                    ItemContainer items = serializer.Deserialize(reader) as ItemContainer;
                    reader.Close();
                    return items;
                }
            }

How can I change Resources.Load to use StreamingAssets folder?

You’ll have to load it using System.IO and the streaming assets data path I believe. Unity handles the path for you with Application.streamingAssetsPath. I have a couple older JSON tutorials that show loading and saving using Application.dataPath, so just swap that for the assets path and it should be the same:

Loading video (note, it’s the resources folder but we’re not using Resources.Load or anything)
Saving video

2 Likes

@Austin-Gregory thank you very much! I used File.ReadAllText and now it works!

Here’s the code incase someone needs fixed version:

public class ItemContainer
    {
        [XmlArray("Items")]
        [XmlArrayItem("Item")]
        public List<Item> items = new List<Item>();

        public static ItemContainer Load(string path)
        {
            //TextAsset _xml = Resources.Load<TextAsset>(path);
            string _xml = File.ReadAllText(Application.streamingAssetsPath + path);

            XmlSerializer serializer = new XmlSerializer(typeof(ItemContainer));

            StringReader reader = new StringReader(_xml);

            ItemContainer items = serializer.Deserialize(reader) as ItemContainer;
            reader.Close();
            return items;
        }
    }